diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Address.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Address.java similarity index 91% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Address.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Address.java index f79ed39f69..6c32585f35 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Address.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Address.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Embeddable; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/AddressType.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/AddressType.java similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/AddressType.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/AddressType.java index b4f661f213..7baa11bf3e 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/AddressType.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/AddressType.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Book.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Book.java similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Book.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Book.java index f6a904ebb2..8e46b5c855 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Book.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Book.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/CorpType.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/CorpType.java similarity index 88% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/CorpType.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/CorpType.java index 3ff860c3b0..4e1979de92 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/CorpType.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/CorpType.java @@ -6,11 +6,12 @@ */ //$Id$ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; + +import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; -import javax.persistence.Column; /** * @author Emmanuel Bernard diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Country.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Country.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Country.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Country.java index 1f28d7d2cf..d9b2386d64 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Country.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Country.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Embeddable; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Deal.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Deal.java similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Deal.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Deal.java index 98d7e8afc8..6761fe787f 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Deal.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Deal.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import javax.persistence.Embedded; import javax.persistence.Id; import javax.persistence.MappedSuperclass; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/EmbeddableA.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/EmbeddableA.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/EmbeddableA.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/EmbeddableA.java index 547ff0aedd..9a53d69cee 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/EmbeddableA.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/EmbeddableA.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/EmbeddableB.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/EmbeddableB.java similarity index 90% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/EmbeddableB.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/EmbeddableB.java index 25d55c7df0..5843cf99f1 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/EmbeddableB.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/EmbeddableB.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import javax.persistence.Embeddable; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/EmbeddedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/EmbeddedTest.java new file mode 100644 index 0000000000..aecb202d84 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/EmbeddedTest.java @@ -0,0 +1,789 @@ +/* + * 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 . + */ +package org.hibernate.orm.test.annotations.embedded; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Set; +import java.util.UUID; + +import org.hibernate.Hibernate; +import org.hibernate.Transaction; +import org.hibernate.boot.spi.MetadataImplementor; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.dialect.SybaseDialect; +import org.hibernate.orm.test.annotations.embedded.FloatLeg.RateIndex; +import org.hibernate.orm.test.annotations.embedded.Leg.Frequency; +import org.hibernate.query.Query; +import org.hibernate.query.spi.QueryImplementor; + +import org.hibernate.testing.FailureExpected; +import org.hibernate.testing.SkipForDialect; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.test.util.SchemaUtil; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** + * @author Emmanuel Bernard + */ +@DomainModel( + annotatedClasses = { + Person.class, + WealthyPerson.class, + RegionalArticle.class, + AddressType.class, + VanillaSwap.class, + SpreadDeal.class, + Book.class, + InternetProvider.class, + CorpType.class, + Nationality.class, + Manager.class, + FavoriteThings.class + } +) +@SessionFactory +@ServiceRegistry(settings = { + @ServiceRegistry.Setting(name = AvailableSettings.IMPLICIT_NAMING_STRATEGY, value = "jpa") +}) +public class EmbeddedTest { + + @AfterEach + public void cleanup(SessionFactoryScope scope) { + scope.inTransaction( session -> { + for ( Person person : session.createQuery( "from Person", Person.class ).getResultList() ) { + session.delete( person ); + } + session.createQuery( "delete from InternetProvider" ).executeUpdate(); + session.createQuery( "delete from Manager" ).executeUpdate(); + session.createQuery( "delete from Book" ).executeUpdate(); + session.createQuery( "delete from FavoriteThings" ).executeUpdate(); + } ); + } + + @Test + public void testSimple(SessionFactoryScope scope) { + Person person = new Person(); + Address a = new Address(); + Country c = new Country(); + Country bornCountry = new Country(); + c.setIso2( "DM" ); + c.setName( "Matt Damon Land" ); + bornCountry.setIso2( "US" ); + bornCountry.setName( "United States of America" ); + + a.address1 = "colorado street"; + a.city = "Springfield"; + a.country = c; + person.address = a; + person.bornIn = bornCountry; + person.name = "Homer"; + + scope.inTransaction( session -> session.persist( person ) ); + + scope.inTransaction( session -> { + Person p = session.get( Person.class, person.id ); + assertNotNull( p ); + assertNotNull( p.address ); + assertEquals( "Springfield", p.address.city ); + assertNotNull( p.address.country ); + assertEquals( "DM", p.address.country.getIso2() ); + assertNotNull( p.bornIn ); + assertEquals( "US", p.bornIn.getIso2() ); + } ); + } + + @Test + @TestForIssue(jiraKey = "HHH-8172") + public void testQueryWithEmbeddedIsNull(SessionFactoryScope scope) { + Person person = new Person(); + Address a = new Address(); + Country c = new Country(); + Country bornCountry = new Country(); + c.setIso2( "DM" ); + c.setName( "Matt Damon Land" ); + assertNull( bornCountry.getIso2() ); + assertNull( bornCountry.getName() ); + + a.address1 = "colorado street"; + a.city = "Springfield"; + a.country = c; + person.address = a; + person.bornIn = bornCountry; + person.name = "Homer"; + scope.inTransaction( session -> session.persist( person ) ); + + scope.inTransaction( session -> { + Person p = (Person) session.createQuery( "from Person p where p.bornIn is null" ).uniqueResult(); + assertNotNull( p ); + assertNotNull( p.address ); + assertEquals( "Springfield", p.address.city ); + assertNotNull( p.address.country ); + assertEquals( "DM", p.address.country.getIso2() ); + assertNull( p.bornIn ); + } ); + } + + @Test + @TestForIssue(jiraKey = "HHH-8172") + @FailureExpected(jiraKey = "HHH-8172") + public void testQueryWithEmbeddedParameterAllNull(SessionFactoryScope scope) { + Person person = new Person(); + Address a = new Address(); + Country c = new Country(); + Country bornCountry = new Country(); + assertNull( bornCountry.getIso2() ); + assertNull( bornCountry.getName() ); + + a.address1 = "colorado street"; + a.city = "Springfield"; + a.country = c; + person.address = a; + person.bornIn = bornCountry; + person.name = "Homer"; + + scope.inTransaction( session -> session.persist( person ) ); + + scope.inTransaction( session -> { + Person p = (Person) session.createQuery( "from Person p where p.bornIn = :b" ) + .setParameter( "b", person.bornIn ) + .uniqueResult(); + assertNotNull( p ); + assertNotNull( p.address ); + assertEquals( "Springfield", p.address.city ); + assertNotNull( p.address.country ); + assertEquals( "DM", p.address.country.getIso2() ); + assertNull( p.bornIn ); + } ); + } + + @Test + @TestForIssue(jiraKey = "HHH-8172") + @SkipForDialect(value = SybaseDialect.class, comment = "skip for Sybase because (null = null) evaluates to true") + @FailureExpected(jiraKey = "HHH-8172") + public void testQueryWithEmbeddedParameterOneNull(SessionFactoryScope scope) { + Person person = new Person(); + Address a = new Address(); + Country c = new Country(); + Country bornCountry = new Country(); + c.setIso2( "DM" ); + c.setName( "Matt Damon Land" ); + bornCountry.setIso2( "US" ); + assertNull( bornCountry.getName() ); + + a.address1 = "colorado street"; + a.city = "Springfield"; + a.country = c; + person.address = a; + person.bornIn = bornCountry; + person.name = "Homer"; + + scope.inTransaction( session -> session.persist( person ) ); + + scope.inTransaction( session -> { + Person p = (Person) session.createQuery( "from Person p where p.bornIn = :b" ) + .setParameter( "b", person.bornIn ) + .uniqueResult(); + assertNotNull( p ); + assertNotNull( p.address ); + assertEquals( "Springfield", p.address.city ); + assertNotNull( p.address.country ); + assertEquals( "DM", p.address.country.getIso2() ); + assertEquals( "US", p.bornIn.getIso2() ); + assertNull( p.bornIn.getName() ); + } ); + } + + @Test + @TestForIssue(jiraKey = "HHH-8172") + public void testQueryWithEmbeddedWithNullUsingSubAttributes(SessionFactoryScope scope) { + Person person = new Person(); + Address a = new Address(); + Country c = new Country(); + Country bornCountry = new Country(); + c.setIso2( "DM" ); + c.setName( "Matt Damon Land" ); + bornCountry.setIso2( "US" ); + assertNull( bornCountry.getName() ); + + a.address1 = "colorado street"; + a.city = "Springfield"; + a.country = c; + person.address = a; + person.bornIn = bornCountry; + person.name = "Homer"; + + scope.inTransaction( session -> session.persist( person ) ); + + scope.inTransaction( session -> { + Query query = session.createQuery( "from Person p " + + "where ( p.bornIn.iso2 is null or p.bornIn.iso2 = :i ) and " + + "( p.bornIn.name is null or p.bornIn.name = :n )" + ); + query.setParameter( "i", person.bornIn.getIso2() ); + query.setParameter( "n", person.bornIn.getName() ); + Person p = (Person) query.uniqueResult(); + assertNotNull( p ); + assertNotNull( p.address ); + assertEquals( "Springfield", p.address.city ); + assertNotNull( p.address.country ); + assertEquals( "DM", p.address.country.getIso2() ); + assertEquals( "US", p.bornIn.getIso2() ); + assertNull( p.bornIn.getName() ); + } ); + } + + @Test + public void testCompositeId(SessionFactoryScope scope) { + + RegionalArticle reg = new RegionalArticle(); + + scope.inTransaction( + session -> { + RegionalArticlePk pk = new RegionalArticlePk(); + pk.iso2 = "FR"; + pk.localUniqueKey = "1234567890123"; + reg.setName( "Je ne veux pes rester sage - Dolly" ); + reg.setPk( pk ); + session.persist( reg ); + } + ); + + scope.inTransaction( + session -> { + RegionalArticle reg1 = session.get( RegionalArticle.class, reg.getPk() ); + assertNotNull( reg1 ); + assertNotNull( reg1.getPk() ); + assertEquals( "Je ne veux pes rester sage - Dolly", reg1.getName() ); + assertEquals( "FR", reg1.getPk().iso2 ); + } + ); + } + + @Test + public void testManyToOneInsideComponent(SessionFactoryScope scope) { + Address add = new Address(); + AddressType type = new AddressType(); + + scope.inTransaction( + session -> { + Person p = new Person(); + Country bornIn = new Country(); + bornIn.setIso2( "FR" ); + bornIn.setName( "France" ); + p.bornIn = bornIn; + p.name = "Emmanuel"; + + type.setName( "Primary Home" ); + session.persist( type ); + Country currentCountry = new Country(); + currentCountry.setIso2( "US" ); + currentCountry.setName( "USA" ); + + add.address1 = "4 square street"; + add.city = "San diego"; + add.country = currentCountry; + add.type = type; + p.address = add; + session.persist( p ); + } + ); + + scope.inTransaction( + session -> { + Query q = session.createQuery( "select p from Person p where p.address.city = :city" ); + q.setParameter( "city", add.city ); + List result = q.list(); + Person samePerson = result.get( 0 ); + assertNotNull( samePerson.address.type ); + assertEquals( type.getName(), samePerson.address.type.getName() ); + } + ); + } + + @Test + public void testEmbeddedSuperclass(SessionFactoryScope scope) { + VanillaSwap swap = new VanillaSwap(); + scope.inTransaction( + session -> { + swap.setInstrumentId( "US345421" ); + swap.setCurrency( VanillaSwap.Currency.EUR ); + FixedLeg fixed = new FixedLeg(); + fixed.setPaymentFrequency( Frequency.SEMIANNUALLY ); + fixed.setRate( 5.6 ); + FloatLeg floating = new FloatLeg(); + floating.setPaymentFrequency( Frequency.QUARTERLY ); + floating.setRateIndex( RateIndex.LIBOR ); + floating.setRateSpread( 1.1 ); + swap.setFixedLeg( fixed ); + swap.setFloatLeg( floating ); + session.persist( swap ); + } + ); + + scope.inTransaction( + session -> { + VanillaSwap vanillaSwap = session.get( VanillaSwap.class, swap.getInstrumentId() ); + // All fields must be filled with non-default values + FixedLeg fixed = vanillaSwap.getFixedLeg(); + assertNotNull( "Fixed leg retrieved as null", fixed ); + FloatLeg floating = vanillaSwap.getFloatLeg(); + assertNotNull( "Floating leg retrieved as null", floating ); + assertEquals( Frequency.SEMIANNUALLY, fixed.getPaymentFrequency() ); + assertEquals( Frequency.QUARTERLY, floating.getPaymentFrequency() ); + session.delete( vanillaSwap ); + } + ); + } + + @Test + public void testDottedProperty(SessionFactoryScope scope) { + SpreadDeal deal = scope.fromTransaction( + session -> { + // Create short swap + Swap shortSwap = new Swap(); + shortSwap.setTenor( 2 ); + FixedLeg shortFixed = new FixedLeg(); + shortFixed.setPaymentFrequency( Frequency.SEMIANNUALLY ); + shortFixed.setRate( 5.6 ); + FloatLeg shortFloating = new FloatLeg(); + shortFloating.setPaymentFrequency( Frequency.QUARTERLY ); + shortFloating.setRateIndex( RateIndex.LIBOR ); + shortFloating.setRateSpread( 1.1 ); + shortSwap.setFixedLeg( shortFixed ); + shortSwap.setFloatLeg( shortFloating ); + // Create medium swap + Swap swap = new Swap(); + swap.setTenor( 7 ); + FixedLeg fixed = new FixedLeg(); + fixed.setPaymentFrequency( Frequency.MONTHLY ); + fixed.setRate( 7.6 ); + FloatLeg floating = new FloatLeg(); + floating.setPaymentFrequency( Frequency.MONTHLY ); + floating.setRateIndex( RateIndex.TIBOR ); + floating.setRateSpread( 0.8 ); + swap.setFixedLeg( fixed ); + swap.setFloatLeg( floating ); + // Create long swap + Swap longSwap = new Swap(); + longSwap.setTenor( 7 ); + FixedLeg longFixed = new FixedLeg(); + longFixed.setPaymentFrequency( Frequency.MONTHLY ); + longFixed.setRate( 7.6 ); + FloatLeg longFloating = new FloatLeg(); + longFloating.setPaymentFrequency( Frequency.MONTHLY ); + longFloating.setRateIndex( RateIndex.TIBOR ); + longFloating.setRateSpread( 0.8 ); + longSwap.setFixedLeg( longFixed ); + longSwap.setFloatLeg( longFloating ); + // Compose a curve spread deal + SpreadDeal spreadDeal = new SpreadDeal(); + spreadDeal.setId( "FX45632" ); + spreadDeal.setNotional( 450000.0 ); + spreadDeal.setShortSwap( shortSwap ); + spreadDeal.setSwap( swap ); + spreadDeal.setLongSwap( longSwap ); + session.persist( spreadDeal ); + return spreadDeal; + } + ); + + scope.inTransaction( + session -> { + SpreadDeal spreadDeal = session.get( SpreadDeal.class, deal.getId() ); + // All fields must be filled with non-default values + assertNotNull( "Short swap is null.", spreadDeal.getShortSwap() ); + assertNotNull( "Swap is null.", spreadDeal.getSwap() ); + assertNotNull( "Long swap is null.", spreadDeal.getLongSwap() ); + assertEquals( 2, spreadDeal.getShortSwap().getTenor() ); + assertEquals( 7, spreadDeal.getSwap().getTenor() ); + assertEquals( 7, spreadDeal.getLongSwap().getTenor() ); + assertNotNull( "Short fixed leg is null.", spreadDeal.getShortSwap().getFixedLeg() ); + assertNotNull( "Short floating leg is null.", spreadDeal.getShortSwap().getFloatLeg() ); + assertNotNull( "Fixed leg is null.", spreadDeal.getSwap().getFixedLeg() ); + assertNotNull( "Floating leg is null.", spreadDeal.getSwap().getFloatLeg() ); + assertNotNull( "Long fixed leg is null.", spreadDeal.getLongSwap().getFixedLeg() ); + assertNotNull( "Long floating leg is null.", spreadDeal.getLongSwap().getFloatLeg() ); + assertEquals( + Frequency.SEMIANNUALLY, + spreadDeal.getShortSwap().getFixedLeg().getPaymentFrequency() + ); + assertEquals( Frequency.QUARTERLY, spreadDeal.getShortSwap().getFloatLeg().getPaymentFrequency() ); + assertEquals( Frequency.MONTHLY, spreadDeal.getSwap().getFixedLeg().getPaymentFrequency() ); + assertEquals( Frequency.MONTHLY, spreadDeal.getSwap().getFloatLeg().getPaymentFrequency() ); + assertEquals( Frequency.MONTHLY, spreadDeal.getLongSwap().getFixedLeg().getPaymentFrequency() ); + assertEquals( Frequency.MONTHLY, spreadDeal.getLongSwap().getFloatLeg().getPaymentFrequency() ); + assertEquals( 5.6, spreadDeal.getShortSwap().getFixedLeg().getRate(), 0.01 ); + assertEquals( 7.6, spreadDeal.getSwap().getFixedLeg().getRate(), 0.01 ); + assertEquals( 7.6, spreadDeal.getLongSwap().getFixedLeg().getRate(), 0.01 ); + assertEquals( RateIndex.LIBOR, spreadDeal.getShortSwap().getFloatLeg().getRateIndex() ); + assertEquals( RateIndex.TIBOR, spreadDeal.getSwap().getFloatLeg().getRateIndex() ); + assertEquals( RateIndex.TIBOR, spreadDeal.getLongSwap().getFloatLeg().getRateIndex() ); + assertEquals( 1.1, spreadDeal.getShortSwap().getFloatLeg().getRateSpread(), 0.01 ); + assertEquals( 0.8, spreadDeal.getSwap().getFloatLeg().getRateSpread(), 0.01 ); + assertEquals( 0.8, spreadDeal.getLongSwap().getFloatLeg().getRateSpread(), 0.01 ); + session.delete( spreadDeal ); + } + ); + } + + @Test + public void testEmbeddedInSecondaryTable(SessionFactoryScope scope) { + scope.inSession( + session -> { + session.getTransaction().begin(); + try { + Book book = new Book(); + book.setIsbn( "1234" ); + book.setName( "HiA Second Edition" ); + Summary summary = new Summary(); + summary.setText( "This is a HiA SE summary" ); + summary.setSize( summary.getText().length() ); + book.setSummary( summary ); + session.persist( book ); + session.getTransaction().commit(); + + session.clear(); + + Transaction tx = session.beginTransaction(); + Book loadedBook = session.get( Book.class, book.getIsbn() ); + assertNotNull( loadedBook.getSummary() ); + assertEquals( book.getSummary().getText(), loadedBook.getSummary().getText() ); + session.delete( loadedBook ); + tx.commit(); + } + catch (Exception e) { + if ( session.getTransaction().isActive() ) { + session.getTransaction().rollback(); + } + throw e; + } + } + ); + } + + @Test + public void testParent(SessionFactoryScope scope) { + scope.inSession( + session -> { + session.getTransaction().begin(); + try { + Book book = new Book(); + book.setIsbn( "1234" ); + book.setName( "HiA Second Edition" ); + Summary summary = new Summary(); + summary.setText( "This is a HiA SE summary" ); + summary.setSize( summary.getText().length() ); + book.setSummary( summary ); + session.persist( book ); + session.getTransaction().commit(); + + session.clear(); + + Transaction tx = session.beginTransaction(); + Book loadedBook = session.get( Book.class, book.getIsbn() ); + assertNotNull( loadedBook.getSummary() ); + assertEquals( loadedBook, loadedBook.getSummary().getSummarizedBook() ); + session.delete( loadedBook ); + tx.commit(); + } + catch (Exception e) { + if ( session.getTransaction().isActive() ) { + session.getTransaction().rollback(); + } + throw e; + } + } + ); + } + + @Test + public void testEmbeddedAndMultipleManyToOne(SessionFactoryScope scope) { + CorpType type = new CorpType(); + InternetProvider provider = new InternetProvider(); + Nationality nat = new Nationality(); + + scope.inTransaction( + session -> { + type.setType( "National" ); + session.persist( type ); + nat.setName( "Canadian" ); + session.persist( nat ); + provider.setBrandName( "Fido" ); + LegalStructure structure = new LegalStructure(); + structure.setCorporationType( type ); + structure.setCountry( "Canada" ); + structure.setName( "Rogers" ); + provider.setOwner( structure ); + structure.setOrigin( nat ); + session.persist( provider ); + } + ); + + scope.inTransaction( + session -> { + InternetProvider internetProvider = session.get( InternetProvider.class, provider.getId() ); + assertNotNull( internetProvider.getOwner() ); + assertNotNull( "Many to one not set", internetProvider.getOwner().getCorporationType() ); + assertEquals( + "Wrong link", + type.getType(), + internetProvider.getOwner().getCorporationType().getType() + ); + assertNotNull( "2nd Many to one not set", internetProvider.getOwner().getOrigin() ); + assertEquals( "Wrong 2nd link", nat.getName(), internetProvider.getOwner().getOrigin().getName() ); + session.delete( internetProvider ); + session.delete( internetProvider.getOwner().getCorporationType() ); + session.delete( internetProvider.getOwner().getOrigin() ); + } + ); + } + + @Test + public void testEmbeddedAndOneToMany(SessionFactoryScope scope) { + InternetProvider provider = new InternetProvider(); + scope.inTransaction( + session -> { + provider.setBrandName( "Fido" ); + LegalStructure structure = new LegalStructure(); + structure.setCountry( "Canada" ); + structure.setName( "Rogers" ); + provider.setOwner( structure ); + session.persist( provider ); + Manager manager = new Manager(); + manager.setName( "Bill" ); + manager.setEmployer( provider ); + structure.getTopManagement().add( manager ); + session.persist( manager ); + } + ); + + scope.inTransaction( + session -> { + InternetProvider internetProvider = session.get( InternetProvider.class, provider.getId() ); + assertNotNull( internetProvider.getOwner() ); + Set topManagement = internetProvider.getOwner().getTopManagement(); + assertNotNull( "OneToMany not set", topManagement ); + assertEquals( "Wrong number of elements", 1, topManagement.size() ); + Manager manager = topManagement.iterator().next(); + assertEquals( "Wrong element", "Bill", manager.getName() ); + session.delete( manager ); + session.delete( provider ); + } + ); + } + + @Test + @TestForIssue(jiraKey = "HHH-9642") + public void testEmbeddedAndOneToManyHql(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + InternetProvider provider = new InternetProvider(); + provider.setBrandName( "Fido" ); + LegalStructure structure = new LegalStructure(); + structure.setCountry( "Canada" ); + structure.setName( "Rogers" ); + provider.setOwner( structure ); + session.persist( provider ); + Manager manager = new Manager(); + manager.setName( "Bill" ); + manager.setEmployer( provider ); + structure.getTopManagement().add( manager ); + session.persist( manager ); + } + ); + + scope.inTransaction( + session -> { + InternetProvider internetProviderQueried = + (InternetProvider) session.createQuery( "from InternetProvider" ).uniqueResult(); + assertFalse( Hibernate.isInitialized( internetProviderQueried.getOwner().getTopManagement() ) ); + + } + ); + + scope.inTransaction( + session -> { + InternetProvider internetProviderQueried = + (InternetProvider) session.createQuery( + "from InternetProvider i join fetch i.owner.topManagement" ) + .uniqueResult(); + assertTrue( Hibernate.isInitialized( internetProviderQueried.getOwner().getTopManagement() ) ); + + } + ); + + InternetProvider provider = scope.fromTransaction( + session -> { + InternetProvider internetProviderQueried = + (InternetProvider) session.createQuery( + "from InternetProvider i join fetch i.owner o join fetch o.topManagement" ) + .uniqueResult(); + assertTrue( Hibernate.isInitialized( internetProviderQueried.getOwner().getTopManagement() ) ); + return internetProviderQueried; + } + ); + + scope.inTransaction( + session -> { + InternetProvider internetProvider = session.get( InternetProvider.class, provider.getId() ); + Manager manager = internetProvider.getOwner().getTopManagement().iterator().next(); + session.delete( manager ); + session.delete( internetProvider ); + } + ); + } + + + @Test + public void testDefaultCollectionTable(SessionFactoryScope scope) { + //are the tables correct? + MetadataImplementor metadata = scope.getMetadataImplementor(); + assertTrue( SchemaUtil.isTablePresent( "WealthyPerson_vacationHomes", metadata ) ); + assertTrue( SchemaUtil.isTablePresent( "WelPers_LegacyVacHomes", metadata ) ); + assertTrue( SchemaUtil.isTablePresent( "WelPers_VacHomes", metadata ) ); + + //just to make sure, use the mapping + WealthyPerson p = new WealthyPerson(); + Address a = new Address(); + Address vacation = new Address(); + Country c = new Country(); + Country bornCountry = new Country(); + c.setIso2( "DM" ); + c.setName( "Matt Damon Land" ); + bornCountry.setIso2( "US" ); + bornCountry.setName( "United States of America" ); + + a.address1 = "colorado street"; + a.city = "Springfield"; + a.country = c; + vacation.address1 = "rock street"; + vacation.city = "Plymouth"; + vacation.country = c; + p.vacationHomes.add( vacation ); + p.address = a; + p.bornIn = bornCountry; + p.name = "Homer"; + scope.inTransaction( + session -> session.persist( p ) + ); + + scope.inTransaction( + session -> { + WealthyPerson wealthyPerson = session.get( WealthyPerson.class, p.id ); + assertNotNull( wealthyPerson ); + assertNotNull( wealthyPerson.address ); + assertEquals( "Springfield", wealthyPerson.address.city ); + assertNotNull( wealthyPerson.address.country ); + assertEquals( "DM", wealthyPerson.address.country.getIso2() ); + assertNotNull( wealthyPerson.bornIn ); + assertEquals( "US", wealthyPerson.bornIn.getIso2() ); + } + ); + } + + // make sure we support collection of embeddable objects inside embeddable objects + @Test + public void testEmbeddableInsideEmbeddable(SessionFactoryScope scope) { + scope.inSession( + session -> { + Collection urls = new ArrayList<>(); + URLFavorite urlFavorite = new URLFavorite(); + urlFavorite.setUrl( "http://highscalability.com/" ); + urls.add( urlFavorite ); + + urlFavorite = new URLFavorite(); + urlFavorite.setUrl( "http://www.jboss.org/" ); + urls.add( urlFavorite ); + + urlFavorite = new URLFavorite(); + urlFavorite.setUrl( "http://www.hibernate.org/" ); + urls.add( urlFavorite ); + + urlFavorite = new URLFavorite(); + urlFavorite.setUrl( "http://www.jgroups.org/" ); + urls.add( urlFavorite ); + + Collection ideas = new ArrayList<>(); + ideas.add( "lionheart" ); + ideas.add( "xforms" ); + ideas.add( "dynamic content" ); + ideas.add( "http" ); + + InternetFavorites internetFavorites = new InternetFavorites(); + internetFavorites.setLinks( urls ); + internetFavorites.setIdeas( ideas ); + + FavoriteThings favoriteThings = new FavoriteThings(); + favoriteThings.setWeb( internetFavorites ); + + Transaction tx = session.beginTransaction(); + try { + session.persist( favoriteThings ); + tx.commit(); + + tx = session.beginTransaction(); + session.flush(); + + favoriteThings = session.get( FavoriteThings.class, favoriteThings.getId() ); + assertTrue( "has web", favoriteThings.getWeb() != null ); + assertTrue( "has ideas", favoriteThings.getWeb().getIdeas() != null ); + assertTrue( "has favorite idea 'http'", favoriteThings.getWeb().getIdeas().contains( "http" ) ); + assertTrue( + "has favorite idea 'http'", + favoriteThings.getWeb().getIdeas().contains( "dynamic content" ) + ); + + urls = favoriteThings.getWeb().getLinks(); + assertTrue( "has urls", urls != null ); + URLFavorite[] favs = new URLFavorite[4]; + urls.toArray( favs ); + assertTrue( + "has http://www.hibernate.org url favorite link", + "http://www.hibernate.org/".equals( favs[0].getUrl() ) || + "http://www.hibernate.org/".equals( favs[1].getUrl() ) || + "http://www.hibernate.org/".equals( favs[2].getUrl() ) || + "http://www.hibernate.org/".equals( favs[3].getUrl() ) + ); + tx.commit(); + } + catch (Exception e) { + if ( session.getTransaction().isActive() ) { + session.getTransaction().rollback(); + } + throw e; + } + } + ); + } + + @Test + @TestForIssue(jiraKey = "HHH-3868") + public void testTransientMergeComponentParent(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Book b = new Book(); + b.setIsbn( UUID.randomUUID().toString() ); + b.setSummary( new Summary() ); + b = (Book) session.merge( b ); + } + ); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/EntityWithNestedEmbeddables.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/EntityWithNestedEmbeddables.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/EntityWithNestedEmbeddables.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/EntityWithNestedEmbeddables.java index c4a10eb0dd..8690e64504 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/EntityWithNestedEmbeddables.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/EntityWithNestedEmbeddables.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import javax.persistence.Embedded; import javax.persistence.Entity; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/FavoriteThings.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/FavoriteThings.java similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/FavoriteThings.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/FavoriteThings.java index f7af0d936b..deac36951f 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/FavoriteThings.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/FavoriteThings.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.Id; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/FixedLeg.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/FixedLeg.java similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/FixedLeg.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/FixedLeg.java index e5352e55f6..dda166416d 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/FixedLeg.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/FixedLeg.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import java.text.NumberFormat; import javax.persistence.Embeddable; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/FloatLeg.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/FloatLeg.java similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/FloatLeg.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/FloatLeg.java index 5890b506e7..12a05ed68b 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/FloatLeg.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/FloatLeg.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import java.text.NumberFormat; import javax.persistence.Embeddable; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/InternetFavorites.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/InternetFavorites.java similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/InternetFavorites.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/InternetFavorites.java index a0e6f82d13..177fe45f27 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/InternetFavorites.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/InternetFavorites.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import java.util.Collection; import javax.persistence.Embeddable; import javax.persistence.Embedded; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/InternetProvider.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/InternetProvider.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/InternetProvider.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/InternetProvider.java index 28d8512a4d..b6bcc3c197 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/InternetProvider.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/InternetProvider.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Leg.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Leg.java similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Leg.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Leg.java index af87e66c3c..af4e3f100e 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Leg.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Leg.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import javax.persistence.MappedSuperclass; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/LegalStructure.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/LegalStructure.java similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/LegalStructure.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/LegalStructure.java index 066cb95651..c2f9dbad6b 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/LegalStructure.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/LegalStructure.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import java.util.HashSet; import java.util.Set; import javax.persistence.Embeddable; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Manager.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Manager.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Manager.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Manager.java index 1285fd839c..df601a3255 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Manager.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Manager.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Nationality.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Nationality.java similarity index 92% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Nationality.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Nationality.java index e972c26a41..bd4db6eb5d 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Nationality.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Nationality.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/NestedEmbeddableAttributeOverrideTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/NestedEmbeddableAttributeOverrideTest.java similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/NestedEmbeddableAttributeOverrideTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/NestedEmbeddableAttributeOverrideTest.java index de5a130c4f..81fff226d1 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/NestedEmbeddableAttributeOverrideTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/NestedEmbeddableAttributeOverrideTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import org.hibernate.Session; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/NotonialDeal.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/NotonialDeal.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/NotonialDeal.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/NotonialDeal.java index 339f85f772..bcdfe42451 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/NotonialDeal.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/NotonialDeal.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Person.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Person.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Person.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Person.java index 4d4317d5a6..3432c78af3 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Person.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Person.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import java.io.Serializable; import javax.persistence.AttributeOverride; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/RegionalArticle.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/RegionalArticle.java similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/RegionalArticle.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/RegionalArticle.java index 1295864d43..d7a9f88660 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/RegionalArticle.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/RegionalArticle.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.Id; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/RegionalArticlePk.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/RegionalArticlePk.java similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/RegionalArticlePk.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/RegionalArticlePk.java index f47e5ff1f9..5d8a8c1caa 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/RegionalArticlePk.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/RegionalArticlePk.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import java.io.Serializable; import javax.persistence.Embeddable; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/SpreadDeal.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/SpreadDeal.java similarity index 97% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/SpreadDeal.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/SpreadDeal.java index 1e8b72b72f..dfbd0dd9dd 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/SpreadDeal.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/SpreadDeal.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Summary.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Summary.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Summary.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Summary.java index 2f03da0fd2..b1d340a59d 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Summary.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Summary.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import javax.persistence.Column; import javax.persistence.Embeddable; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Swap.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Swap.java similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Swap.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Swap.java index 57bb98eb94..67e1d535ad 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/Swap.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/Swap.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import javax.persistence.AttributeOverride; import javax.persistence.Column; import javax.persistence.Embeddable; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/URLFavorite.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/URLFavorite.java similarity index 89% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/URLFavorite.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/URLFavorite.java index 2ad8188e59..01ff8e41c6 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/URLFavorite.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/URLFavorite.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import javax.persistence.Embeddable; @Embeddable diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/VanillaSwap.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/VanillaSwap.java similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/VanillaSwap.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/VanillaSwap.java index 74180127a3..594f74da12 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/VanillaSwap.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/VanillaSwap.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import javax.persistence.AttributeOverride; import javax.persistence.Column; import javax.persistence.Embedded; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/WealthyPerson.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/WealthyPerson.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/WealthyPerson.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/WealthyPerson.java index 05df8f1df6..9cf4016578 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/WealthyPerson.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embedded/WealthyPerson.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.annotations.embedded; +package org.hibernate.orm.test.annotations.embedded; import java.util.HashSet; import java.util.Set; import javax.persistence.CollectionTable; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embeddables/attributeOverrides/AttributeOverrideEnhancedUserTypeTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/embeddables/attributeoverrides/AttributeOverrideEnhancedUserTypeTest.java similarity index 98% rename from hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embeddables/attributeOverrides/AttributeOverrideEnhancedUserTypeTest.java rename to hibernate-core/src/test/java/org/hibernate/test/annotations/embeddables/attributeoverrides/AttributeOverrideEnhancedUserTypeTest.java index 9db36f1950..398e9d3827 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embeddables/attributeOverrides/AttributeOverrideEnhancedUserTypeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/embeddables/attributeoverrides/AttributeOverrideEnhancedUserTypeTest.java @@ -1,4 +1,4 @@ -package org.hibernate.orm.test.annotations.embeddables.attributeOverrides; +package org.hibernate.test.annotations.embeddables.attributeoverrides; import java.io.Serializable; import java.sql.PreparedStatement; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/EmbeddedTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/EmbeddedTest.java deleted file mode 100644 index a4242e90b0..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/EmbeddedTest.java +++ /dev/null @@ -1,765 +0,0 @@ -/* - * 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 . - */ -package org.hibernate.test.annotations.embedded; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Set; -import java.util.UUID; - -import org.hibernate.Hibernate; -import org.hibernate.query.Query; -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.boot.MetadataBuilder; -import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl; -import org.hibernate.dialect.SybaseDialect; - -import org.hibernate.testing.FailureExpected; -import org.hibernate.testing.SkipForDialect; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.hibernate.testing.transaction.TransactionUtil; -import org.hibernate.test.annotations.embedded.FloatLeg.RateIndex; -import org.hibernate.test.annotations.embedded.Leg.Frequency; -import org.hibernate.test.util.SchemaUtil; - -import org.junit.After; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -/** - * @author Emmanuel Bernard - */ -public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase { - - @After - public void cleanup() { - TransactionUtil.doInHibernate( this::sessionFactory, session ->{ - for ( Person person : session.createQuery( "from Person", Person.class ).getResultList() ) { - session.delete( person ); - } - } ); - } - - @Test - public void testSimple() throws Exception { - Person person = new Person(); - Address a = new Address(); - Country c = new Country(); - Country bornCountry = new Country(); - c.setIso2( "DM" ); - c.setName( "Matt Damon Land" ); - bornCountry.setIso2( "US" ); - bornCountry.setName( "United States of America" ); - - a.address1 = "colorado street"; - a.city = "Springfield"; - a.country = c; - person.address = a; - person.bornIn = bornCountry; - person.name = "Homer"; - - TransactionUtil.doInHibernate( this::sessionFactory, session ->{ - session.persist( person ); - } ); - - TransactionUtil.doInHibernate( this::sessionFactory, session ->{ - Person p = session.get( Person.class, person.id ); - assertNotNull( p ); - assertNotNull( p.address ); - assertEquals( "Springfield", p.address.city ); - assertNotNull( p.address.country ); - assertEquals( "DM", p.address.country.getIso2() ); - assertNotNull( p.bornIn ); - assertEquals( "US", p.bornIn.getIso2() ); - } ); - } - - @Test - @TestForIssue(jiraKey = "HHH-8172") - public void testQueryWithEmbeddedIsNull() throws Exception { - Person person = new Person(); - Address a = new Address(); - Country c = new Country(); - Country bornCountry = new Country(); - c.setIso2( "DM" ); - c.setName( "Matt Damon Land" ); - assertNull( bornCountry.getIso2() ); - assertNull( bornCountry.getName() ); - - a.address1 = "colorado street"; - a.city = "Springfield"; - a.country = c; - person.address = a; - person.bornIn = bornCountry; - person.name = "Homer"; - TransactionUtil.doInHibernate( this::sessionFactory, session -> { - session.persist( person ); - } ); - - TransactionUtil.doInHibernate( this::sessionFactory, session -> { - Person p = (Person) session.createQuery( "from Person p where p.bornIn is null" ).uniqueResult(); - assertNotNull( p ); - assertNotNull( p.address ); - assertEquals( "Springfield", p.address.city ); - assertNotNull( p.address.country ); - assertEquals( "DM", p.address.country.getIso2() ); - assertNull( p.bornIn ); - } ); - } - - @Test - @TestForIssue(jiraKey = "HHH-8172") - @FailureExpected(jiraKey = "HHH-8172") - public void testQueryWithEmbeddedParameterAllNull() throws Exception { - Session s; - Transaction tx; - Person person = new Person(); - Address a = new Address(); - Country c = new Country(); - Country bornCountry = new Country(); - assertNull( bornCountry.getIso2() ); - assertNull( bornCountry.getName() ); - - a.address1 = "colorado street"; - a.city = "Springfield"; - a.country = c; - person.address = a; - person.bornIn = bornCountry; - person.name = "Homer"; - - TransactionUtil.doInHibernate( this::sessionFactory, session -> { - session.persist( person ); - } ); - - TransactionUtil.doInHibernate( this::sessionFactory, session -> { - Person p = (Person) session.createQuery( "from Person p where p.bornIn = :b" ) - .setParameter( "b", person.bornIn ) - .uniqueResult(); - assertNotNull( p ); - assertNotNull( p.address ); - assertEquals( "Springfield", p.address.city ); - assertNotNull( p.address.country ); - assertEquals( "DM", p.address.country.getIso2() ); - assertNull( p.bornIn ); - } ); - } - - @Test - @TestForIssue(jiraKey = "HHH-8172") - @SkipForDialect( value = SybaseDialect.class, comment = "skip for Sybase because (null = null) evaluates to true") - @FailureExpected(jiraKey = "HHH-8172") - public void testQueryWithEmbeddedParameterOneNull() throws Exception { - Person person = new Person(); - Address a = new Address(); - Country c = new Country(); - Country bornCountry = new Country(); - c.setIso2( "DM" ); - c.setName( "Matt Damon Land" ); - bornCountry.setIso2( "US" ); - assertNull( bornCountry.getName() ); - - a.address1 = "colorado street"; - a.city = "Springfield"; - a.country = c; - person.address = a; - person.bornIn = bornCountry; - person.name = "Homer"; - - TransactionUtil.doInHibernate( this::sessionFactory, session -> { - session.persist( person ); - } ); - - TransactionUtil.doInHibernate( this::sessionFactory, session -> { - Person p = (Person) session.createQuery( "from Person p where p.bornIn = :b" ) - .setParameter( "b", person.bornIn ) - .uniqueResult(); - assertNotNull( p ); - assertNotNull( p.address ); - assertEquals( "Springfield", p.address.city ); - assertNotNull( p.address.country ); - assertEquals( "DM", p.address.country.getIso2() ); - assertEquals( "US", p.bornIn.getIso2() ); - assertNull( p.bornIn.getName() ); - } ); - } - - @Test - @TestForIssue(jiraKey = "HHH-8172") - public void testQueryWithEmbeddedWithNullUsingSubAttributes() throws Exception { - Person person = new Person(); - Address a = new Address(); - Country c = new Country(); - Country bornCountry = new Country(); - c.setIso2( "DM" ); - c.setName( "Matt Damon Land" ); - bornCountry.setIso2( "US" ); - assertNull( bornCountry.getName() ); - - a.address1 = "colorado street"; - a.city = "Springfield"; - a.country = c; - person.address = a; - person.bornIn = bornCountry; - person.name = "Homer"; - TransactionUtil.doInHibernate( this::sessionFactory, session -> { - session.persist( person ); - } ); - - TransactionUtil.doInHibernate( this::sessionFactory, session -> { - Person p = (Person) session.createQuery( "from Person p " + - "where ( p.bornIn.iso2 is null or p.bornIn.iso2 = :i ) and " + - "( p.bornIn.name is null or p.bornIn.name = :n )" - ).setParameter( "i", person.bornIn.getIso2() ) - .setParameter( "n", person.bornIn.getName() ) - .uniqueResult(); - assertNotNull( p ); - assertNotNull( p.address ); - assertEquals( "Springfield", p.address.city ); - assertNotNull( p.address.country ); - assertEquals( "DM", p.address.country.getIso2() ); - assertEquals( "US", p.bornIn.getIso2() ); - assertNull( p.bornIn.getName() ); - } ); - } - - @Test - public void testCompositeId() throws Exception { - Session s; - Transaction tx; - RegionalArticlePk pk = new RegionalArticlePk(); - pk.iso2 = "FR"; - pk.localUniqueKey = "1234567890123"; - RegionalArticle reg = new RegionalArticle(); - reg.setName( "Je ne veux pes rester sage - Dolly" ); - reg.setPk( pk ); - s = openSession(); - tx = s.beginTransaction(); - s.persist( reg ); - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - reg = (RegionalArticle) s.get( RegionalArticle.class, reg.getPk() ); - assertNotNull( reg ); - assertNotNull( reg.getPk() ); - assertEquals( "Je ne veux pes rester sage - Dolly", reg.getName() ); - assertEquals( "FR", reg.getPk().iso2 ); - tx.commit(); - s.close(); - } - - @Test - public void testManyToOneInsideComponent() throws Exception { - Session s; - Transaction tx; - s = openSession(); - tx = s.beginTransaction(); - Person p = new Person(); - Country bornIn = new Country(); - bornIn.setIso2( "FR" ); - bornIn.setName( "France" ); - p.bornIn = bornIn; - p.name = "Emmanuel"; - AddressType type = new AddressType(); - type.setName( "Primary Home" ); - s.persist( type ); - Country currentCountry = new Country(); - currentCountry.setIso2( "US" ); - currentCountry.setName( "USA" ); - Address add = new Address(); - add.address1 = "4 square street"; - add.city = "San diego"; - add.country = currentCountry; - add.type = type; - p.address = add; - s.persist( p ); - tx.commit(); - - s = openSession(); - tx = s.beginTransaction(); - Query q = s.createQuery( "select p from Person p where p.address.city = :city" ); - q.setParameter( "city", add.city ); - List result = q.list(); - Person samePerson = (Person) result.get( 0 ); - assertNotNull( samePerson.address.type ); - assertEquals( type.getName(), samePerson.address.type.getName() ); - tx.commit(); - s.close(); - } - - @Test - public void testEmbeddedSuperclass() { - Session s; - Transaction tx; - s = openSession(); - tx = s.beginTransaction(); - VanillaSwap swap = new VanillaSwap(); - swap.setInstrumentId( "US345421" ); - swap.setCurrency( VanillaSwap.Currency.EUR ); - FixedLeg fixed = new FixedLeg(); - fixed.setPaymentFrequency( Leg.Frequency.SEMIANNUALLY ); - fixed.setRate( 5.6 ); - FloatLeg floating = new FloatLeg(); - floating.setPaymentFrequency( Leg.Frequency.QUARTERLY ); - floating.setRateIndex( FloatLeg.RateIndex.LIBOR ); - floating.setRateSpread( 1.1 ); - swap.setFixedLeg( fixed ); - swap.setFloatLeg( floating ); - s.persist( swap ); - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - swap = (VanillaSwap) s.get( VanillaSwap.class, swap.getInstrumentId() ); - // All fields must be filled with non-default values - fixed = swap.getFixedLeg(); - assertNotNull( "Fixed leg retrieved as null", fixed ); - floating = swap.getFloatLeg(); - assertNotNull( "Floating leg retrieved as null", floating ); - assertEquals( Leg.Frequency.SEMIANNUALLY, fixed.getPaymentFrequency() ); - assertEquals( Leg.Frequency.QUARTERLY, floating.getPaymentFrequency() ); - s.delete( swap ); - tx.commit(); - s.close(); - } - - @Test - public void testDottedProperty() { - Session s; - Transaction tx; - s = openSession(); - tx = s.beginTransaction(); - // Create short swap - Swap shortSwap = new Swap(); - shortSwap.setTenor( 2 ); - FixedLeg shortFixed = new FixedLeg(); - shortFixed.setPaymentFrequency( Frequency.SEMIANNUALLY ); - shortFixed.setRate( 5.6 ); - FloatLeg shortFloating = new FloatLeg(); - shortFloating.setPaymentFrequency( Frequency.QUARTERLY ); - shortFloating.setRateIndex( RateIndex.LIBOR ); - shortFloating.setRateSpread( 1.1 ); - shortSwap.setFixedLeg( shortFixed ); - shortSwap.setFloatLeg( shortFloating ); - // Create medium swap - Swap swap = new Swap(); - swap.setTenor( 7 ); - FixedLeg fixed = new FixedLeg(); - fixed.setPaymentFrequency( Frequency.MONTHLY ); - fixed.setRate( 7.6 ); - FloatLeg floating = new FloatLeg(); - floating.setPaymentFrequency( Frequency.MONTHLY ); - floating.setRateIndex( RateIndex.TIBOR ); - floating.setRateSpread( 0.8 ); - swap.setFixedLeg( fixed ); - swap.setFloatLeg( floating ); - // Create long swap - Swap longSwap = new Swap(); - longSwap.setTenor( 7 ); - FixedLeg longFixed = new FixedLeg(); - longFixed.setPaymentFrequency( Frequency.MONTHLY ); - longFixed.setRate( 7.6 ); - FloatLeg longFloating = new FloatLeg(); - longFloating.setPaymentFrequency( Frequency.MONTHLY ); - longFloating.setRateIndex( RateIndex.TIBOR ); - longFloating.setRateSpread( 0.8 ); - longSwap.setFixedLeg( longFixed ); - longSwap.setFloatLeg( longFloating ); - // Compose a curve spread deal - SpreadDeal deal = new SpreadDeal(); - deal.setId( "FX45632" ); - deal.setNotional( 450000.0 ); - deal.setShortSwap( shortSwap ); - deal.setSwap( swap ); - deal.setLongSwap( longSwap ); - s.persist( deal ); - - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - deal = (SpreadDeal) s.get( SpreadDeal.class, deal.getId() ); - // All fields must be filled with non-default values - assertNotNull( "Short swap is null.", deal.getShortSwap() ); - assertNotNull( "Swap is null.", deal.getSwap() ); - assertNotNull( "Long swap is null.", deal.getLongSwap() ); - assertEquals( 2, deal.getShortSwap().getTenor() ); - assertEquals( 7, deal.getSwap().getTenor() ); - assertEquals( 7, deal.getLongSwap().getTenor() ); - assertNotNull( "Short fixed leg is null.", deal.getShortSwap().getFixedLeg() ); - assertNotNull( "Short floating leg is null.", deal.getShortSwap().getFloatLeg() ); - assertNotNull( "Fixed leg is null.", deal.getSwap().getFixedLeg() ); - assertNotNull( "Floating leg is null.", deal.getSwap().getFloatLeg() ); - assertNotNull( "Long fixed leg is null.", deal.getLongSwap().getFixedLeg() ); - assertNotNull( "Long floating leg is null.", deal.getLongSwap().getFloatLeg() ); - assertEquals( Frequency.SEMIANNUALLY, deal.getShortSwap().getFixedLeg().getPaymentFrequency() ); - assertEquals( Frequency.QUARTERLY, deal.getShortSwap().getFloatLeg().getPaymentFrequency() ); - assertEquals( Frequency.MONTHLY, deal.getSwap().getFixedLeg().getPaymentFrequency() ); - assertEquals( Frequency.MONTHLY, deal.getSwap().getFloatLeg().getPaymentFrequency() ); - assertEquals( Frequency.MONTHLY, deal.getLongSwap().getFixedLeg().getPaymentFrequency() ); - assertEquals( Frequency.MONTHLY, deal.getLongSwap().getFloatLeg().getPaymentFrequency() ); - assertEquals( 5.6, deal.getShortSwap().getFixedLeg().getRate(), 0.01 ); - assertEquals( 7.6, deal.getSwap().getFixedLeg().getRate(), 0.01 ); - assertEquals( 7.6, deal.getLongSwap().getFixedLeg().getRate(), 0.01 ); - assertEquals( RateIndex.LIBOR, deal.getShortSwap().getFloatLeg().getRateIndex() ); - assertEquals( RateIndex.TIBOR, deal.getSwap().getFloatLeg().getRateIndex() ); - assertEquals( RateIndex.TIBOR, deal.getLongSwap().getFloatLeg().getRateIndex() ); - assertEquals( 1.1, deal.getShortSwap().getFloatLeg().getRateSpread(), 0.01 ); - assertEquals( 0.8, deal.getSwap().getFloatLeg().getRateSpread(), 0.01 ); - assertEquals( 0.8, deal.getLongSwap().getFloatLeg().getRateSpread(), 0.01 ); - s.delete( deal ); - tx.commit(); - s.close(); - } - - @Test - public void testEmbeddedInSecondaryTable() throws Exception { - Session s; - s = openSession(); - s.getTransaction().begin(); - Book book = new Book(); - book.setIsbn( "1234" ); - book.setName( "HiA Second Edition" ); - Summary summary = new Summary(); - summary.setText( "This is a HiA SE summary" ); - summary.setSize( summary.getText().length() ); - book.setSummary( summary ); - s.persist( book ); - s.getTransaction().commit(); - - s.clear(); - - Transaction tx = s.beginTransaction(); - Book loadedBook = (Book) s.get( Book.class, book.getIsbn() ); - assertNotNull( loadedBook.getSummary() ); - assertEquals( book.getSummary().getText(), loadedBook.getSummary().getText() ); - s.delete( loadedBook ); - tx.commit(); - s.close(); - } - - @Test - public void testParent() throws Exception { - Session s; - s = openSession(); - s.getTransaction().begin(); - Book book = new Book(); - book.setIsbn( "1234" ); - book.setName( "HiA Second Edition" ); - Summary summary = new Summary(); - summary.setText( "This is a HiA SE summary" ); - summary.setSize( summary.getText().length() ); - book.setSummary( summary ); - s.persist( book ); - s.getTransaction().commit(); - - s.clear(); - - Transaction tx = s.beginTransaction(); - Book loadedBook = (Book) s.get( Book.class, book.getIsbn() ); - assertNotNull( loadedBook.getSummary() ); - assertEquals( loadedBook, loadedBook.getSummary().getSummarizedBook() ); - s.delete( loadedBook ); - tx.commit(); - s.close(); - } - - @Test - public void testEmbeddedAndMultipleManyToOne() throws Exception { - Session s; - s = openSession(); - Transaction tx = s.beginTransaction(); - CorpType type = new CorpType(); - type.setType( "National" ); - s.persist( type ); - Nationality nat = new Nationality(); - nat.setName( "Canadian" ); - s.persist( nat ); - InternetProvider provider = new InternetProvider(); - provider.setBrandName( "Fido" ); - LegalStructure structure = new LegalStructure(); - structure.setCorporationType( type ); - structure.setCountry( "Canada" ); - structure.setName( "Rogers" ); - provider.setOwner( structure ); - structure.setOrigin( nat ); - s.persist( provider ); - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - provider = (InternetProvider) s.get( InternetProvider.class, provider.getId() ); - assertNotNull( provider.getOwner() ); - assertNotNull( "Many to one not set", provider.getOwner().getCorporationType() ); - assertEquals( "Wrong link", type.getType(), provider.getOwner().getCorporationType().getType() ); - assertNotNull( "2nd Many to one not set", provider.getOwner().getOrigin() ); - assertEquals( "Wrong 2nd link", nat.getName(), provider.getOwner().getOrigin().getName() ); - s.delete( provider ); - s.delete( provider.getOwner().getCorporationType() ); - s.delete( provider.getOwner().getOrigin() ); - tx.commit(); - s.close(); - } - - @Test - public void testEmbeddedAndOneToMany() throws Exception { - Session s; - s = openSession(); - Transaction tx = s.beginTransaction(); - InternetProvider provider = new InternetProvider(); - provider.setBrandName( "Fido" ); - LegalStructure structure = new LegalStructure(); - structure.setCountry( "Canada" ); - structure.setName( "Rogers" ); - provider.setOwner( structure ); - s.persist( provider ); - Manager manager = new Manager(); - manager.setName( "Bill" ); - manager.setEmployer( provider ); - structure.getTopManagement().add( manager ); - s.persist( manager ); - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - provider = (InternetProvider) s.get( InternetProvider.class, provider.getId() ); - assertNotNull( provider.getOwner() ); - Set topManagement = provider.getOwner().getTopManagement(); - assertNotNull( "OneToMany not set", topManagement ); - assertEquals( "Wrong number of elements", 1, topManagement.size() ); - manager = topManagement.iterator().next(); - assertEquals( "Wrong element", "Bill", manager.getName() ); - s.delete( manager ); - s.delete( provider ); - tx.commit(); - s.close(); - } - - @Test - @TestForIssue(jiraKey = "HHH-9642") - public void testEmbeddedAndOneToManyHql() throws Exception { - Session s; - s = openSession(); - Transaction tx = s.beginTransaction(); - InternetProvider provider = new InternetProvider(); - provider.setBrandName( "Fido" ); - LegalStructure structure = new LegalStructure(); - structure.setCountry( "Canada" ); - structure.setName( "Rogers" ); - provider.setOwner( structure ); - s.persist( provider ); - Manager manager = new Manager(); - manager.setName( "Bill" ); - manager.setEmployer( provider ); - structure.getTopManagement().add( manager ); - s.persist( manager ); - tx.commit(); - s.close(); - - s = openSession(); - s.getTransaction().begin(); - InternetProvider internetProviderQueried = - (InternetProvider) s.createQuery( "from InternetProvider" ).uniqueResult(); - assertFalse( Hibernate.isInitialized( internetProviderQueried.getOwner().getTopManagement() ) ); - s.getTransaction().commit(); - s.close(); - - s = openSession(); - s.getTransaction().begin(); - internetProviderQueried = - (InternetProvider) s.createQuery( "from InternetProvider i join fetch i.owner.topManagement" ) - .uniqueResult(); - assertTrue( Hibernate.isInitialized( internetProviderQueried.getOwner().getTopManagement() ) ); - s.getTransaction().commit(); - s.close(); - - s = openSession(); - s.getTransaction().begin(); - internetProviderQueried = - (InternetProvider) s.createQuery( - "from InternetProvider i join fetch i.owner o join fetch o.topManagement" ) - .uniqueResult(); - assertTrue( Hibernate.isInitialized( internetProviderQueried.getOwner().getTopManagement() ) ); - s.getTransaction().commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - provider = (InternetProvider) s.get( InternetProvider.class, provider.getId() ); - manager = provider.getOwner().getTopManagement().iterator().next(); - s.delete( manager ); - s.delete( provider ); - tx.commit(); - s.close(); - } - - - @Test - public void testDefaultCollectionTable() throws Exception { - //are the tables correct? - assertTrue( SchemaUtil.isTablePresent( "WealthyPerson_vacationHomes", metadata() ) ); - assertTrue( SchemaUtil.isTablePresent( "WelPers_LegacyVacHomes", metadata() ) ); - assertTrue( SchemaUtil.isTablePresent( "WelPers_VacHomes", metadata() ) ); - - //just to make sure, use the mapping - Session s; - Transaction tx; - WealthyPerson p = new WealthyPerson(); - Address a = new Address(); - Address vacation = new Address(); - Country c = new Country(); - Country bornCountry = new Country(); - c.setIso2( "DM" ); - c.setName( "Matt Damon Land" ); - bornCountry.setIso2( "US" ); - bornCountry.setName( "United States of America" ); - - a.address1 = "colorado street"; - a.city = "Springfield"; - a.country = c; - vacation.address1 = "rock street"; - vacation.city = "Plymouth"; - vacation.country = c; - p.vacationHomes.add( vacation ); - p.address = a; - p.bornIn = bornCountry; - p.name = "Homer"; - s = openSession(); - tx = s.beginTransaction(); - s.persist( p ); - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - p = (WealthyPerson) s.get( WealthyPerson.class, p.id ); - assertNotNull( p ); - assertNotNull( p.address ); - assertEquals( "Springfield", p.address.city ); - assertNotNull( p.address.country ); - assertEquals( "DM", p.address.country.getIso2() ); - assertNotNull( p.bornIn ); - assertEquals( "US", p.bornIn.getIso2() ); - tx.commit(); - s.close(); - } - - // make sure we support collection of embeddable objects inside embeddable objects - @Test - public void testEmbeddableInsideEmbeddable() throws Exception { - Session s; - Transaction tx; - - Collection urls = new ArrayList(); - URLFavorite urlFavorite = new URLFavorite(); - urlFavorite.setUrl( "http://highscalability.com/" ); - urls.add( urlFavorite ); - - urlFavorite = new URLFavorite(); - urlFavorite.setUrl( "http://www.jboss.org/" ); - urls.add( urlFavorite ); - - urlFavorite = new URLFavorite(); - urlFavorite.setUrl( "http://www.hibernate.org/" ); - urls.add( urlFavorite ); - - urlFavorite = new URLFavorite(); - urlFavorite.setUrl( "http://www.jgroups.org/" ); - urls.add( urlFavorite ); - - Collection ideas = new ArrayList(); - ideas.add( "lionheart" ); - ideas.add( "xforms" ); - ideas.add( "dynamic content" ); - ideas.add( "http" ); - - InternetFavorites internetFavorites = new InternetFavorites(); - internetFavorites.setLinks( urls ); - internetFavorites.setIdeas( ideas ); - - FavoriteThings favoriteThings = new FavoriteThings(); - favoriteThings.setWeb( internetFavorites ); - - s = openSession(); - - tx = s.beginTransaction(); - s.persist( favoriteThings ); - tx.commit(); - - tx = s.beginTransaction(); - s.flush(); - favoriteThings = (FavoriteThings) s.get( FavoriteThings.class, favoriteThings.getId() ); - assertTrue( "has web", favoriteThings.getWeb() != null ); - assertTrue( "has ideas", favoriteThings.getWeb().getIdeas() != null ); - assertTrue( "has favorite idea 'http'", favoriteThings.getWeb().getIdeas().contains( "http" ) ); - assertTrue( "has favorite idea 'http'", favoriteThings.getWeb().getIdeas().contains( "dynamic content" ) ); - - urls = favoriteThings.getWeb().getLinks(); - assertTrue( "has urls", urls != null ); - URLFavorite[] favs = new URLFavorite[4]; - urls.toArray( favs ); - assertTrue( - "has http://www.hibernate.org url favorite link", - "http://www.hibernate.org/".equals( favs[0].getUrl() ) || - "http://www.hibernate.org/".equals( favs[1].getUrl() ) || - "http://www.hibernate.org/".equals( favs[2].getUrl() ) || - "http://www.hibernate.org/".equals( favs[3].getUrl() ) - ); - tx.commit(); - s.close(); - } - - @Test - @TestForIssue(jiraKey = "HHH-3868") - public void testTransientMergeComponentParent() { - Session s = openSession(); - Transaction tx = s.beginTransaction(); - Book b = new Book(); - b.setIsbn( UUID.randomUUID().toString() ); - b.setSummary( new Summary() ); - b = (Book) s.merge( b ); - tx.commit(); - s.close(); - } - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Person.class, - WealthyPerson.class, - RegionalArticle.class, - AddressType.class, - VanillaSwap.class, - SpreadDeal.class, - Book.class, - InternetProvider.class, - CorpType.class, - Nationality.class, - Manager.class, - FavoriteThings.class - }; - } - - @Override - protected void configureMetadataBuilder(MetadataBuilder metadataBuilder) { - super.configureMetadataBuilder( metadataBuilder ); - metadataBuilder.applyImplicitNamingStrategy( ImplicitNamingStrategyJpaCompliantImpl.INSTANCE ); - } -}