Provided id of the Wrong type for class - [JPA] Embeddableid formed by another Embeddable

Asked

Viewed 54 times

0



On the bench I have two tables called Table and Tabub.

To Table is composed of a primary key, which is composed of 3 attributes.

To Tabub is composed of a primary key, which is composed of 3 attributes that are also foreign key, pointing to the 3 primary key attributes of the Table.

The goal is to make the primary key of the Tabub point to the primary key of the Table. Just like in the picture:

inserir a descrição da imagem aqui

The mapping of Table is as follows:

@Entity
@Table(name="TabelaA")
public class TabelaA {

    @EmbeddedId
    private TabelaAPK pk;

    @OneToOne(mappedBy = "tabelaA")
    private TabelaB tabelaB;

}


@Embeddable
public class TabelaAPK implements Serializable {

    private static final long serialVersionUID = -7143941209658519625L;

    @Column(name = "atributo1")
    private Long atributo1;

    @Column(name = "atributo2")
    private String atributo2;

    @Column(name = "atributo3")
    private Long atributo3;

    public TabelaAPK(Long atributo1, String atributo2, Long atributo3) {
        this.atributo1 = atributo1;
        this.atributo2 = atributo2;
        this.atributo3 = atributo3;
    }

    public TabelaAPK() {}

}


The mapping of Tabub is as follows:

@Table(name = "TabelaB")
@Entity
@Data
public class TabelaB{

    @EmbeddedId
    private TabelaBPK pk;

    @OneToOne
    @PrimaryKeyJoinColumns(value = {
        @PrimaryKeyJoinColumn(name = "atributo1", referencedColumnName = "atributo1"),
        @PrimaryKeyJoinColumn(name = "atributo2", referencedColumnName = "atributo2"),
        @PrimaryKeyJoinColumn(name = "atributo3", referencedColumnName = "atributo3")
    })
    private TabelaA tabelaA;
}


@Embeddable
@NoArgsConstructor
public class TabelaBPK implements Serializable {

    private static final long serialVersionUID = -4788476806371092848L;

    @Embedded
    private TabelaAPK tabelaAPK;

    public TabelaBPK(TabelaAPK tabelaAPK) {
        this.tabelaAPK= tabelaAPK;
    }

}


As you can see, the object Tabelabpk has the object Tabelaapk as an attribute @Embedded.

The purpose of this is to make sure that if a change is made to the object Tabelaapk this change is reflected in the object Tabelabpk.

Problem: When I do a database search through the code box below, I get the following error:

Caused by: org.hibernate.TypeMismatchException: Provided id of the wrong type for class TabelaA. Expected: class TabelaAPK, got class TabelaBPK.

Search code:

[...]

String ql = 
        String.format("SELECT b FROM TabelaB b "
                    + "WHERE b.pk.canalPK.atributo1 = %d"
                    + " AND b.pk.pk.atributo2 = '%s'"
                    + " AND b.pk.pk.atributo3 = %d", 
                atributo1, atributo2, atributo3);

return em.createQuery(ql, TabelaB .class).getSingleResult();

[...]


  • What is the problem?
  • How can I fix it?


1 answer

0


After a night’s sleep I found the solution.

The reason of the error was that the primary key type of the class used in the relation @PrimaryKeyJoinColumns (in the case, the class Table) has to be the same type used in the primary key of the owner class of the relation (in this case, the Tabub).

In my case I was trying to use as the primary key of the class Tabub a class Tabelabpk which is formed by the primary key of the class Table, to Tabelaapk, but that won’t work because the class Tabelaapk must be a direct primary key of the class Tabub.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.