Hibernate version: 3
Domain Objects:
Code:
public class Account implements Serializable {
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
@Column(name = "account_no_in", nullable = false)
private Integer accountID;
@OneToOne(cascade = {CascadeType.REMOVE, CascadeType.MERGE, CascadeType.REFRESH}, mappedBy = "account")
private AccountImage image;
}
public class AccountImage implements Serializable {
@Id
@Column(name = "account_no_in", nullable = false)
private Integer accountID;
@JoinColumn(name = "account_no_in",referencedColumnName= "account_no_in", insertable = false, updatable = false)
@OneToOne
private Account account;
}
Code between sessionFactory.openSession() and session.close():Code:
EntityManager em = null;
Properties sysProps = System.getProperties();
Properties properties = new Properties();
properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
properties.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
properties.put("java.naming.provider.url", sysProps.getProperty("jboss.bind.address") + ":1099");
Context context = new InitialContext(properties);
em = (EntityManagerFactory)context.lookup(factoryJNDILookup);
int accountID = 1;
try {
em = factory.createEntityManager();
Object entity = em.find(AccountImage.class, accountID);
em.remove(entity);
em.flush();
} catch(Exception e) {
throw new ApplicationException(e);
} finally {
try { em.close();}catch(Exception d){};
}
Partial stack trace of any exception that occurs:Code:
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.petco.psm.configuration.domain.Account.image -> com.petco.psm.configuration.domain.AccountImage
at org.hibernate.engine.CascadingAction$9.noCascade(CascadingAction.java:353)
at org.hibernate.engine.Cascade.cascade(Cascade.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:131)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:122)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1004)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:342)
at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:523)
Database:
Informix
I hope that's enough information.
As you can see, an Account has a OneToOne relationship with the AccountImage. In the database, the AccountImage table has the AccountID as it's primary key. I want to be able to add/delete the AccountImage for a specific account, adding works fine, but deleting is not. We have constraints on the tables enforcing a OneToOne relationship.
I've also tried, getting the Account, setting the AccountImage property to null and persisting the Account back to the database, still doesn't work.
I'm fairly new to Hibernate, i have searched [and searched ] though for this similiar situation and haven't found anything. Am i going about this the wrong way?
Let me know if anymore clarification is needed.
Any help is appreciated.
Thank you