Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
Hibernate 3.2.5, Annotations 3.3.0
Mapping documents:
(using annotations)
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.rtrms.application.view.filter.FilterImpl
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:87)
at org.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:307)
at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:755)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1143)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:145)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.rtrms.persistence.hibernate.HibernateTransaction.commit(HibernateTransaction.java:123)
... 17 more
Name and version of the database you are using:
HSQL
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
The problem I have involves three classes, a top level entity called View, an embeddable object called Column, and another entity called Filter. The View contains a CollectionOfElements of Columns, and each Column has a ManyToOne reference to a Filter:
@javax.persistence.Entity()
@Entity(dynamicUpdate=true)
@Table(name="PORTFOLIO_VIEW")
class View {
....
@JoinTable(name="PORTFOLIO_VIEW_COLUMN",joinColumns={@JoinColumn(name="VIEW_ID")})
@IndexColumn(name="COLUMN_INDEX")
@Cascade(CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
@CollectionOfElements
private List<Column> columns;
....
}
@Embeddable
class Column {
....
@ManyToOne(fetch=FetchType.EAGER)
@Cascade(CascadeType.ALL)
@JoinColumn(name="FILTER")
@Target(FilterImpl.class)
private Filter filter;
....
}
@javax.persistence.Entity
@Entity(dynamicUpdate=true)
@Table(name="FILTER")
@DiscriminatorColumn(name="CLASS_NAME",length=255)
abstract class FilterImpl {
....
}
When I call save on a View which has a Column which has a Filter, I'd like that Filter to get saved (thus the CascadeType.ALL with the CollectionOfElements). It doesn't, and I get the TransientObjectException.
I'm not sure if there's an annotation that I'm missing, or whether Hibernate doesn't support this type of operation. I'm aware that I could remodel this so that Column is also an Entity, but I feel like that shouldn't be necessary. I should be able to tell components/embeddables to cascade to referenced entities.
Can anyone point me in the right direction?