-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Embedabble with a OneToMany relation is not working
PostPosted: Tue Jun 22, 2010 10:00 am 
Newbie

Joined: Tue Jun 22, 2010 9:35 am
Posts: 1
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();
   }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.