Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix for GRAILS-6349 "Many-to-Many relationships with composite ids fail"
- Loading branch information
1 parent
a7b3577
commit 8540adf
Showing
2 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/test/org/codehaus/groovy/grails/orm/hibernate/ManyToManyCompositeIdTests.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package org.codehaus.groovy.grails.orm.hibernate; | ||
|
||
import junit.framework.TestCase; | ||
|
||
class ManyToManyCompositeIdTests extends AbstractGrailsHibernateTests { | ||
|
||
void testManyToManyDomain() { | ||
def authorDomain = ga.getDomainClass("ManyToManyCompositeIdAuthor") | ||
def bookDomain = ga.getDomainClass("ManyToManyCompositeIdBook") | ||
|
||
def books = authorDomain?.getPropertyByName("books") | ||
def authors = bookDomain?.getPropertyByName("authors") | ||
|
||
assert books?.isManyToMany() | ||
assert authors?.isManyToMany() | ||
assert !books?.isOneToMany() | ||
assert !authors?.isOneToMany() | ||
} | ||
void testManyToManyMapping() { | ||
def Author = ga.getDomainClass("ManyToManyCompositeIdAuthor").clazz | ||
def Book = ga.getDomainClass("ManyToManyCompositeIdBook").clazz | ||
|
||
def a = Author.newInstance(name:"Stephen King", family:10, child:1) | ||
|
||
a.addToBooks(Book.newInstance(title:"The Shining", isbn:5001, edition:2)) | ||
.addToBooks(Book.newInstance(title:"The Stand", isbn:3402, edition:4)) | ||
.save(true) | ||
assertEquals 2, Book.list().size() | ||
|
||
def b = Book.get(Book.newInstance(isbn:5001, edition:2)) | ||
assert b | ||
assertNotNull b.authors | ||
assertEquals 1, b.authors.size() | ||
|
||
a = Author.get(Author.newInstance(family:10, child:1)) | ||
assert a | ||
assertNotNull a.books | ||
assertEquals 2, a.books.size() | ||
|
||
|
||
assertEquals b, a.books.find { (it.isbn == 5001) && (it.edition == 2) } | ||
this.session.flush() | ||
session.clear() | ||
|
||
a = Author.get(Author.newInstance(family:10, child:1)) | ||
assert a | ||
assert a.books | ||
|
||
b = Book.get(Book.newInstance(isbn:5001, edition:2)) | ||
assert b | ||
assert b.authors | ||
|
||
// now let's test the database state | ||
def c = session.connection() | ||
|
||
def ps = c.prepareStatement("select * from many_to_many_composite_id_author_books order by many_to_many_composite_id_book_isbn") | ||
def rs = ps.executeQuery() | ||
assert rs.next() | ||
|
||
assertEquals 10,rs.getInt("many_to_many_composite_id_author_family") | ||
assertEquals 1,rs.getInt("many_to_many_composite_id_author_child") | ||
assertEquals 3402,rs.getInt("many_to_many_composite_id_book_isbn") | ||
assertEquals 4,rs.getInt("many_to_many_composite_id_book_edition") | ||
|
||
assert rs.next() | ||
assertEquals 10,rs.getInt("many_to_many_composite_id_author_family") | ||
assertEquals 1,rs.getInt("many_to_many_composite_id_author_child") | ||
assertEquals 5001,rs.getInt("many_to_many_composite_id_book_isbn") | ||
assertEquals 2,rs.getInt("many_to_many_composite_id_book_edition") | ||
} | ||
|
||
void onSetUp() { | ||
this.gcl.parseClass(''' | ||
import grails.persistence.* | ||
@Entity | ||
class ManyToManyCompositeIdBook implements Serializable { | ||
Long isbn | ||
Long edition | ||
String title | ||
static belongsTo = ManyToManyCompositeIdAuthor | ||
static hasMany = [authors:ManyToManyCompositeIdAuthor] | ||
static mapping = { | ||
id composite:['isbn', 'edition'], generator:'assigned' | ||
} | ||
} | ||
@Entity | ||
class ManyToManyCompositeIdAuthor implements Serializable { | ||
Long family | ||
Long child | ||
String name | ||
static hasMany = [books:ManyToManyCompositeIdBook] | ||
static mapping = { | ||
id composite:['family', 'child'], generator:'assigned' | ||
} | ||
} | ||
''' | ||
) | ||
} | ||
|
||
void onTearDown() { | ||
|
||
} | ||
} |