From ff582903aa5e8e775e54672ece2161470dea2962 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Tue, 3 Jan 2017 17:34:26 +0100 Subject: [PATCH] HHH-11540 - Test for embeddable type that is only used in a type variable --- .../test/metamodel/BaseEmbeddedEntity.java | 62 ++++++++++++++++ .../jpa/test/metamodel/GenericsTest.java | 42 +++++++++++ .../hibernate/jpa/test/metamodel/Person.java | 44 +++++++++++ .../jpa/test/metamodel/PersonId.java | 73 +++++++++++++++++++ 4 files changed, 221 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/metamodel/BaseEmbeddedEntity.java create mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/metamodel/GenericsTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/metamodel/Person.java create mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/metamodel/PersonId.java diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/metamodel/BaseEmbeddedEntity.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/metamodel/BaseEmbeddedEntity.java new file mode 100644 index 0000000000..0180a2cde1 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/metamodel/BaseEmbeddedEntity.java @@ -0,0 +1,62 @@ + +/* + * 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.jpa.test.metamodel; + +import java.io.Serializable; +import javax.persistence.EmbeddedId; +import javax.persistence.MappedSuperclass; + +/** + * @author Christian Beikov + */ +@MappedSuperclass +public abstract class BaseEmbeddedEntity implements Serializable { + + private I id; + + public BaseEmbeddedEntity() { + } + + public BaseEmbeddedEntity(I id) { + this.id = id; + } + + @EmbeddedId + public I getId() { + return id; + } + + public void setId(I id) { + this.id = id; + } + + @Override + public int hashCode() { + int hash = 3; + hash = 47 * hash + (this.id != null ? this.id.hashCode() : 0); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final BaseEmbeddedEntity other = (BaseEmbeddedEntity) obj; + if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) { + return false; + } + return true; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/metamodel/GenericsTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/metamodel/GenericsTest.java new file mode 100644 index 0000000000..a47df0bef2 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/metamodel/GenericsTest.java @@ -0,0 +1,42 @@ +/* + * 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.jpa.test.metamodel; + +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; +import org.hibernate.testing.TestForIssue; +import org.junit.Test; + +import javax.persistence.EntityManager; +import javax.persistence.metamodel.EmbeddableType; +import javax.persistence.metamodel.EntityType; +import javax.persistence.metamodel.ManagedType; +import javax.persistence.metamodel.SingularAttribute; + +import static org.junit.Assert.*; + +/** + * @author Christian Beikov + */ +@TestForIssue( jiraKey = "HHH-11540" ) +public class GenericsTest extends BaseEntityManagerFunctionalTestCase { + @Override + public Class[] getAnnotatedClasses() { + return new Class[] { + Person.class, + PersonId.class + }; + } + + @Test + public void testEmbeddableTypeExists() { + EntityManager em = getOrCreateEntityManager(); + EmbeddableType idType = em.getMetamodel().embeddable( PersonId.class) ; + assertNotNull( idType ); + em.close(); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/metamodel/Person.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/metamodel/Person.java new file mode 100644 index 0000000000..6940fd34ff --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/metamodel/Person.java @@ -0,0 +1,44 @@ +/* + * 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.jpa.test.metamodel; + +import javax.persistence.Entity; + +/** + * @author Christian Beikov + */ +@Entity +public class Person extends BaseEmbeddedEntity { + + private String firstName; + private String lastName; + + public Person() { + } + + public Person(PersonId id, String firstName, String lastName) { + super(id); + this.firstName = firstName; + this.lastName = lastName; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/metamodel/PersonId.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/metamodel/PersonId.java new file mode 100644 index 0000000000..7a92d74767 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/metamodel/PersonId.java @@ -0,0 +1,73 @@ +/* + * 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.jpa.test.metamodel; + +import java.io.Serializable; +import javax.persistence.Embeddable; + +/** + * @author Christian Beikov + */ +@Embeddable +public class PersonId implements Serializable { + + private String ssn; + private String name; + + public PersonId() { + } + + public PersonId(String ssn, String name) { + this.ssn = ssn; + this.name = name; + } + + public String getSsn() { + return ssn; + } + + public void setSsn(String ssn) { + this.ssn = ssn; + } + + public String getName() { + return name; + } + + @Override + public int hashCode() { + int hash = 7; + hash = 67 * hash + (this.ssn != null ? this.ssn.hashCode() : 0); + hash = 67 * hash + (this.name != null ? this.name.hashCode() : 0); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final PersonId other = (PersonId) obj; + if ((this.ssn == null) ? (other.ssn != null) : !this.ssn.equals(other.ssn)) { + return false; + } + if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) { + return false; + } + return true; + } + + public void setName(String name) { + this.name = name; + } +}