Hibernate version: 3.2.5.ga
Hibernate-annotations: 3.3.0.ga
Hibernate-entitymanager: 3.3.1.ga
MySQL 5.0.41
Hi!
I have a problem with embedding which I managed to cut down to a simple test case (I misused JUnit for it, could have also been a simple class with a main method).
In words: I have a Container with an optional 1:1-relation to a Containee which itself embedds an Embeddee. I first persist the container without a containee, detach it by closing and reopening the entity manager, then put a containee in the still detached container and eventually merge it back to the persistent context. There a PropertyAccessException is thrown. Source code is pretty simple. I left out package and import lines.
Is this a bug in hibernate or do we do something wrong?
Thanks!
Code:
public class EmbeddedTest extends TestCase {
private EntityManagerFactory emf;
private EntityManager em;
private EntityTransaction tx;
protected void setUp() throws Exception {
super.setUp();
emf = Persistence.createEntityManagerFactory("TEST_SE", map);
}
protected void tearDown() throws Exception {
}
public void testEmbedded() {
em = emf.createEntityManager();
tx = em.getTransaction();
tx.begin();
Container c = new Container();
// persist container entity with no containee
em.persist(c);
tx.commit();
int id = c.id;
c = em.find(Container.class, id);
// implicitely detach c by closing and reopening entitymanager
em.close();
em = emf.createEntityManager();
// manipulate detached c
c.containee = new Containee();
tx = em.getTransaction();
tx.begin();
// merge c into persistent context
Container cMerged = em.merge(c);
/*
* gives:
*
*javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of de.regiogmbh.inecos.framework.Embedee.string
*
**/
tx.commit();
em.close();
}
}
@Entity
@AccessType("field")
@Table(
name="container"
)
public class Container {
@Id
@GeneratedValue
int id;
@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER,optional=true)
Containee containee;
}
@Entity
@Table(
name="containee"
)
public class Containee {
@SuppressWarnings("unused")
@Id
@GeneratedValue
int id;
@Embedded
Embedee embedee;
}
@Embeddable
@AccessType("field")
public class Embedee {
String string = "string";
}