1

我有这个关系:

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

实体自动.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

有什么问题/错误?

4

2 回答 2

1

I think the reason is your embeddable classes are same. I didnt see any difference between PoaPK and AutPK, what about if you just use same embeddable class for all? If this doesnt work also you can try composite ID

于 2012-07-16T11:51:48.483 回答
0

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

于 2012-07-16T11:28:43.220 回答