0

Im confused with the JPA manytoone relationship, i have a class 'judgement' which has many 'judges', when i tried to store a judgement instance into postgres db, it threw error message as:

Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.dyihi.evaluation.model.Judgement.judge -> com.dyihi.evaluation.model.User

the judgement class:

@Entity
@Table(name = "JUDGEMENTS")
public class Judgement implements Serializable, Cloneable {

    /**
     * 
     */
    private static final long serialVersionUID = -7049957706738879274L;

    @ManyToOne(cascade = { CascadeType.REFRESH, CascadeType.DETACH})
    @JoinColumn(name = "user_id")
    private User judge;

and my service class:

public Long store(Judgement judgement) throws RepositoryException {

        EntityManager em = null;
        EntityTransaction tx = null;
        try {
            em = _emf.createEntityManager();
            tx = em.getTransaction();
            tx.begin();
            em.persist(judgement);
            tx.commit();
            return judgement.getId();
        }
        finally {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
            if (em != null) {
                em.close();
                em = null;
            }
        }
    }

and in my db i have tables Judgement and User, do i need to create a table as Judgement_User for the ManyToOne relationship?

4

1 回答 1

0

首先,如果如您所说,您正在寻找一个有很多法官的判决,那么您的情况就落后了。正如kem 在他的评论中所建议的那样,您现在编码的方式许多判断有一个判断,相反的是一个判断可能有很多判断。

在回答您的问题时,与您试图挽救的判决相关的法官本身并没有得救。您的级联定义不允许将持久性级联到法官中,因此JPA别无选择,只能抛出异常。您可以更改级联定义,或手动保存判断。如果法官是持久的,但分离的,那么您可能需要先将其合并以将其重新附加到实体管理器。

于 2012-05-17T21:01:22.323 回答