如何使用JPA

时间:2017-05-12 08:32:49

标签: java hibernate oracle11g jpa-2.0

我正在使用SpringHibernateJPA 2.xOracle作为数据库开发经典的Web应用程序。 我有一个@ManyToMany关系,当我尝试加载它时会遇到异常。 Demande.java:

@Entity
@Table(name = "T_DEMANDE_AUTORISATION")
public class DemandeAutorisationEntity implements java.io.Serializable {

    @Id
    private Long id;
    @ManyToOne
    @JoinColumn(name = "VAR_ID", nullable = false)
    private VarianteEntity variante;
 ....
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "T_DEMANDE_QUESTION", joinColumns = {
            @JoinColumn(name = "DEMAUT_ID", nullable = false, updatable = false) }, inverseJoinColumns = {
                    @JoinColumn(name = "DEMQUEST_ID", nullable = false, updatable = false) })
    private Set<VarQuestionEntity> varQuestions;

//Getters and Setters

Question.java:

@Entity
@Table(name = "T_VAR_QUESTION")
@Immutable
public class VarQuestionEntity implements Serializable {

@Id
private Long id;
@ManyToOne
@JoinColumn(name = "VAR_ID", nullable = false)
private VarianteEntity variante;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "varQuestions")
private Set<DemandeAutorisationEntity> demandeAutorisations;
//Getters and Setters

查询:

@Override
public List<DemandeAutorisationEntity> searchDemandes(DemandeAutorisationSearch search) {
     StringBuilder sb = new StringBuilder();
     sb.append("from DemandeAutorisationEntity demande ")
       .append(" INNER JOIN FETCH demande.varQuestions varQuestions");
     Query query = em.createQuery(sb.toString());

     return query.getResultList();
}

Demande和Question都有@ManyToOne Variante。 当我执行查询时,我得到了这个例外:

Caused by: javax.persistence.PersistenceException: org.hibernate.HibernateException: identifier of an instance of fr.pasteur.gaip.dal.parameters.VarQuestionEntity was altered from 1 to null
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)
    at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:67)
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)
    ... 74 more
Caused by: org.hibernate.HibernateException: identifier of an instance of fr.pasteur.gaip.dal.parameters.VarQuestionEntity was altered from 1 to null
    at org.hibernate.event.internal.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:64)
    at org.hibernate.event.internal.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:175)
    at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:135)
    at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:216)
    at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:85)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:38)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1282)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:465)
    at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:2963)
    at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2339)
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:147)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231)
    at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65)
    at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:61)
    ... 75 more

0 个答案:

没有答案