Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
646 additions
and
0 deletions.
There are no files selected for viewing
222 changes: 222 additions & 0 deletions
222
.../test/java/org/hibernate/jpa/test/factory/puUtil/CompositeIdDerivedIdWithIdClassTest.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.jpa.test.factory.puUtil; | ||
|
||
import java.io.Serializable; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import javax.persistence.CascadeType; | ||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.Id; | ||
import javax.persistence.IdClass; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.ManyToOne; | ||
import javax.persistence.OneToMany; | ||
|
||
import org.junit.After; | ||
import org.junit.Test; | ||
|
||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; | ||
|
||
import org.hibernate.testing.TestForIssue; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class CompositeIdDerivedIdWithIdClassTest extends BaseEntityManagerFunctionalTestCase { | ||
@Override | ||
protected Class<?>[] getAnnotatedClasses() { | ||
return new Class<?>[] { | ||
ShoppingCart.class, | ||
LineItem.class | ||
}; | ||
} | ||
|
||
@After | ||
public void cleanup() { | ||
EntityManager em = getOrCreateEntityManager(); | ||
em.getTransaction().begin(); | ||
em.createQuery( "delete from LineItem" ).executeUpdate(); | ||
em.createQuery( "delete from Cart" ).executeUpdate(); | ||
em.getTransaction().commit(); | ||
em.close(); | ||
} | ||
|
||
@Test | ||
@TestForIssue(jiraKey = "HHH-11328") | ||
public void testMergeTransientIdManyToOne() throws Exception { | ||
ShoppingCart transientCart = new ShoppingCart( "cart1" ); | ||
transientCart.addLineItem( new LineItem( 0, "description2", transientCart ) ); | ||
|
||
// assertion for HHH-11274 - checking for exception | ||
//final Object identifier = new PersistenceUnitUtilImpl( sessionFactory() ).getIdentifier( transientCart.getLineItems().get( 0 ) ); | ||
final Object identifier = entityManagerFactory().getPersistenceUnitUtil().getIdentifier( | ||
transientCart.getLineItems() | ||
.get( 0 ) | ||
); | ||
|
||
// merge ID with transient many-to-one | ||
EntityManager em = getOrCreateEntityManager(); | ||
em.getTransaction().begin(); | ||
em.merge( transientCart ); | ||
em.getTransaction().commit(); | ||
em.close(); | ||
|
||
em = getOrCreateEntityManager(); | ||
em.getTransaction().begin(); | ||
ShoppingCart updatedCart = em.find( ShoppingCart.class, "cart1" ); | ||
// assertion for HHH-11274 - checking for exception | ||
// new PersistenceUnitUtilImpl( sessionFactory() ).getIdentifier( transientCart.getLineItems().get( 0 ) ); | ||
entityManagerFactory().getPersistenceUnitUtil().getIdentifier( transientCart.getLineItems().get( 0 ) ); | ||
assertEquals( 1, updatedCart.getLineItems().size() ); | ||
assertEquals( "description2", updatedCart.getLineItems().get( 0 ).getDescription() ); | ||
em.getTransaction().commit(); | ||
em.close(); | ||
} | ||
|
||
@Test | ||
@TestForIssue(jiraKey = "HHH-10623") | ||
public void testMergeDetachedIdManyToOne() throws Exception { | ||
ShoppingCart cart = new ShoppingCart("cart1"); | ||
|
||
EntityManager em = getOrCreateEntityManager(); | ||
em.getTransaction().begin(); | ||
em.persist( cart ); | ||
em.getTransaction().commit(); | ||
em.close(); | ||
|
||
// cart is detached now | ||
LineItem lineItem = new LineItem( 0, "description2", cart ); | ||
cart.addLineItem( lineItem ); | ||
|
||
// merge lineItem with an ID with detached many-to-one | ||
em = getOrCreateEntityManager(); | ||
em.getTransaction().begin(); | ||
em.merge(lineItem); | ||
em.getTransaction().commit(); | ||
em.close(); | ||
|
||
em = getOrCreateEntityManager(); | ||
em.getTransaction().begin(); | ||
ShoppingCart updatedCart = em.find( ShoppingCart.class, "cart1" ); | ||
assertEquals( 1, updatedCart.getLineItems().size() ); | ||
assertEquals("description2", updatedCart.getLineItems().get( 0 ).getDescription()); | ||
em.getTransaction().commit(); | ||
em.close(); | ||
} | ||
|
||
@Entity(name = "Cart") | ||
public static class ShoppingCart { | ||
@Id | ||
@Column(name = "id", nullable = false) | ||
private String id; | ||
|
||
@OneToMany(mappedBy = "cart", cascade = CascadeType.ALL, orphanRemoval = true) | ||
private List<LineItem> lineItems = new ArrayList<>(); | ||
|
||
protected ShoppingCart() { | ||
} | ||
|
||
public ShoppingCart(String id) { | ||
this.id = id; | ||
} | ||
|
||
public List<LineItem> getLineItems() { | ||
return lineItems; | ||
} | ||
|
||
public void addLineItem(LineItem lineItem) { | ||
lineItems.add(lineItem); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
ShoppingCart that = (ShoppingCart) o; | ||
return Objects.equals( id, that.id ); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id); | ||
} | ||
} | ||
|
||
@Entity(name = "LineItem") | ||
@IdClass(LineItem.Pk.class) | ||
public static class LineItem { | ||
|
||
@Id | ||
@Column(name = "item_seq_number", nullable = false) | ||
private Integer sequenceNumber; | ||
|
||
@Column(name = "description") | ||
private String description; | ||
|
||
@Id | ||
@ManyToOne | ||
@JoinColumn(name = "cart_id") | ||
private ShoppingCart cart; | ||
|
||
protected LineItem() { | ||
} | ||
|
||
public LineItem(Integer sequenceNumber, String description, ShoppingCart cart) { | ||
this.sequenceNumber = sequenceNumber; | ||
this.description = description; | ||
this.cart = cart; | ||
} | ||
|
||
public Integer getSequenceNumber() { | ||
return sequenceNumber; | ||
} | ||
|
||
public ShoppingCart getCart() { | ||
return cart; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof LineItem)) return false; | ||
LineItem lineItem = (LineItem) o; | ||
return Objects.equals(getSequenceNumber(), lineItem.getSequenceNumber()) && | ||
Objects.equals(getCart(), lineItem.getCart()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash( getSequenceNumber(), getCart() ); | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public static class Pk implements Serializable { | ||
public Integer sequenceNumber; | ||
public String cart; | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof Pk)) return false; | ||
Pk pk = (Pk) o; | ||
return Objects.equals(sequenceNumber, pk.sequenceNumber) && | ||
Objects.equals(cart, pk.cart); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(sequenceNumber, cart); | ||
} | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...t/java/org/hibernate/jpa/test/factory/puUtil/CompositeIdDerivedIdWithIdClassTest.java.rej
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,30 @@ | ||
--- hibernate-entitymanager/src/test/java/org/hibernate/test/annotations/derivedidentities/bidirectional/CompositeIdDerivedIdWithIdClassTest.java | ||
+++ hibernate-entitymanager/src/test/java/org/hibernate/test/annotations/derivedidentities/bidirectional/CompositeIdDerivedIdWithIdClassTest.java | ||
@@ -23,6 +23,8 @@ import org.junit.After; | ||
import org.junit.Test; | ||
|
||
import org.hibernate.Session; | ||
+import org.hibernate.jpa.internal.PersistenceUnitUtilImpl; | ||
+ | ||
import org.hibernate.testing.TestForIssue; | ||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; | ||
|
||
@@ -53,6 +55,9 @@ public class CompositeIdDerivedIdWithIdClassTest extends BaseCoreFunctionalTestC | ||
ShoppingCart transientCart = new ShoppingCart( "cart1" ); | ||
transientCart.addLineItem( new LineItem( 0, "description2", transientCart ) ); | ||
|
||
+ // assertion for HHH-11274 - checking for exception | ||
+ final Object identifier = new PersistenceUnitUtilImpl( sessionFactory() ).getIdentifier( transientCart.getLineItems().get( 0 ) ); | ||
+ | ||
// merge ID with transient many-to-one | ||
Session s = openSession(); | ||
s.getTransaction().begin(); | ||
@@ -63,6 +68,8 @@ public class CompositeIdDerivedIdWithIdClassTest extends BaseCoreFunctionalTestC | ||
s = openSession(); | ||
s.getTransaction().begin(); | ||
ShoppingCart updatedCart = s.get( ShoppingCart.class, "cart1" ); | ||
+ // assertion for HHH-11274 - checking for exception | ||
+ new PersistenceUnitUtilImpl( sessionFactory() ).getIdentifier( transientCart.getLineItems().get( 0 ) ); | ||
assertEquals( 1, updatedCart.getLineItems().size() ); | ||
assertEquals( "description2", updatedCart.getLineItems().get( 0 ).getDescription() ); | ||
s.getTransaction().commit(); |
70 changes: 70 additions & 0 deletions
70
...-entitymanager/src/test/java/org/hibernate/jpa/test/factory/puUtil/GetIdentifierTest.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html | ||
*/ | ||
package org.hibernate.jpa.test.factory.puUtil; | ||
|
||
import java.io.Serializable; | ||
import javax.persistence.EntityManager; | ||
|
||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
/** | ||
* @author Steve Ebersole | ||
*/ | ||
public class GetIdentifierTest extends BaseEntityManagerFunctionalTestCase { | ||
@Override | ||
protected Class<?>[] getAnnotatedClasses() { | ||
return new Class[] { | ||
LegacyEntity.class, | ||
ModernEntity.class, | ||
NestedLegacyEntity.class | ||
}; | ||
} | ||
|
||
@Before | ||
public void createData() { | ||
|
||
} | ||
|
||
@After | ||
public void dropData() { | ||
|
||
} | ||
|
||
@Test | ||
public void getIdentifierTest() throws Exception { | ||
EntityManager entityManager = getOrCreateEntityManager(); | ||
entityManager.getTransaction().begin(); | ||
|
||
// This gives a NullPointerException right now. Look at HHH-10623 when this issue is fixed | ||
Serializable nestedLegacyEntityId = (Serializable) entityManager.getEntityManagerFactory() | ||
.getPersistenceUnitUtil().getIdentifier(createExisitingNestedLegacyEntity()); | ||
|
||
entityManager.getTransaction().commit(); | ||
entityManager.close(); | ||
} | ||
|
||
private NestedLegacyEntity createExisitingNestedLegacyEntity() { | ||
|
||
ModernEntity modernEntity = new ModernEntity(); | ||
modernEntity.setFoo(2); | ||
|
||
LegacyEntity legacyEntity = new LegacyEntity(); | ||
legacyEntity.setPrimitivePk1(1); | ||
legacyEntity.setPrimitivePk2(2); | ||
legacyEntity.setFoo("Foo"); | ||
|
||
NestedLegacyEntity nestedLegacyEntity = new NestedLegacyEntity(); | ||
nestedLegacyEntity.setModernEntity(modernEntity); | ||
nestedLegacyEntity.setLegacyEntity(legacyEntity); | ||
|
||
return nestedLegacyEntity; | ||
} | ||
} |
Oops, something went wrong.