I have a Parent class mapped as an entity.
Parent has a @ElementCollection on a Child class mapped as embeddable.
The child class has a OneToMany on a AnotherEntity class mapped as entity.
I'm using hibernate 3.5.1.Final (both for core and annotations)
When I instanciate the session factory, I encounter a ConcurrentModificationException.
The complete test case that is failing is at the bottom of this post.
Note that mapping child as an entity (and adding an id, of course) makes the test pass.
I think this is illogic since I using ElementCollection on an Entity is not supported (because the entity already has a table whereas the ElemetnCollection should trigger the creation of a table).
Please help,
Code:
package net.decalog.gabi.dao;
import java.sql.Driver;
import java.util.List;
import java.util.Properties;
import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Environment;
import org.junit.Test;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
public class EmbeddableWithOneToManyTest {
@Entity
public static class Parent {
private String id;
private List<Child> children;
@Id
public String getId() {
return this.id;
}
@ElementCollection
@CollectionTable(name = "CHILDREN", joinColumns = @JoinColumn(name = "PARENT_ID"))
@OrderBy("childNumber")
public List<Child> getChildren() {
return this.children;
}
public void setId(String id) {
this.id = id;
}
public void setChildren(List<Child> children) {
this.children = children;
}
}
@Embeddable
public static class Child {
private int childNumber;
private List<AnotherEntity> entities;
public int getChildNumber() {
return this.childNumber;
}
@OneToMany
@OrderBy("name")
public List<AnotherEntity> getEntities() {
return this.entities;
}
public void setChildNumber(int childNumber) {
this.childNumber = childNumber;
}
public void setEntities(List<AnotherEntity> entities) {
this.entities = entities;
}
}
@Entity
public static class AnotherEntity {
private String id;
private String name;
@Id
public String getId() {
return this.id;
}
public String getName() {
return this.name;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
@Test
public void test() throws Exception {
Driver driver = new org.hsqldb.jdbcDriver();
String url = "jdbc:hsqldb:mem:MY_DB";
DataSource ds = new SimpleDriverDataSource(driver, url, "sa", "");
AnnotationSessionFactoryBean sf_ = new AnnotationSessionFactoryBean();
sf_.setDataSource(ds);
sf_.setAnnotatedClasses(new Class[] { Parent.class, Child.class,
AnotherEntity.class });
Properties props = new Properties();
props.put(Environment.SHOW_SQL, "false");
props.put(Environment.HBM2DDL_AUTO, "create-drop");
props.put(Environment.DIALECT, "org.hibernate.dialect.HSQLDialect");
sf_.setHibernateProperties(props);
sf_.afterPropertiesSet();
SessionFactory sf = (SessionFactory) sf_.getObject();
}
}