【问题标题】:JPA Annotation Error with OneToOne and composite primary keyOneToOne 和复合主键的 JPA 注释错误
【发布时间】:2012-07-15 05:18:44
【问题描述】:

我有这种关系:

+------------------+    +----------------+    +----------+
| 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。与下一个实体:

Entity 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
}

Entity 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

有什么问题/错误?

【问题讨论】:

    标签: java hibernate jpa spring-mvc annotations


    【解决方案1】:

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

    【讨论】:

    • 谢谢!我已经尝试对两者都使用可嵌入类,但我收到错误 ORA-00918。使用复合 ID 它可以工作,但我可以为老板任务更改此设置
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-05
      • 1970-01-01
      • 2016-05-26
      • 2013-08-20
      • 2011-09-18
      • 1970-01-01
      相关资源
      最近更新 更多