-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Deleting Obj1 that's in OneToOne rel. with Obj2
PostPosted: Wed Dec 10, 2008 8:41 pm 
Newbie

Joined: Wed Dec 10, 2008 7:38 pm
Posts: 1
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.