Reputation: 753
Need help in entity mapping. I have Debtor entity and it has one to many mapping with Addresses entity. I also have different address types and Debtor should have one to one mapping with each address type. Each address type is a subclass of Addresses. While running test case I am getting below error
"Provided id of the wrong type for class inquiry.entity.CurrentAddress. Expected: class java.lang.Integer, got class java.lang.String; nested exception is java.lang.IllegalArgumentException:"
Can anyone help, how to map the entities.
@Entity
@Table(name="debtors")
public class Debtor{
@Id
@Column(name = "id" , length = 127)
private String id;
@OneToOne
@JoinColumn(name = "id", referencedColumnName = "parent_id")
private CurrentAddressStd currAddrStd;
@OneToOne
@JoinColumn(name = "id", referencedColumnName = "parent_id")
private CurrentAddress currAddr;
@OneToMany
@JoinColumn(name = "parent_id")
private List<Addresses> addresses;
}
@Entity
@Table(name="addresses")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING, name = "type")
public class Addresses{
@Id
@GeneratedValue(strategy= GenerationType.SEQUENCE)
private int id;
@Any(metaColumn = @Column(name = "parent_type"))
@AnyMetaDef(idType = "string", metaType = "string",
metaValues = {
@MetaValue(targetEntity = Contact.class, value = "Contact"),
@MetaValue(targetEntity = Debtors.class, value = "Debtor"),
})
@JoinColumn(name = "parent_id")
public Object parentItem;
}
@Entity
@DiscriminatorValue("CurrAddrStd")
public class CurrentAddressStd extends Addresses{
}
public interface AddressesRepository extends CrudRepository<Addresses,Integer> {
}
Upvotes: 2
Views: 1340
Reputation: 7865
In your Debtor
class you have this:
@OneToOne
@JoinColumn(name = "id", referencedColumnName = "parent_id")
private CurrentAddress currAddr;
The @OneToOne
is telling Hibernate that the foreign key is found in the Debtor class itself, with column name "id", which is a String
. In your CurrentAddress
class, which is mapped back to Addresses
, you have a @Id
field, which is an int
. Hibernate can't match an int
Primary Key with a String
Foreign Key, which results in the error you are getting.
Taken from here for the definition of the name attribute of the JoinColumn
annotation:
(Optional) The name of the foreign key column. The table in which it is found depends upon the context.
If the join is for a OneToOne or ManyToOne mapping using a foreign key mapping strategy, the foreign key column is in the table of the source entity or embeddable.
Upvotes: 1