OneToOne和复合主键的JPA注释错误

时间:2012-07-16 11:03:42

标签: java hibernate jpa spring-mvc annotations

我有这种关系:

+------------------+    +----------------+    +----------+
| TTR_AUT          |    | TTR_ANO_ESCALA |    | TTR_POA  |
+------------------+    +----------------+    +----------+
|#ID_ESCAL         |    |#ID_ESCAL       |    |#ID_ESCAL |
|#ANO              |----|#ANO            |----|#ANO      |
|#MES              |    | NOMBRE         |    |#MES      |
| OBSERVACIONES    |    +----------------+    | VALOR    |
+------------------+                          +----------+

TTR_AUT和TTR_POA具有oneToOne关系。现在我不会从POA获得一个带有VALOR的对象Aut。下一个实体:

实体Aut.java

@Entity
@Table(name="TTR_AUT")
public class Aut implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private AutPK id;

    @Column(name="OBSERVACIONES")
    private String observaciones;

    @OneToOne
    @JoinTable(name="TTR_POA",
        joinColumns = {
          @JoinColumn(name="ANO", insertable=false, updatable=false, referencedColumnName="ANO"),
          @JoinColumn(name="ID_ESCAL", insertable=false, updatable=false, referencedColumnName="ID_ESCAL"),
          @JoinColumn(name="MES", insertable=false, updatable=false, referencedColumnName="MES")          
        },
        inverseJoinColumns = {
          @JoinColumn(name="ANO", insertable=false, updatable=false, referencedColumnName="ANO"),
          @JoinColumn(name="ID_ESCAL", insertable=false, updatable=false, referencedColumnName="ID_ESCAL"),
          @JoinColumn(name="MES", insertable=false, updatable=false, referencedColumnName="MES")
        }     
      )
    private Poa poa;

    public Aut() {
    }

    //GETTERS AND SETTERS
}

实体AutPK.java

@Embeddable
public class AutPK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(name="ID_ESCAL", unique=true, nullable=false, precision=22)
    private Long idEscal;

    @Column(name="ANO", unique=true, nullable=false, precision=22)
    private Long ano;

    @Column(name="MES", unique=true, nullable=false, precision=22)
    private Long mes;

    public AutPK() {
    }

    //GETTERS AND SETTERS
}

实体Poa.java

@Entity
@Table(name="TTR_POA")
public class Poa implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private PoaPK id;

    @Column(name="VALOR", precision=22)
    private BigDecimal valor;

    public Poa() {
    }

    //GETTERS AND SETTERS
}

实体PoaPK.java

@Embeddable
public class PoaPK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(name="ID_ESCAL", unique=true, nullable=false, precision=22)
    private Long idEscal;

    @Column(unique=true, nullable=false, precision=22)
    private Long ano;

    @Column(unique=true, nullable=false, precision=22)
    private Long mes;

    public PoaPK() {
    }

    //GETTERS AND SETTERS
}

当我得到Poa对象时,会抛出此错误:

]] Root cause of ServletException.
java.lang.IllegalArgumentException: org.hibernate.TypeMismatchException: Provided id of the wrong type for class es.model.entities.Poa. Expected: class es.model.entities.PoaPK, got class es.model.entities.AutPK
    at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:314)
    at es.model.entities.dao.impl.AutDAOImpl.getAutDeterminada(AutDAOImpl.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    Truncated. see log file for complete stacktrace
Caused By: org.hibernate.TypeMismatchException: Provided id of the wrong type for class es.model.entities.Poa. Expected: class es.model.entities.PoaPK, got class es.model.entities.AutPK
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:132)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1079)
    at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1006)
    at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:613)
    at org.hibernate.type.EntityType.resolve(EntityType.java:441)
    Truncated. see log file for complete stacktrace

问题/错误是什么?

2 个答案:

答案 0 :(得分:1)

我认为原因是你的可嵌入类是一样的。我没有看到PoaPK和AutPK之间有什么区别,如果你只使用相同的嵌入类呢?如果这不起作用,您可以尝试composite ID

答案 1 :(得分:0)

也许您需要为PoaPK和AutPK定义不同的serialVersionUID。 将PoaPK的serialVersionUID更改为2或除1之外的任何其他数字。