Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-11005 - OneToMany generates incorrect SQL where MapKey is on supe…
…rclass and using InheritanceType.JOINED
- Loading branch information
1 parent
455d863
commit e75b017
Showing
14 changed files
with
844 additions
and
16 deletions.
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
36 changes: 36 additions & 0 deletions
36
hibernate-core/src/test/java/org/hibernate/test/onetomany/inheritance/joined/Book.java
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,36 @@ | |||
package org.hibernate.test.onetomany.inheritance.joined; | |||
|
|||
import javax.persistence.Entity; | |||
import javax.persistence.ManyToOne; | |||
import javax.persistence.Table; | |||
|
|||
@Entity | |||
@Table(name="BOOKTABJO") | |||
public class Book extends Product { | |||
|
|||
private String isbn; | |||
|
|||
@ManyToOne | |||
private Library library; | |||
|
|||
public Book() { | |||
super(); | |||
} | |||
|
|||
public Book(String inventoryCode, String isbn) { | |||
super(inventoryCode); | |||
this.isbn = isbn; | |||
} | |||
|
|||
public String getIsbn() { | |||
return isbn; | |||
} | |||
|
|||
public Library getLibrary() { | |||
return library; | |||
} | |||
|
|||
public void setLibrary(Library library) { | |||
this.library = library; | |||
} | |||
} |
52 changes: 52 additions & 0 deletions
52
hibernate-core/src/test/java/org/hibernate/test/onetomany/inheritance/joined/Library.java
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,52 @@ | |||
package org.hibernate.test.onetomany.inheritance.joined; | |||
|
|||
import java.util.HashMap; | |||
import java.util.Map; | |||
import javax.persistence.CascadeType; | |||
import javax.persistence.Entity; | |||
import javax.persistence.GeneratedValue; | |||
import javax.persistence.Id; | |||
import javax.persistence.MapKey; | |||
import javax.persistence.OneToMany; | |||
import javax.persistence.Table; | |||
|
|||
@Entity | |||
@Table(name="LIBRARYJO") | |||
public class Library { | |||
|
|||
@Id | |||
@GeneratedValue | |||
private int entid; | |||
|
|||
@OneToMany(mappedBy="library", cascade = CascadeType.ALL) | |||
@MapKey(name="inventoryCode") | |||
private Map<String,Book> booksOnInventory = new HashMap<>(); | |||
|
|||
@OneToMany(mappedBy="library", cascade = CascadeType.ALL) | |||
@MapKey(name="isbn") | |||
private Map<String,Book> booksOnIsbn = new HashMap<>(); | |||
|
|||
public int getEntid() { | |||
return entid; | |||
} | |||
|
|||
public Map<String,Book> getBooksOnInventory() { | |||
return booksOnInventory; | |||
} | |||
|
|||
public Map<String, Book> getBooksOnIsbn() { | |||
return booksOnIsbn; | |||
} | |||
|
|||
public void addBook(Book book) { | |||
book.setLibrary( this ); | |||
booksOnInventory.put( book.getInventoryCode(), book ); | |||
booksOnIsbn.put( book.getIsbn(), book ); | |||
} | |||
|
|||
public void removeBook(Book book) { | |||
book.setLibrary( null ); | |||
booksOnInventory.remove( book.getInventoryCode() ); | |||
booksOnIsbn.remove( book.getIsbn() ); | |||
} | |||
} |
142 changes: 142 additions & 0 deletions
142
...rc/test/java/org/hibernate/test/onetomany/inheritance/joined/MappedSuperclassMapTest.java
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,142 @@ | |||
package org.hibernate.test.onetomany.inheritance.joined; | |||
|
|||
import java.util.List; | |||
import java.util.Map.Entry; | |||
|
|||
import org.hibernate.testing.TestForIssue; | |||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
|
|||
import org.jboss.logging.Logger; | |||
|
|||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; | |||
import static org.junit.Assert.assertEquals; | |||
import static org.junit.Assert.assertNotNull; | |||
|
|||
@TestForIssue(jiraKey = "HHH-11005") | |||
public class MappedSuperclassMapTest extends BaseNonConfigCoreFunctionalTestCase { | |||
|
|||
private static final Logger log = Logger.getLogger( MappedSuperclassMapTest.class ); | |||
|
|||
private static final String SKU001 = "SKU001"; | |||
private static final String SKU002 = "SKU002"; | |||
private static final String WAR_AND_PEACE = "0140447938"; | |||
private static final String ANNA_KARENINA = "0140449175"; | |||
|
|||
@Override | |||
protected Class<?>[] getAnnotatedClasses() { | |||
return new Class[]{ | |||
Book.class, | |||
Library.class, | |||
Product.class | |||
}; | |||
} | |||
|
|||
@Before | |||
public void init() { | |||
doInHibernate( this::sessionFactory, sess -> { | |||
Book book1 = new Book( SKU001, WAR_AND_PEACE); | |||
Book book2 = new Book( SKU002, ANNA_KARENINA); | |||
sess.persist( book1 ); | |||
sess.flush(); | |||
sess.persist( book2 ); | |||
sess.flush(); | |||
Library library = new Library(); | |||
library.addBook( book1 ); | |||
library.addBook( book2 ); | |||
sess.persist(library); | |||
} ); | |||
} | |||
|
|||
@Test | |||
public void lookupEntities() { | |||
doInHibernate( this::sessionFactory, sess -> { | |||
List<Library> libraries = sess.createQuery( "FROM Library").list(); | |||
assertEquals(1, libraries.size()); | |||
Library library = libraries.get( 0); | |||
assertNotNull(library); | |||
|
|||
assertEquals(2, library.getBooksOnInventory().size()); | |||
|
|||
Book book = library.getBooksOnInventory().get( SKU001); | |||
assertNotNull(book); | |||
Library Library = library; | |||
Library.getBooksOnIsbn().get( WAR_AND_PEACE ); | |||
assertEquals(WAR_AND_PEACE, book.getIsbn()); | |||
|
|||
book = library.getBooksOnInventory().get(SKU002); | |||
assertNotNull(book); | |||
assertEquals(ANNA_KARENINA, book.getIsbn()); | |||
} ); | |||
} | |||
|
|||
@Test | |||
public void lookupEntities_entrySet() { | |||
doInHibernate( this::sessionFactory, sess -> { | |||
List<Library> libraries = sess.createQuery( "FROM Library").list(); | |||
assertEquals(1, libraries.size()); | |||
Library library = libraries.get( 0); | |||
assertNotNull(library); | |||
|
|||
assertEquals(2, library.getBooksOnInventory().size()); | |||
|
|||
for (Entry<String,Book> entry : library.getBooksOnInventory().entrySet()) { | |||
log.info("Found SKU " + entry.getKey() + " with ISBN " + entry.getValue().getIsbn()); | |||
} | |||
} ); | |||
} | |||
|
|||
@Test | |||
public void breakReferences() { | |||
doInHibernate( this::sessionFactory, sess -> { | |||
List<Book> books = sess.createQuery( "FROM Book").list(); | |||
assertEquals(2, books.size()); | |||
|
|||
for (Book book : books) { | |||
assertNotNull(book.getLibrary()); | |||
log.info("Found SKU " + book.getInventoryCode() + " with library " + book.getLibrary().getEntid()); | |||
} | |||
|
|||
for (Book book : books) { | |||
book.getLibrary().removeBook( book ); | |||
} | |||
} ); | |||
doInHibernate( this::sessionFactory, sess -> { | |||
List<Book> books = sess.createQuery( "FROM Book").list(); | |||
assertEquals(2, books.size()); | |||
|
|||
for (Book book : books) { | |||
if (book.getLibrary() == null ) { | |||
log.info("Found SKU " + book.getInventoryCode() + " with no library"); | |||
} | |||
} | |||
|
|||
List<Library> libraries = sess.createQuery( "FROM Library").list(); | |||
assertEquals(1, libraries.size()); | |||
Library library = libraries.get( 0); | |||
assertNotNull(library); | |||
|
|||
assertEquals(0, library.getBooksOnInventory().size()); | |||
log.info("Found Library " + library.getEntid() + " with no books"); | |||
} ); | |||
} | |||
|
|||
@Override | |||
protected boolean isCleanupTestDataRequired() { | |||
return true; | |||
} | |||
|
|||
@Override | |||
protected void cleanupTestData() throws Exception { | |||
doInHibernate( this::sessionFactory, sess -> { | |||
sess.createQuery( "delete from Book" ).executeUpdate(); | |||
sess.createQuery( "delete from Library" ).executeUpdate(); | |||
} ); | |||
} | |||
|
|||
@Override | |||
protected boolean rebuildSessionFactoryOnError() { | |||
return false; | |||
} | |||
} |
Oops, something went wrong.