diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/cfg/WrongCircularityDetectionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/cfg/WrongCircularityDetectionTest.java deleted file mode 100644 index 5e9241e917d6..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/cfg/WrongCircularityDetectionTest.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-or-later - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.orm.test.cfg; - -import jakarta.persistence.Basic; -import jakarta.persistence.Entity; -import jakarta.persistence.Id; -import jakarta.persistence.Inheritance; -import jakarta.persistence.InheritanceType; -import jakarta.persistence.Table; - -import org.hibernate.boot.Metadata; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistry; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; - -import org.hibernate.testing.orm.junit.JiraKey; -import org.hibernate.testing.junit4.BaseUnitTestCase; -import org.hibernate.testing.util.ServiceRegistryUtil; -import org.junit.Test; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -/** - * This test illustrates the problem when two related (in terms of joins) - * classes have the same table name in different schemas. - * - * @author Didier Villevalois - */ -@JiraKey(value = "HHH-7134") -public class WrongCircularityDetectionTest extends BaseUnitTestCase { - - @Test - public void testNoCircularityDetection() { - StandardServiceRegistry ssr = ServiceRegistryUtil.serviceRegistry(); - - try { - final Metadata metadata = new MetadataSources( ssr ) - .addAnnotatedClass( Entity1.class ) - .addAnnotatedClass( Entity2.class ) - .buildMetadata(); - - - org.hibernate.mapping.Table entity1Table = metadata.getEntityBinding( Entity1.class.getName() ).getTable(); - org.hibernate.mapping.Table entity2Table = metadata.getEntityBinding( Entity2.class.getName() ).getTable(); - - assertTrue( entity1Table.getName().equals( entity2Table.getName() ) ); - assertFalse( entity1Table.getSchema().equals( entity2Table.getSchema() ) ); - } - finally { - StandardServiceRegistryBuilder.destroy( ssr ); - } - } - - - @Entity - @Inheritance(strategy = InheritanceType.JOINED) - @Table(schema = "schema1", name = "entity") - public static class Entity1 { - private String id; - - @Id - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - } - - @Entity - @Table(schema = "schema2", name = "entity") - public static class Entity2 extends Entity1 { - private String value; - - @Basic - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/cut/CompositeDateTime.java b/hibernate-core/src/test/java/org/hibernate/orm/test/cut/CompositeDateTime.java deleted file mode 100644 index 20d27c5db91b..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/cut/CompositeDateTime.java +++ /dev/null @@ -1,184 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-or-later - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.orm.test.cut; - -import java.io.Serializable; - -/** - * Class for testing composite user types with more than two fields. - * - * @author Etienne Miret - */ -public class CompositeDateTime implements Serializable { - - private static final long serialVersionUID = 7401750071679578453L; - - private Integer year; - - private Integer month; - - private Integer day; - - private Integer hour; - - private Integer minute; - - private Integer second; - - public CompositeDateTime(final Integer year, final Integer month, final Integer day, final Integer hour, - final Integer minute, final Integer second) { - super(); - this.year = year; - this.month = month; - this.day = day; - this.hour = hour; - this.minute = minute; - this.second = second; - } - - /* - * Constructor for those who hate auto (un)boxing. - */ - public CompositeDateTime(final int year, final int month, final int day, final int hour, - final int minute, final int second) { - this( new Integer( year ), Integer.valueOf( month ), Integer.valueOf( day ), Integer.valueOf( hour ), - Integer.valueOf( minute ), Integer.valueOf( second ) ); - } - - public CompositeDateTime(final CompositeDateTime other) { - super(); - this.year = other.year; - this.month = other.month; - this.day = other.day; - this.hour = other.hour; - this.minute = other.minute; - this.second = other.second; - } - - public Integer getYear() { - return year; - } - - public void setYear(final Integer year) { - this.year = year; - } - - public Integer getMonth() { - return month; - } - - public void setMonth(final Integer month) { - this.month = month; - } - - public Integer getDay() { - return day; - } - - public void setDay(final Integer day) { - this.day = day; - } - - public Integer getHour() { - return hour; - } - - public void setHour(final Integer hour) { - this.hour = hour; - } - - public Integer getMinute() { - return minute; - } - - public void setMinute(final Integer minute) { - this.minute = minute; - } - - public Integer getSecond() { - return second; - } - - public void setSecond(final Integer second) { - this.second = second; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ( ( day == null ) ? 0 : day.hashCode() ); - result = prime * result + ( ( hour == null ) ? 0 : hour.hashCode() ); - result = prime * result + ( ( minute == null ) ? 0 : minute.hashCode() ); - result = prime * result + ( ( month == null ) ? 0 : month.hashCode() ); - result = prime * result + ( ( second == null ) ? 0 : second.hashCode() ); - result = prime * result + ( ( year == null ) ? 0 : year.hashCode() ); - return result; - } - - @Override - public boolean equals(Object obj) { - if ( this == obj ) { - return true; - } - if ( obj == null ) { - return false; - } - if ( !( obj instanceof CompositeDateTime ) ) { - return false; - } - CompositeDateTime other = (CompositeDateTime) obj; - if ( day == null ) { - if ( other.day != null ) { - return false; - } - } - else if ( !day.equals( other.day ) ) { - return false; - } - if ( hour == null ) { - if ( other.hour != null ) { - return false; - } - } - else if ( !hour.equals( other.hour ) ) { - return false; - } - if ( minute == null ) { - if ( other.minute != null ) { - return false; - } - } - else if ( !minute.equals( other.minute ) ) { - return false; - } - if ( month == null ) { - if ( other.month != null ) { - return false; - } - } - else if ( !month.equals( other.month ) ) { - return false; - } - if ( second == null ) { - if ( other.second != null ) { - return false; - } - } - else if ( !second.equals( other.second ) ) { - return false; - } - if ( year == null ) { - if ( other.year != null ) { - return false; - } - } - else if ( !year.equals( other.year ) ) { - return false; - } - return true; - } - -} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/cut/CompositeDateTimeUserType.java b/hibernate-core/src/test/java/org/hibernate/orm/test/cut/CompositeDateTimeUserType.java deleted file mode 100644 index c9a8f79afa65..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/cut/CompositeDateTimeUserType.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-or-later - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.orm.test.cut; - -import java.io.Serializable; - -import org.hibernate.HibernateException; -import org.hibernate.metamodel.spi.ValueAccess; -import org.hibernate.usertype.CompositeUserType; - -/** - * Class for testing composite user types with more than two fields. - * - * @author Etienne Miret - */ -public class CompositeDateTimeUserType implements CompositeUserType { - - @Override - public Object getPropertyValue(CompositeDateTime dateTime, int property) throws HibernateException { - return switch ( property ) { - case 0 -> dateTime.getYear(); - case 1 -> dateTime.getMonth(); - case 2 -> dateTime.getDay(); - case 3 -> dateTime.getHour(); - case 4 -> dateTime.getMinute(); - case 5 -> dateTime.getSecond(); - default -> throw new HibernateException( "This type has only 6 fields." ); - }; - } - - @Override - public CompositeDateTime instantiate(ValueAccess values) { - Integer year = values.getValue( 0, Integer.class ); - Integer month = values.getValue( 1, Integer.class ); - Integer day = values.getValue( 2, Integer.class ); - Integer hour = values.getValue( 3, Integer.class ); - Integer minute = values.getValue( 4, Integer.class ); - Integer second = values.getValue( 5, Integer.class ); - if ( year == null && month == null && day == null && hour == null && minute == null && second == null ) { - return null; - } - return new CompositeDateTime( year, month, day, hour, minute, second ); - } - - @Override - public Class embeddable() { - return CompositeDateTime.class; - } - - @Override - public Class returnedClass() { - return CompositeDateTime.class; - } - - @Override - public boolean equals(CompositeDateTime x, CompositeDateTime y) throws HibernateException { - return x == null ? y == null : x.equals( y ); - } - - @Override - public int hashCode(CompositeDateTime x) throws HibernateException { - return x == null ? 0 : x.hashCode(); - } - - @Override - public CompositeDateTime deepCopy(CompositeDateTime value) throws HibernateException { - return value == null ? null : new CompositeDateTime( value ); - } - - @Override - public boolean isMutable() { - return true; - } - - @Override - public Serializable disassemble(CompositeDateTime value) { - return deepCopy( value ); - } - - @Override - public CompositeDateTime assemble(Serializable cached, Object owner) { - return deepCopy( (CompositeDateTime) cached ); - } - - @Override - public CompositeDateTime replace(CompositeDateTime original, CompositeDateTime managed, Object owner) { - return deepCopy( original ); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/cut/CompositeUserTypeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/cut/CompositeUserTypeTest.java index a4001130881f..54ae94ef57b3 100755 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/cut/CompositeUserTypeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/cut/CompositeUserTypeTest.java @@ -11,9 +11,7 @@ import org.hibernate.dialect.DB2Dialect; import org.hibernate.dialect.HSQLDialect; import org.hibernate.dialect.SybaseASEDialect; -import org.hibernate.query.Query; -import org.hibernate.testing.orm.junit.JiraKey; import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; @@ -132,177 +130,4 @@ public void testCustomColumnReadAndWrite(SessionFactoryScope scope) { } ); } - - /** - * Tests the {@code =} operator on composite types. - */ - @Test - public void testEqualOperator(SessionFactoryScope scope) { - scope.inTransaction( - session -> { - final Transaction txn = new Transaction(); - txn.setDescription( "foo" ); - txn.setValue( new MonetoryAmount( new BigDecimal( 42 ), Currency.getInstance( "AUD" ) ) ); - txn.setTimestamp( new CompositeDateTime( 2014, 8, 23, 14, 35, 0 ) ); - session.persist( txn ); - final Query q = session.createQuery( - "from Transaction where value = :amount", - Transaction.class - ); - - /* Both amount and currency match. */ - q.setParameter( - "amount", - new MonetoryAmount( new BigDecimal( 42 ), Currency.getInstance( "AUD" ) ) - ); - assertEquals( 1, q.list().size() ); - - /* Only currency matches. */ - q.setParameter( - "amount", - new MonetoryAmount( new BigDecimal( 36 ), Currency.getInstance( "AUD" ) ) - ); - assertEquals( 0, q.list().size() ); - - /* Only amount matches. */ - q.setParameter( - "amount", - new MonetoryAmount( new BigDecimal( 42 ), Currency.getInstance( "EUR" ) ) - ); - assertEquals( 0, q.list().size() ); - - /* None match. */ - q.setParameter( - "amount", - new MonetoryAmount( new BigDecimal( 76 ), Currency.getInstance( "USD" ) ) - ); - assertEquals( 0, q.list().size() ); - - final Query qTimestamp = session.createQuery( - "from Transaction where timestamp = :timestamp", - Transaction.class - ); - - /* All matches. */ - qTimestamp.setParameter( "timestamp", new CompositeDateTime( 2014, 8, 23, 14, 35, 0 ) ); - assertEquals( 1, qTimestamp.list().size() ); - - /* None matches. */ - qTimestamp.setParameter( "timestamp", new CompositeDateTime( 2013, 9, 25, 12, 31, 25 ) ); - assertEquals( 0, qTimestamp.list().size() ); - - /* Year doesn't match. */ - qTimestamp.setParameter( "timestamp", new CompositeDateTime( 2013, 8, 23, 14, 35, 0 ) ); - assertEquals( 0, qTimestamp.list().size() ); - - /* Month doesn't match. */ - qTimestamp.setParameter( "timestamp", new CompositeDateTime( 2014, 9, 23, 14, 35, 0 ) ); - assertEquals( 0, qTimestamp.list().size() ); - - /* Minute doesn't match. */ - qTimestamp.setParameter( "timestamp", new CompositeDateTime( 2014, 8, 23, 14, 41, 0 ) ); - assertEquals( 0, qTimestamp.list().size() ); - - /* Second doesn't match. */ - qTimestamp.setParameter( "timestamp", new CompositeDateTime( 2014, 8, 23, 14, 35, 28 ) ); - assertEquals( 0, qTimestamp.list().size() ); - } - ); - } - - @Test - @JiraKey(value = "HHH-5946") - public void testNotEqualOperator(SessionFactoryScope scope) { - scope.inTransaction( - session -> { - final Transaction t1 = new Transaction(); - t1.setDescription( "foo" ); - t1.setValue( new MonetoryAmount( new BigDecimal( 178 ), Currency.getInstance( "EUR" ) ) ); - t1.setTimestamp( new CompositeDateTime( 2014, 8, 23, 14, 23, 0 ) ); - session.persist( t1 ); - - final Transaction t2 = new Transaction(); - t2.setDescription( "bar" ); - t2.setValue( new MonetoryAmount( new BigDecimal( 1000000 ), Currency.getInstance( "USD" ) ) ); - t2.setTimestamp( new CompositeDateTime( 2014, 8, 22, 14, 23, 0 ) ); - session.persist( t2 ); - - final Transaction t3 = new Transaction(); - t3.setDescription( "bar" ); - t3.setValue( new MonetoryAmount( new BigDecimal( 1000000 ), Currency.getInstance( "EUR" ) ) ); - t3.setTimestamp( new CompositeDateTime( 2014, 8, 22, 14, 23, 01 ) ); - session.persist( t3 ); - - final Query q1 = session.createQuery( "from Transaction where value <> :amount" ); - q1.setParameter( - "amount", - new MonetoryAmount( new BigDecimal( 178 ), Currency.getInstance( "EUR" ) ) - ); - assertEquals( 2, q1.list().size() ); - - final Query q2 = session.createQuery( - "from Transaction where value <> :amount and description = :str" ); - q2.setParameter( - "amount", - new MonetoryAmount( new BigDecimal( 1000000 ), Currency.getInstance( "USD" ) ) - ); - q2.setParameter( "str", "bar" ); - assertEquals( 1, q2.list().size() ); - - final Query q3 = session.createQuery( "from Transaction where timestamp <> :timestamp" ); - q3.setParameter( "timestamp", new CompositeDateTime( 2014, 8, 23, 14, 23, 0 ) ); - assertEquals( 2, q3.list().size() ); - } - ); - } - - @Test - @JiraKey(value = "HHH-5946") - public void testLessThanOperator(SessionFactoryScope scope) { - scope.inTransaction( - session -> { - final Query q = session.createQuery( "from Transaction where value < :amount", Transaction.class ); - q.setParameter( "amount", new MonetoryAmount( BigDecimal.ZERO, Currency.getInstance( "EUR" ) ) ); - q.list(); - } - ); - - } - - @Test - @JiraKey(value = "HHH-5946") - public void testLessOrEqualOperator(SessionFactoryScope scope) { - scope.inTransaction( - session -> { - final Query q = session.createQuery( "from Transaction where value <= :amount", Transaction.class ); - q.setParameter( "amount", new MonetoryAmount( BigDecimal.ZERO, Currency.getInstance( "USD" ) ) ); - q.list(); - } - ); - } - - @Test - @JiraKey(value = "HHH-5946") - public void testGreaterThanOperator(SessionFactoryScope scope) { - scope.inTransaction( - session -> { - final Query q = session.createQuery( "from Transaction where value > :amount", Transaction.class ); - q.setParameter( "amount", new MonetoryAmount( BigDecimal.ZERO, Currency.getInstance( "EUR" ) ) ); - q.list(); - } - ); - } - - @Test - @JiraKey(value = "HHH-5946") - public void testGreaterOrEqualOperator(SessionFactoryScope scope) { - scope.inTransaction( - session -> { - final Query q = session.createQuery( "from Transaction where value >= :amount", Transaction.class ); - q.setParameter( "amount", new MonetoryAmount( BigDecimal.ZERO, Currency.getInstance( "USD" ) ) ); - q.list(); - } - ); - } - } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/cut/Transaction.java b/hibernate-core/src/test/java/org/hibernate/orm/test/cut/Transaction.java index 3df4ba058820..425a95c61094 100755 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/cut/Transaction.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/cut/Transaction.java @@ -13,7 +13,6 @@ public class Transaction { private Long id; private String description; private MonetoryAmount value; - private CompositeDateTime timestamp; public String getDescription() { return description; @@ -39,12 +38,4 @@ public void setValue(MonetoryAmount value) { this.value = value; } - public CompositeDateTime getTimestamp() { - return timestamp; - } - - public void setTimestamp(CompositeDateTime timestamp) { - this.timestamp = timestamp; - } - } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/mapjoin/MapJoinTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/mapjoin/MapJoinTest.java deleted file mode 100644 index cfc35a97b894..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/mapjoin/MapJoinTest.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-or-later - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.orm.test.jpa.criteria.mapjoin; - -import jakarta.persistence.EntityManager; -import jakarta.persistence.criteria.CriteriaBuilder; -import jakarta.persistence.criteria.CriteriaQuery; -import jakarta.persistence.criteria.MapJoin; -import jakarta.persistence.criteria.Root; - -import org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest; -import org.hibernate.orm.test.jpa.metamodel.MapEntity; -import org.hibernate.orm.test.jpa.metamodel.MapEntityLocal; -import org.hibernate.orm.test.jpa.metamodel.MapEntityLocal_; -import org.hibernate.orm.test.jpa.metamodel.MapEntity_; - -import org.junit.jupiter.api.Test; - -public class MapJoinTest extends AbstractMetamodelSpecificTest { - - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { MapEntity.class, MapEntityLocal.class }; - } - - @Test - public void allEntities() { - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - - CriteriaBuilder cb = em.getCriteriaBuilder(); - CriteriaQuery query = cb.createQuery(MapEntity.class); - - Root entity = query.from(MapEntity.class); - MapJoin cname = entity.join(MapEntity_.localized); - - query = query - .select(entity) - .where( - cb.equal( cname.key(), "en" ) - ) - .orderBy( cb.asc( cb.upper( cname.value().get(MapEntityLocal_.shortName) ) ) ); - - em.createQuery(query).getResultList(); - - em.getTransaction().commit(); - em.close(); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/paths/PluralAttributeExpressionsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/paths/PluralAttributeExpressionsTest.java index 15fda22934e6..815052a0dd2b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/paths/PluralAttributeExpressionsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/paths/PluralAttributeExpressionsTest.java @@ -18,9 +18,8 @@ import org.hibernate.orm.test.jpa.metamodel.Address_; import org.hibernate.orm.test.jpa.metamodel.Article; import org.hibernate.orm.test.jpa.metamodel.Article_; -import org.hibernate.orm.test.jpa.metamodel.MapEntity; -import org.hibernate.orm.test.jpa.metamodel.MapEntityLocal; -import org.hibernate.orm.test.jpa.metamodel.MapEntity_; +import org.hibernate.orm.test.jpa.metamodel.EntityWithMapEC; +import org.hibernate.orm.test.jpa.metamodel.EntityWithMapEC_; import org.hibernate.orm.test.jpa.metamodel.Translation; import org.hibernate.query.criteria.HibernateCriteriaBuilder; import org.hibernate.query.criteria.JpaExpression; @@ -40,8 +39,7 @@ public class PluralAttributeExpressionsTest extends AbstractMetamodelSpecificTes public Class[] getAnnotatedClasses() { List classes = new ArrayList<>(); Collections.addAll( classes, super.getAnnotatedClasses() ); - classes.add( MapEntity.class ); - classes.add( MapEntityLocal.class ); + classes.add( EntityWithMapEC.class ); classes.add( Article.class ); classes.add( Translation.class ); @@ -77,7 +75,7 @@ public void testCollectionIsEmptyCriteria() { @Jira("https://hibernate.atlassian.net/browse/HHH-11225") public void testElementMapIsEmptyHql() { doInJPA( this::entityManagerFactory, entityManager -> { - entityManager.createQuery( "select m from MapEntity m where m.localized is empty" ).getResultList(); + entityManager.createQuery( "select m from EntityWithMapEC m where m.elements is empty" ).getResultList(); }); } @@ -87,11 +85,11 @@ public void testElementMapIsEmptyCriteria() { doInJPA( this::entityManagerFactory, entityManager -> { final HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) entityManager.getCriteriaBuilder(); - final CriteriaQuery criteria = cb.createQuery( MapEntity.class ); - final Root root = criteria.from( MapEntity.class); + final CriteriaQuery criteria = cb.createQuery( EntityWithMapEC.class ); + final Root root = criteria.from( EntityWithMapEC.class); criteria.select( root ) - .where( cb.isMapEmpty( (JpaExpression) root.get( MapEntity_.localized ) ) ); + .where( cb.isMapEmpty( (JpaExpression) root.get( EntityWithMapEC_.elements ) ) ); entityManager.createQuery( criteria ).getResultList(); }); @@ -151,7 +149,7 @@ public void testCollectionSizeCriteria() { @Jira("https://hibernate.atlassian.net/browse/HHH-11225") public void testElementMapSizeHql() { doInJPA( this::entityManagerFactory, entityManager -> { - entityManager.createQuery( "select m from MapEntity m where size( m.localized ) > 1" ).getResultList(); + entityManager.createQuery( "select m from EntityWithMapEC m where size( m.elements ) > 1" ).getResultList(); }); } @@ -161,11 +159,11 @@ public void testElementMapSizeCriteria() { doInJPA( this::entityManagerFactory, entityManager -> { final HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) entityManager.getCriteriaBuilder(); - final CriteriaQuery criteria = cb.createQuery( MapEntity.class ); - final Root root = criteria.from( MapEntity.class); + final CriteriaQuery criteria = cb.createQuery( EntityWithMapEC.class ); + final Root root = criteria.from( EntityWithMapEC.class); criteria.select( root ) - .where( cb.gt( cb.mapSize( (JpaExpression) root.get( MapEntity_.localized ) ), 1 ) ); + .where( cb.gt( cb.mapSize( (JpaExpression) root.get( EntityWithMapEC_.elements ) ), 1 ) ); entityManager.createQuery( criteria ).getResultList(); }); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/metamodel/EntityWithMapEC.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/metamodel/EntityWithMapEC.java new file mode 100644 index 000000000000..5094e836d8e8 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/metamodel/EntityWithMapEC.java @@ -0,0 +1,30 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.jpa.metamodel; + +import jakarta.persistence.CollectionTable; +import jakarta.persistence.ElementCollection; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + +import java.util.Map; + +@Entity +public class EntityWithMapEC { + @Id + private Long id; + + @ElementCollection + @CollectionTable + private Map elements; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/metamodel/MapElement.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/metamodel/MapElement.java new file mode 100644 index 000000000000..cc6c7d4575be --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/metamodel/MapElement.java @@ -0,0 +1,12 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.jpa.metamodel; + +import jakarta.persistence.Embeddable; + +@Embeddable +public class MapElement { + private String name; +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/metamodel/MapEntity.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/metamodel/MapEntity.java deleted file mode 100644 index e277cc763c0d..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/metamodel/MapEntity.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-or-later - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.orm.test.jpa.metamodel; - -import java.util.Map; - -import jakarta.persistence.CollectionTable; -import jakarta.persistence.Column; -import jakarta.persistence.ElementCollection; -import jakarta.persistence.Entity; -import jakarta.persistence.FetchType; -import jakarta.persistence.Id; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.MapKeyColumn; -import jakarta.persistence.Table; - -@Entity -@Table( name = "MAP_ENTITY" ) -public class MapEntity { - - @Id - @Column(name="key_") - private String key; - - @ElementCollection(fetch=FetchType.LAZY) - @CollectionTable(name="MAP_ENTITY_NAME", joinColumns=@JoinColumn(name="key_")) - @MapKeyColumn(name="lang_") - private Map localized; - - public String getKey() { - return key; - } - - public void setKey(String key) { - this.key = key; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/metamodel/MapEntityLocal.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/metamodel/MapEntityLocal.java deleted file mode 100644 index 2925bbecb883..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/metamodel/MapEntityLocal.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-or-later - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.orm.test.jpa.metamodel; - -import jakarta.persistence.Column; -import jakarta.persistence.Embeddable; - -@Embeddable -public class MapEntityLocal { - - @Column(name="short_name") - private String shortName; - - public String getShortName() { - return shortName; - } - - public void setShortName(String shortName) { - this.shortName = shortName; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/joined/JoinedInheritanceSameTableDifferentSchemaTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/joined/JoinedInheritanceSameTableDifferentSchemaTest.java new file mode 100644 index 000000000000..f924cb55480f --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/joined/JoinedInheritanceSameTableDifferentSchemaTest.java @@ -0,0 +1,55 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.mapping.inheritance.joined; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; +import jakarta.persistence.Table; +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.testing.orm.junit.Jira; +import org.hibernate.testing.util.ServiceRegistryUtil; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +@Jira("https://hibernate.atlassian.net/browse/HHH-7134") +public class JoinedInheritanceSameTableDifferentSchemaTest { + @Test + public void testMapping() { + StandardServiceRegistry ssr = ServiceRegistryUtil.serviceRegistry(); + try { + final Metadata metadata = new MetadataSources( ServiceRegistryUtil.serviceRegistry() ) + .addAnnotatedClass( EntityA.class ) + .addAnnotatedClass( EntityB.class ) + .buildMetadata(); + org.hibernate.mapping.Table entity1Table = metadata.getEntityBinding( EntityA.class.getName() ).getTable(); + org.hibernate.mapping.Table entity2Table = metadata.getEntityBinding( EntityB.class.getName() ).getTable(); + assertThat( entity1Table.getName() ).isEqualTo( entity2Table.getName() ); + assertThat( entity1Table.getSchema() ).isNotEqualTo( entity2Table.getSchema() ); + } + finally { + StandardServiceRegistryBuilder.destroy( ssr ); + } + } + + @Entity(name = "EntityA") + @Inheritance(strategy = InheritanceType.JOINED) + @Table(schema = "schema_1", name = "my_table") + public static class EntityA { + @Id + private Long id; + } + + @Entity(name = "EntityB") + @Table(schema = "schema_2", name = "my_table") + public static class EntityB extends EntityA { + private String name; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/namingstrategy/components/ComponentNamingStrategyForJoinColumnTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/namingstrategy/components/ComponentNamingStrategyForJoinColumnTest.java deleted file mode 100644 index 05e725125e26..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/namingstrategy/components/ComponentNamingStrategyForJoinColumnTest.java +++ /dev/null @@ -1,216 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-or-later - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.orm.test.namingstrategy.components; - -import java.util.ArrayList; -import java.util.List; - -import org.hibernate.boot.Metadata; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl; -import org.hibernate.boot.registry.StandardServiceRegistry; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.metamodel.CollectionClassification; - -import org.hibernate.testing.orm.junit.JiraKey; -import org.hibernate.testing.orm.junit.BaseUnitTest; -import org.hibernate.testing.util.ServiceRegistryUtil; - -import org.junit.jupiter.api.Test; - -import jakarta.persistence.ElementCollection; -import jakarta.persistence.Embeddable; -import jakarta.persistence.Embedded; -import jakarta.persistence.Entity; -import jakarta.persistence.FetchType; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.ManyToOne; - -import static org.junit.jupiter.api.Assertions.assertEquals; - - -/** - * @author Cai Chun - */ -@JiraKey(value = "HHH-11826") -@BaseUnitTest -public class ComponentNamingStrategyForJoinColumnTest { - - @Test - public void testNamingComponentPath() { - final StandardServiceRegistry ssr = ServiceRegistryUtil.serviceRegistryBuilder() - .applySetting( AvailableSettings.DEFAULT_LIST_SEMANTICS, CollectionClassification.BAG ) - .build(); - - try { - final MetadataSources ms = new MetadataSources( ssr ) - .addAnnotatedClass( Employee.class ) - .addAnnotatedClass( BankAccounts.class ) - .addAnnotatedClass( BankAccount.class ) - .addAnnotatedClass( WebUser.class ); - - final Metadata metadata = ms.getMetadataBuilder() - .applyImplicitNamingStrategy( - ImplicitNamingStrategyComponentPathImpl.INSTANCE ) - .build(); - - checkDefaultJoinTableAndAllColumnNames( - metadata, - Employee.class, - "bankAccounts.accounts", - "ComponentNamingStrategyForJoinColumnTest$Employee_bankAccounts_accounts", - "ComponentNamingStrategyForJoinColumnTest$Employee_id", - new String[] { - "ComponentNamingStrategyForJoinColumnTest$Employee_id", - "bankAccounts_accounts_accountNumber", - "bankAccounts_accounts_bankName", - "bankAccounts_accounts_verificationUser_id" - } - ); - } - finally { - StandardServiceRegistryBuilder.destroy( ssr ); - } - } - - protected void checkDefaultJoinTableAndAllColumnNames( - Metadata metadata, - Class ownerEntityClass, - String ownerCollectionPropertyName, - String expectedCollectionTableName, - String ownerForeignKeyNameExpected, - String[] columnNames) { - final org.hibernate.mapping.Collection collection = metadata.getCollectionBinding( ownerEntityClass.getName() + '.' + ownerCollectionPropertyName ); - - final org.hibernate.mapping.Table table = collection.getCollectionTable(); - assertEquals( expectedCollectionTableName, table.getName() ); - - // The default owner and inverse join columns can only be computed if they have PK with 1 column. - assertEquals( 1, collection.getOwner().getKey().getColumnSpan() ); - assertEquals( - ownerForeignKeyNameExpected, - collection.getKey().getSelectables().get( 0 ).getText() - ); - - int columnNumber = table.getColumnSpan(); - for ( int i = 0; i < columnNumber; i++ ) { - assertEquals( columnNames[i], table.getColumn( i + 1 ).getName()); - } - } - - @Entity - public static class Employee { - - @Id - @GeneratedValue( - strategy = GenerationType.AUTO - ) - private Long id; - - private String name; - - @Embedded - private BankAccounts bankAccounts = new BankAccounts(); - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public BankAccounts getBankAccounts() { - return bankAccounts; - } - - public void setBankAccounts(BankAccounts bankAccounts) { - this.bankAccounts = bankAccounts; - } - - } - - @Embeddable - public static class BankAccounts { - - @ElementCollection(fetch = FetchType.LAZY) - private List accounts = new ArrayList<>(); - - public List getAccounts() { - return this.accounts; - } - } - - @Embeddable - public static class BankAccount { - - private String bankName; - - private String accountNumber; - - @ManyToOne(fetch = FetchType.LAZY) - private WebUser verificationUser; - - public String getBankName() { - return bankName; - } - public void setBankName(String bankName) { - this.bankName = bankName; - } - - public String getAccountNumber() { - return accountNumber; - } - public void setAccountNumber(String accountNumber) { - this.accountNumber = accountNumber; - } - - public WebUser getVerificationUser() { - return verificationUser; - } - public void setVerificationUser(WebUser verificationUser) { - this.verificationUser = verificationUser; - } - } - - @Entity - public static class WebUser { - - @Id - @GeneratedValue( - strategy = GenerationType.AUTO - ) - private Long id; - - private String name; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/namingstrategy/components/ComponentNamingStrategyJoinColumnTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/namingstrategy/components/ComponentNamingStrategyJoinColumnTest.java new file mode 100644 index 000000000000..ccc4d6732c00 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/namingstrategy/components/ComponentNamingStrategyJoinColumnTest.java @@ -0,0 +1,91 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.namingstrategy.components; + +import jakarta.persistence.ElementCollection; +import jakarta.persistence.Embeddable; +import jakarta.persistence.Embedded; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.mapping.Column; +import org.hibernate.testing.orm.junit.Jira; +import org.hibernate.testing.util.ServiceRegistryUtil; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + + +@Jira("https://hibernate.atlassian.net/browse/HHH-11826") +public class ComponentNamingStrategyJoinColumnTest { + @Test + public void testNamingComponentPath() { + final StandardServiceRegistry ssr = ServiceRegistryUtil.serviceRegistry(); + + try { + final MetadataSources ms = new MetadataSources( ssr ) + .addAnnotatedClass( BaseEntity.class ) + .addAnnotatedClass( CollectionWrapper.class ) + .addAnnotatedClass( CollectionItem.class ) + .addAnnotatedClass( ToOneEntity.class ); + final Metadata metadata = ms.getMetadataBuilder() + .applyImplicitNamingStrategy( ImplicitNamingStrategyComponentPathImpl.INSTANCE ) + .build(); + + final org.hibernate.mapping.Collection collection = metadata.getCollectionBinding( + BaseEntity.class.getName() + '.' + "collectionWrapper.items" + ); + + final org.hibernate.mapping.Table table = collection.getCollectionTable(); + assertThat( table.getName() ).isEqualTo( "BaseEntity_collectionWrapper_items" ); + assertThat( collection.getOwner().getKey().getColumnSpan() ).isEqualTo( 1 ); + assertThat( collection.getKey().getColumns().get( 0 ).getName() ).isEqualTo( "BaseEntity_id" ); + assertThat( table.getColumns().stream().map( Column::getName ) ).contains( + "BaseEntity_id", + "collectionWrapper_items_name", + "collectionWrapper_items_toOne_id" + ); + } + finally { + StandardServiceRegistryBuilder.destroy( ssr ); + } + } + + @Entity(name = "BaseEntity") + static class BaseEntity { + @Id + private Long id; + + @Embedded + private CollectionWrapper collectionWrapper; + } + + @Embeddable + static class CollectionWrapper { + @ElementCollection + private List items; + } + + @Embeddable + static class CollectionItem { + private String name; + + @ManyToOne + private ToOneEntity toOne; + } + + @Entity(name = "ToOneEntity") + static class ToOneEntity { + @Id + private Long id; + } +} diff --git a/hibernate-core/src/test/resources/org/hibernate/orm/test/cut/Transaction.hbm.xml b/hibernate-core/src/test/resources/org/hibernate/orm/test/cut/Transaction.hbm.xml index b67fa9fe2c25..5d43059a2abc 100755 --- a/hibernate-core/src/test/resources/org/hibernate/orm/test/cut/Transaction.hbm.xml +++ b/hibernate-core/src/test/resources/org/hibernate/orm/test/cut/Transaction.hbm.xml @@ -26,14 +26,6 @@ - - - - - - - -