简体   繁体   中英

JPA Annotation Error with OneToOne and composite primary key

I have this relation:

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

TTR_AUT and TTR_POA have a oneToOne relation. And now I wont to get an object Aut with VALOR from POA. With the next entities:

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
}

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

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

When I get the object Poa, it throws this error:

]] 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

What's the problem/error?

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

Maybe you need define different serialVersionUID for PoaPK and AutPK. Change serialVersionUID of PoaPK to 2 or any other number other than 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM