I am doing using JPA annotations only, with Hibernate as the provider on all my Entities.
I am getting a problem with my @ManyToOne relationships saying that the "object references an unsaved transient instance - save the transient instance before flushing:"
here are my two classes:
Code:
@Entity
@Table(name = "MY_CONFIG")
public class MyConfig {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "CONFIG_SEQ")
@SequenceGenerator(name="CONFIG_SEQ", sequenceName = "CONFIG_SEQ")
@Column(name = "CONFIG_ID")
private Long configId;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="REG_ACTION_ID")
private MyRegAction myRegAction;
}
@Table(name = "MY_REG_ACTION")
public class MyRegAction {
@ID
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "REG_ID")
@SequenceGenerator(name="REG_ID", sequenceName = "REG_ID")
@Column(name = "REG_ACTION_ID")
private Long regActionId;
@OneToMany(mappedBy="myRegAction", cascade = CascadeType.ALL)
private List<MyConfig> regConfig;
Its an oracle DB with a sequence generated for both Objects. If I don't specifically set the ID in my Unit test for the MyRegAction I get the following error:
INFO object references an unsaved transient instance - save the transient instance before flushing: MyRegAction
INFO org.hibernate.TransientObjectException: object references an unsaved transient instance - save MyRegAction
INFO at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
INFO at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
INFO at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:242)
INFO at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:597)
INFO at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3128)
INFO at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:479)
INFO at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:204)
INFO at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:127)
INFO at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
INFO at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
INFO at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
INFO at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
INFO at xxx.HibernateDaoImpl.flush(HibernateDao.java:490)
INFO at ConfigDAOTest.readMConfig(ConfigDAOTest.java:51)
INFO at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
INFO at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
INFO at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
INFO at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
INFO at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
INFO at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
INFO at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
INFO
Obviously I shouldn't need to set any Id in my tests as they are all sequence generated Ids....so what is wrong?