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:
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?