HHH-4313 - ConcurrentModificationException when collection of embeddable contains a collection
Throw exception when the JPA rule about nesting any collections from inside an @ElementCollection
This commit is contained in:
parent
4dea338fb6
commit
b1c7615904
|
@ -1872,6 +1872,18 @@ public final class AnnotationBinder {
|
|||
ManyToMany manyToManyAnn = property.getAnnotation( ManyToMany.class );
|
||||
ElementCollection elementCollectionAnn = property.getAnnotation( ElementCollection.class );
|
||||
|
||||
if ( ( oneToManyAnn != null || manyToManyAnn != null || elementCollectionAnn != null ) &&
|
||||
isToManyAssociationWithinEmbeddableCollection(
|
||||
propertyHolder ) ) {
|
||||
throw new AnnotationException(
|
||||
"@OneToMany, @ManyToMany or @ElementCollection cannot be used inside an @Embeddable that is also contained within an @ElementCollection: "
|
||||
+ BinderHelper.getPath(
|
||||
propertyHolder,
|
||||
inferredData
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
final IndexColumn indexColumn;
|
||||
|
||||
if ( property.isAnnotationPresent( OrderColumn.class ) ) {
|
||||
|
@ -2319,6 +2331,14 @@ public final class AnnotationBinder {
|
|||
}
|
||||
}
|
||||
|
||||
private static boolean isToManyAssociationWithinEmbeddableCollection(PropertyHolder propertyHolder) {
|
||||
if(propertyHolder instanceof ComponentPropertyHolder) {
|
||||
ComponentPropertyHolder componentPropertyHolder = (ComponentPropertyHolder) propertyHolder;
|
||||
return componentPropertyHolder.isWithinElementCollection();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void setVersionInformation(XProperty property, PropertyBinder propertyBinder) {
|
||||
propertyBinder.getSimpleValueBinder().setVersion( true );
|
||||
if(property.isAnnotationPresent( Source.class )) {
|
||||
|
|
|
@ -303,7 +303,12 @@ public class ClassPropertyHolder extends AbstractPropertyHolder {
|
|||
return false;
|
||||
}
|
||||
|
||||
// @Override
|
||||
@Override
|
||||
public boolean isWithinElementCollection() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public AttributeConverterDefinition resolveAttributeConverter(String attributeName) {
|
||||
//
|
||||
// // @Convert annotations take precedence if present
|
||||
|
|
|
@ -288,6 +288,11 @@ public class CollectionPropertyHolder extends AbstractPropertyHolder {
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWithinElementCollection() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public PersistentClass getPersistentClass() {
|
||||
return collection.getOwner();
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
*/
|
||||
package org.hibernate.cfg;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.persistence.Column;
|
||||
|
@ -63,6 +62,7 @@ public class ComponentPropertyHolder extends AbstractPropertyHolder {
|
|||
// joinsPerRealTableName in ClassPropertyHolder
|
||||
private Component component;
|
||||
private boolean isOrWithinEmbeddedId;
|
||||
private boolean isWithinElementCollection;
|
||||
|
||||
// private boolean virtual;
|
||||
private String embeddedAttributeName;
|
||||
|
@ -83,6 +83,8 @@ public class ComponentPropertyHolder extends AbstractPropertyHolder {
|
|||
|| ( embeddedXProperty != null &&
|
||||
( embeddedXProperty.isAnnotationPresent( Id.class )
|
||||
|| embeddedXProperty.isAnnotationPresent( EmbeddedId.class ) ) );
|
||||
this.isWithinElementCollection = parent.isWithinElementCollection() ||
|
||||
parent instanceof CollectionPropertyHolder;
|
||||
|
||||
if ( embeddedXProperty != null ) {
|
||||
// this.virtual = false;
|
||||
|
@ -326,6 +328,10 @@ public class ComponentPropertyHolder extends AbstractPropertyHolder {
|
|||
return isOrWithinEmbeddedId;
|
||||
}
|
||||
|
||||
public boolean isWithinElementCollection() {
|
||||
return isWithinElementCollection;
|
||||
}
|
||||
|
||||
public PersistentClass getPersistentClass() {
|
||||
return component.getOwner();
|
||||
}
|
||||
|
|
|
@ -42,6 +42,11 @@ public interface PropertyHolder {
|
|||
*/
|
||||
boolean isOrWithinEmbeddedId();
|
||||
|
||||
/**
|
||||
* Return true if this component is withing an @ElementCollection.
|
||||
*/
|
||||
boolean isWithinElementCollection();
|
||||
|
||||
PersistentClass getPersistentClass();
|
||||
|
||||
boolean isComponent();
|
||||
|
|
|
@ -0,0 +1,269 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.annotations.embeddables.collection;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.CollectionTable;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.ElementCollection;
|
||||
import javax.persistence.Embeddable;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Version;
|
||||
|
||||
import org.hibernate.AnnotationException;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
@TestForIssue(jiraKey = "HHH-11302")
|
||||
public class EmbeddableWithManyToMany_HHH_11302_Test
|
||||
extends BaseCoreFunctionalTestCase {
|
||||
|
||||
// Add your entities here.
|
||||
@Override
|
||||
protected Class[] getAnnotatedClasses() {
|
||||
return new Class[] {
|
||||
ContactType.class,
|
||||
Person.class
|
||||
};
|
||||
}
|
||||
|
||||
protected void buildSessionFactory() {
|
||||
try {
|
||||
super.buildSessionFactory();
|
||||
fail( "Should throw AnnotationException!" );
|
||||
}
|
||||
catch ( AnnotationException expected ) {
|
||||
assertTrue( expected.getMessage().startsWith(
|
||||
"@OneToMany, @ManyToMany or @ElementCollection cannot be used inside an @Embeddable that is also contained within an @ElementCollection"
|
||||
) );
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
}
|
||||
|
||||
@Entity
|
||||
@Table(name = "CONTACTTYPE")
|
||||
public static class ContactType implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name = "id", updatable = false, nullable = false)
|
||||
private Long id;
|
||||
|
||||
@Version
|
||||
@Column(name = "version")
|
||||
private int version;
|
||||
|
||||
@Column(name = "contactType", nullable = false)
|
||||
private String type;
|
||||
|
||||
public Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(final Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
return this.version;
|
||||
}
|
||||
|
||||
public void setVersion(final int version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if ( this == obj ) {
|
||||
return true;
|
||||
}
|
||||
if ( !( obj instanceof ContactType ) ) {
|
||||
return false;
|
||||
}
|
||||
ContactType other = (ContactType) obj;
|
||||
if ( id != null ) {
|
||||
if ( !id.equals( other.id ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ( ( id == null ) ? 0 : id.hashCode() );
|
||||
return result;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String result = getClass().getSimpleName() + " ";
|
||||
if ( id != null ) {
|
||||
result += "id: " + id;
|
||||
}
|
||||
result += ", version: " + version;
|
||||
if ( type != null && !type.trim().isEmpty() ) {
|
||||
result += ", type: " + type;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity
|
||||
@Table(name = "PERSON")
|
||||
public static class Person implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name = "id", updatable = false, nullable = false)
|
||||
private Long id;
|
||||
|
||||
@Version
|
||||
@Column(name = "version")
|
||||
private int version;
|
||||
|
||||
@ElementCollection
|
||||
@CollectionTable(
|
||||
name = "CONTACT_INFO",
|
||||
joinColumns = @JoinColumn(name = "person_id")
|
||||
)
|
||||
private List<ContactInformation> contacts;
|
||||
|
||||
public Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(final Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
return this.version;
|
||||
}
|
||||
|
||||
public void setVersion(final int version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String result = getClass().getSimpleName() + " ";
|
||||
if ( id != null ) {
|
||||
result += "id: " + id;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
Person person = (Person) o;
|
||||
return version == person.version && Objects.equals( id, person.id );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( id, version, contacts );
|
||||
}
|
||||
|
||||
public void setContacts(List<ContactInformation> contacts) {
|
||||
this.contacts = contacts;
|
||||
}
|
||||
|
||||
public List<ContactInformation> getContacts() {
|
||||
return contacts;
|
||||
}
|
||||
}
|
||||
|
||||
@Embeddable
|
||||
public static class ContactInformation implements Serializable {
|
||||
|
||||
@Column(name = "name")
|
||||
String name;
|
||||
|
||||
@ManyToMany(cascade = CascadeType.ALL)
|
||||
@JoinTable(
|
||||
name = "CONTACT_TYPE",
|
||||
joinColumns = @JoinColumn(name = "id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "id")
|
||||
)
|
||||
private List<ContactType> contactType = new ArrayList<>();
|
||||
|
||||
public List<ContactType> getContactType() {
|
||||
return contactType;
|
||||
}
|
||||
|
||||
public void setContactType(final List<ContactType> contactType) {
|
||||
this.contactType = contactType;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
ContactInformation that = (ContactInformation) o;
|
||||
return Objects.equals( name, that.name );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( name );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,11 +4,10 @@
|
|||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.annotations.embeddables;
|
||||
package org.hibernate.test.annotations.embeddables.collection;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import javax.persistence.CascadeType;
|
||||
|
@ -26,21 +25,21 @@ import javax.persistence.OneToMany;
|
|||
import javax.persistence.Table;
|
||||
import javax.persistence.Version;
|
||||
|
||||
import org.hibernate.testing.FailureExpected;
|
||||
import org.hibernate.AnnotationException;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
@FailureExpected( jiraKey = "HHH-11302", message = "TBD")
|
||||
@Ignore
|
||||
public class EmbeddableWithOneToManyTest extends BaseCoreFunctionalTestCase {
|
||||
@TestForIssue(jiraKey = "HHH-11302")
|
||||
public class EmbeddableWithOneToMany_HHH_11302_Test
|
||||
extends BaseCoreFunctionalTestCase {
|
||||
|
||||
// Add your entities here.
|
||||
@Override
|
||||
|
@ -51,36 +50,20 @@ public class EmbeddableWithOneToManyTest extends BaseCoreFunctionalTestCase {
|
|||
};
|
||||
}
|
||||
|
||||
// Add your tests, using standard JUnit.
|
||||
protected void buildSessionFactory() {
|
||||
try {
|
||||
super.buildSessionFactory();
|
||||
fail( "Should throw AnnotationException!" );
|
||||
}
|
||||
catch ( AnnotationException expected ) {
|
||||
assertTrue( expected.getMessage().startsWith(
|
||||
"@OneToMany, @ManyToMany or @ElementCollection cannot be used inside an @Embeddable that is also contained within an @ElementCollection"
|
||||
) );
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hhh11302Test() throws Exception {
|
||||
Person person = doInHibernate( this::sessionFactory, s -> {
|
||||
ContactType emailContactType = new ContactType();
|
||||
emailContactType.setType("EMAIL");
|
||||
|
||||
ContactType twitterContactType = new ContactType();
|
||||
twitterContactType.setType("TWITTER");
|
||||
|
||||
List<ContactType> contactTypes = Arrays.asList(emailContactType, twitterContactType);
|
||||
|
||||
Person.ContactInformation contactInformation = new Person.ContactInformation();
|
||||
contactInformation.setContactType(contactTypes);
|
||||
|
||||
Person _person = new Person();
|
||||
_person.setContacts(Arrays.asList(contactInformation));
|
||||
s.persist(_person);
|
||||
|
||||
return _person;
|
||||
} );
|
||||
|
||||
doInHibernate( this::sessionFactory, s -> {
|
||||
Person person2 = s.find(Person.class,person.getId());
|
||||
assertEquals(person,person2);
|
||||
assertTrue(person2.getContacts().size() > 0);
|
||||
assertEquals(person.getContacts(),person2.getContacts());
|
||||
assertTrue(person2.getContacts().get(0).getContactType().size() > 0);
|
||||
assertEquals(person.getContacts().get(0).getContactType(),person2.getContacts().get(0).getContactType());
|
||||
} );
|
||||
public void test() {
|
||||
}
|
||||
|
||||
@Entity
|
||||
|
@ -118,15 +101,15 @@ public class EmbeddableWithOneToManyTest extends BaseCoreFunctionalTestCase {
|
|||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
if ( this == obj ) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof ContactType )) {
|
||||
if ( !( obj instanceof ContactType ) ) {
|
||||
return false;
|
||||
}
|
||||
ContactType other = (ContactType) obj;
|
||||
if (id != null) {
|
||||
if (!id.equals(other.id)) {
|
||||
if ( id != null ) {
|
||||
if ( !id.equals( other.id ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -137,7 +120,7 @@ public class EmbeddableWithOneToManyTest extends BaseCoreFunctionalTestCase {
|
|||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((id == null) ? 0 : id.hashCode());
|
||||
result = prime * result + ( ( id == null ) ? 0 : id.hashCode() );
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -152,11 +135,13 @@ public class EmbeddableWithOneToManyTest extends BaseCoreFunctionalTestCase {
|
|||
@Override
|
||||
public String toString() {
|
||||
String result = getClass().getSimpleName() + " ";
|
||||
if (id != null)
|
||||
if ( id != null ) {
|
||||
result += "id: " + id;
|
||||
}
|
||||
result += ", version: " + version;
|
||||
if (type != null && !type.trim().isEmpty())
|
||||
if ( type != null && !type.trim().isEmpty() ) {
|
||||
result += ", type: " + type;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -179,7 +164,7 @@ public class EmbeddableWithOneToManyTest extends BaseCoreFunctionalTestCase {
|
|||
@ElementCollection
|
||||
@CollectionTable(
|
||||
name = "CONTACT_INFO",
|
||||
joinColumns=@JoinColumn(name = "person_id")
|
||||
joinColumns = @JoinColumn(name = "person_id")
|
||||
)
|
||||
private List<ContactInformation> contacts;
|
||||
|
||||
|
@ -202,24 +187,27 @@ public class EmbeddableWithOneToManyTest extends BaseCoreFunctionalTestCase {
|
|||
@Override
|
||||
public String toString() {
|
||||
String result = getClass().getSimpleName() + " ";
|
||||
if (id != null)
|
||||
if ( id != null ) {
|
||||
result += "id: " + id;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
Person person = (Person) o;
|
||||
return version == person.version && Objects.equals( id, person.id);
|
||||
return version == person.version && Objects.equals( id, person.id );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, version, contacts);
|
||||
return Objects.hash( id, version, contacts );
|
||||
}
|
||||
|
||||
public void setContacts(List<ContactInformation> contacts) {
|
||||
|
@ -229,51 +217,53 @@ public class EmbeddableWithOneToManyTest extends BaseCoreFunctionalTestCase {
|
|||
public List<ContactInformation> getContacts() {
|
||||
return contacts;
|
||||
}
|
||||
}
|
||||
|
||||
@Embeddable
|
||||
public static class ContactInformation implements Serializable {
|
||||
@Embeddable
|
||||
public static class ContactInformation implements Serializable {
|
||||
|
||||
@Column(name = "name")
|
||||
String name;
|
||||
@Column(name = "name")
|
||||
String name;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
@JoinTable(
|
||||
name="CONTACT_TYPE",
|
||||
joinColumns = @JoinColumn( name="id"),
|
||||
inverseJoinColumns=@JoinColumn(name="id")
|
||||
)
|
||||
private List<ContactType> contactType = new ArrayList<>();
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
@JoinTable(
|
||||
name = "CONTACT_TYPE",
|
||||
joinColumns = @JoinColumn(name = "id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "id")
|
||||
)
|
||||
private List<ContactType> contactType = new ArrayList<>();
|
||||
|
||||
public List<ContactType> getContactType() {
|
||||
return contactType;
|
||||
}
|
||||
|
||||
public void setContactType(final List<ContactType> contactType) {
|
||||
this.contactType = contactType;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
ContactInformation that = (ContactInformation) o;
|
||||
return Objects.equals(name, that.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(name);
|
||||
}
|
||||
public List<ContactType> getContactType() {
|
||||
return contactType;
|
||||
}
|
||||
|
||||
public void setContactType(final List<ContactType> contactType) {
|
||||
this.contactType = contactType;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
ContactInformation that = (ContactInformation) o;
|
||||
return Objects.equals( name, that.name );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( name );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,153 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.annotations.embeddables.collection;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Set;
|
||||
import javax.persistence.ElementCollection;
|
||||
import javax.persistence.Embeddable;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Version;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Pattern;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
import org.hibernate.AnnotationException;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
@TestForIssue(jiraKey = "HHH-8564")
|
||||
public class EmbeddableWithOneToMany_HHH_8564_Test
|
||||
extends BaseCoreFunctionalTestCase {
|
||||
|
||||
// Add your entities here.
|
||||
@Override
|
||||
protected Class[] getAnnotatedClasses() {
|
||||
return new Class[] {
|
||||
User.class,
|
||||
};
|
||||
}
|
||||
|
||||
protected void buildSessionFactory() {
|
||||
try {
|
||||
super.buildSessionFactory();
|
||||
fail( "Should throw AnnotationException!" );
|
||||
}
|
||||
catch ( AnnotationException expected ) {
|
||||
assertTrue( expected.getMessage().startsWith(
|
||||
"@OneToMany, @ManyToMany or @ElementCollection cannot be used inside an @Embeddable that is also contained within an @ElementCollection"
|
||||
) );
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
}
|
||||
|
||||
@Embeddable
|
||||
public static class Address {
|
||||
|
||||
@ElementCollection(fetch = FetchType.EAGER)
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Set<AddressType> type;
|
||||
|
||||
@NotNull
|
||||
@Size(min = 3, max = 200)
|
||||
private String street;
|
||||
|
||||
@NotNull
|
||||
@Pattern(regexp = "[0-9]{5}")
|
||||
private String zipcode;
|
||||
|
||||
@NotNull
|
||||
@Size(min = 3, max = 60)
|
||||
private String city;
|
||||
|
||||
@NotNull
|
||||
@Size(min = 3, max = 60)
|
||||
private String state;
|
||||
|
||||
}
|
||||
|
||||
public static enum AddressType {
|
||||
|
||||
OFFICE, HOME, BILLING
|
||||
|
||||
}
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
@SuppressWarnings("serial")
|
||||
public static class User implements Serializable {
|
||||
|
||||
@Id
|
||||
@NotNull
|
||||
private String email;
|
||||
|
||||
@NotNull
|
||||
private String password;
|
||||
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
@NotNull
|
||||
private String surname;
|
||||
|
||||
@ElementCollection(fetch = FetchType.EAGER)
|
||||
private Set<Address> addresses;
|
||||
|
||||
@Version
|
||||
private long version;
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ( ( email == null ) ?
|
||||
0 :
|
||||
email.hashCode() );
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if ( this == obj ) {
|
||||
return true;
|
||||
}
|
||||
if ( obj == null ) {
|
||||
return false;
|
||||
}
|
||||
if ( getClass() != obj.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
User other = (User) obj;
|
||||
if ( email == null ) {
|
||||
if ( other.email != null ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if ( !email.equals( other.email ) ) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.annotations.embeddables.collection;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import javax.persistence.ElementCollection;
|
||||
import javax.persistence.Embeddable;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.MapKeyColumn;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Version;
|
||||
|
||||
import org.hibernate.AnnotationException;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
@TestForIssue(jiraKey = "HHH-8860")
|
||||
public class EmbeddableWithOneToMany_HHH_8860_Test
|
||||
extends BaseCoreFunctionalTestCase {
|
||||
|
||||
// Add your entities here.
|
||||
@Override
|
||||
protected Class[] getAnnotatedClasses() {
|
||||
return new Class[] {
|
||||
Data.class,
|
||||
};
|
||||
}
|
||||
|
||||
protected void buildSessionFactory() {
|
||||
try {
|
||||
super.buildSessionFactory();
|
||||
fail( "Should throw AnnotationException!" );
|
||||
}
|
||||
catch ( AnnotationException expected ) {
|
||||
assertTrue( expected.getMessage().startsWith(
|
||||
"@OneToMany, @ManyToMany or @ElementCollection cannot be used inside an @Embeddable that is also contained within an @ElementCollection"
|
||||
) );
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
}
|
||||
|
||||
@Entity(name = "Data")
|
||||
public static class Data {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
@Version
|
||||
private Integer version;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@ElementCollection(fetch = FetchType.LAZY)
|
||||
@MapKeyColumn(name = "key")
|
||||
private Map<String, GenericStringList> stringlist = new TreeMap<>();
|
||||
|
||||
}
|
||||
|
||||
@Embeddable
|
||||
public static class GenericStringList {
|
||||
|
||||
@OneToMany(fetch = FetchType.LAZY)
|
||||
public List<String> stringList = new LinkedList<>();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package org.hibernate.test.annotations.embeddables.collection.xml;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* <code>ContactInformation</code> -
|
||||
*
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class ContactInformation implements Serializable {
|
||||
|
||||
private String name;
|
||||
|
||||
private List<ContactType> contactType = new ArrayList<>();
|
||||
|
||||
public List<ContactType> getContactType() {
|
||||
return contactType;
|
||||
}
|
||||
|
||||
public void setContactType(final List<ContactType> contactType) {
|
||||
this.contactType = contactType;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
ContactInformation that = (ContactInformation) o;
|
||||
return Objects.equals( name, that.name );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( name );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
package org.hibernate.test.annotations.embeddables.collection.xml;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* <code>ContactType</code> -
|
||||
*
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class ContactType implements Serializable {
|
||||
|
||||
private Long id;
|
||||
|
||||
private int version;
|
||||
|
||||
private String type;
|
||||
|
||||
public Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(final Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
return this.version;
|
||||
}
|
||||
|
||||
public void setVersion(final int version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if ( this == obj ) {
|
||||
return true;
|
||||
}
|
||||
if ( !( obj instanceof ContactType ) ) {
|
||||
return false;
|
||||
}
|
||||
ContactType other = (ContactType) obj;
|
||||
if ( id != null ) {
|
||||
if ( !id.equals( other.id ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ( ( id == null ) ? 0 : id.hashCode() );
|
||||
return result;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String result = getClass().getSimpleName() + " ";
|
||||
if ( id != null ) {
|
||||
result += "id: " + id;
|
||||
}
|
||||
result += ", version: " + version;
|
||||
if ( type != null && !type.trim().isEmpty() ) {
|
||||
result += ", type: " + type;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.annotations.embeddables.collection.xml;
|
||||
|
||||
import org.hibernate.AnnotationException;
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
@TestForIssue(jiraKey = "HHH-11302")
|
||||
public class EmbeddableWithOneToMany_HHH_11302_xml_Test extends
|
||||
BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
public String[] getEjb3DD() {
|
||||
return new String[] {
|
||||
"org/hibernate/test/annotations/embeddables/collection/orm.xml"
|
||||
};
|
||||
}
|
||||
|
||||
public void buildEntityManagerFactory() throws Exception {
|
||||
try {
|
||||
super.buildEntityManagerFactory();
|
||||
fail( "Should throw AnnotationException!" );
|
||||
}
|
||||
catch ( AnnotationException expected ) {
|
||||
assertTrue( expected.getMessage().startsWith(
|
||||
"@OneToMany, @ManyToMany or @ElementCollection cannot be used inside an @Embeddable that is also contained within an @ElementCollection"
|
||||
) );
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package org.hibernate.test.annotations.embeddables.collection.xml;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* <code>Person</code> -
|
||||
*
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class Person implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Long id;
|
||||
|
||||
private int version;
|
||||
|
||||
private List<ContactInformation> contacts;
|
||||
|
||||
public Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(final Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
return this.version;
|
||||
}
|
||||
|
||||
public void setVersion(final int version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String result = getClass().getSimpleName() + " ";
|
||||
if ( id != null ) {
|
||||
result += "id: " + id;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
Person person = (Person) o;
|
||||
return version == person.version && Objects.equals( id, person.id );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( id, version, contacts );
|
||||
}
|
||||
|
||||
public void setContacts(List<ContactInformation> contacts) {
|
||||
this.contacts = contacts;
|
||||
}
|
||||
|
||||
public List<ContactInformation> getContacts() {
|
||||
return contacts;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
version="2.0">
|
||||
<package>org.hibernate.test.annotations.embeddables.collection.xml</package>
|
||||
<entity class="Person">
|
||||
<attributes>
|
||||
<id name="id">
|
||||
<generated-value strategy="AUTO"/>
|
||||
</id>
|
||||
<version name="version"/>
|
||||
<element-collection name="contacts">
|
||||
<collection-table name="CONTACT_INFO">
|
||||
<join-column name="person_id"/>
|
||||
</collection-table>
|
||||
</element-collection>
|
||||
</attributes>
|
||||
</entity>
|
||||
<embeddable class="ContactInformation">
|
||||
<attributes>
|
||||
<basic name="name"/>
|
||||
<one-to-many name="contactType">
|
||||
<join-table name="CONTACT_TYPE">
|
||||
<join-column name="id"/>
|
||||
<inverse-join-column name="id"/>
|
||||
</join-table>
|
||||
</one-to-many>
|
||||
</attributes>
|
||||
</embeddable>
|
||||
<entity class="ContactType">
|
||||
<attributes>
|
||||
<id name="id">
|
||||
<generated-value strategy="AUTO"/>
|
||||
</id>
|
||||
<version name="version"/>
|
||||
<basic name="type"/>
|
||||
</attributes>
|
||||
</entity>
|
||||
</entity-mappings>
|
Loading…
Reference in New Issue