[英]Entity Mapping with One to One and One to Many Java/JPA/Hibernate
Need help in entity mapping.在实体映射方面需要帮助。 I have Debtor entity and it has one to many mapping with Addresses entity.我有 Debtor 实体,它与 Addresses 实体有一对多的映射。 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:" "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.任何人都可以帮忙,如何 map 实体。
@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> {
}
In your Debtor
class you have this:在你的Debtor
class 你有这个:
@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
. @OneToOne
告诉 Hibernate 外键在债务人 class 本身中找到,列名为“id”,它是一个String
。 In your CurrentAddress
class, which is mapped back to Addresses
, you have a @Id
field, which is an int
.在映射回Addresses
的CurrentAddress
class 中,您有一个@Id
字段,它是一个int
。 Hibernate can't match an int
Primary Key with a String
Foreign Key, which results in the error you are getting. Hibernate无法将int
主键与String
外键匹配,这会导致您遇到错误。
Taken from here for the definition of the name attribute of the JoinColumn
annotation:取自这里对于JoinColumn
注解的 name 属性的定义:
(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.如果连接是针对使用外键映射策略的 OneToOne 或 ManyToOne 映射,则外键列在源实体的表中或可嵌入的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.