Hibernate Core version: 3.3.1 GA
Hibernate Annotation version: 3.4.0 GA
Hello,
I would like to use CascadeType.ALL or CascadeType.PERSIST for the moment with @Any annotation.
This is what I have in my Entity :
Please note that I create MyBook directly in getMyObject() for test only :). Of course, MyBook and MyDvd implement MyObject's interface.
Code:
MyEntity{
...
@Any(fetch=FetchType.EAGER , metaColumn = @Column(name = "ITEM_TYPE"))
@AnyMetaDef(idType = "long", metaType = "string",
metaValues = {
@MetaValue(targetEntity = MyBook.class, value = "B"),
@MetaValue(targetEntity = MyDvd.class, value = "D"),
})
@JoinColumn(name="ITEM_ID")
@Cascade(value={CascadeType.ALL, CascadeType.DELETE_ORPHAN })
public MyObject getMyObject() {
MyBook book = new MyBook();
book.setBookValue("My Book value!!");
return book;
}
...
}
To persist I do (I use EntityManager):
Code:
//Here MyEntity is created and ready for persist
entityManager.persist(myEntity)
hibernate trace in debug mode:
Code:
15:33:52,171 INFO [STDOUT] Hibernate:
insert
into
MyBook
(id, bookValue, name, version)
values
(null, ?, ?, ?)
15:33:52,171 INFO [STDOUT] Hibernate:
call identity()
15:33:52,171 INFO [STDOUT] Hibernate:
insert
into
MyEntity
(id, code, ITEM_TYPE, ITEM_ID)
values
(null, ?, ?, ?)
15:33:52,171 INFO [STDOUT] Hibernate:
call identity()
15:33:52,203 INFO [STDOUT] Hibernate:
insert
into
MyBook
(id, bookValue, name, version)
values
(null, ?, ?, ?)
15:33:52,203 INFO [STDOUT] Hibernate:
call identity()
15:33:52,218 INFO [STDOUT] Hibernate:
update
MyEntity
set
code=?,
ITEM_TYPE=?,
ITEM_ID=?
where
id=?
I got exception :Code:
ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.package.entity.MyBook
Do you have any suggestion? Thank you all!
Max