英文:
Entity Mapping with One to One and One to Many Java/JPA/Hibernate
问题
需要在实体映射方面求助。我有一个Debtor实体,它与Addresses实体有一对多的映射关系。
我还有不同的地址类型,Debtor应该与每个地址类型都有一对一的映射关系。
每个地址类型都是Addresses的子类。在运行测试用例时,我得到以下错误:
"为类inquiry.entity.CurrentAddress提供了错误类型的id。期望:class java.lang.Integer,实际为class java.lang.String;
嵌套异常为java.lang.IllegalArgumentException:"
有人能帮忙解决如何映射这些实体吗?
@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> {
}
英文:
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> {
}
答案1
得分: 1
在你的 Debtor
类中有以下代码:
@OneToOne
@JoinColumn(name = "id", referencedColumnName = "parent_id")
private CurrentAddress currAddr;
@OneToOne
告诉 Hibernate 外键位于 Debtor 类本身中,列名为 "id",它是一个 String
类型。在你的 CurrentAddress
类中,它映射回 Addresses
,你有一个 @Id
字段,它是一个 int
类型。Hibernate 无法将一个 int
类型的主键与一个 String
类型的外键进行匹配,这导致了你遇到的错误。
关于 JoinColumn
注解的 name 属性的定义,可以从这里获取:
(可选)外键列的名称。它所在的表取决于上下文。
如果联接是使用外键映射策略进行的 OneToOne 或 ManyToOne 映射,
外键列位于源实体或可嵌入实体的表中。
英文:
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.<br><br>
> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论