实体映射涉及一对一和一对多的Java/JPA/Hibernate。

huangapple go评论65阅读模式
英文:

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=&quot;debtors&quot;)
public class Debtor{
@Id
@Column(name = &quot;id&quot; , length = 127)
private String id;
@OneToOne
@JoinColumn(name = &quot;id&quot;, referencedColumnName = &quot;parent_id&quot;)
private CurrentAddressStd currAddrStd;
@OneToOne
@JoinColumn(name = &quot;id&quot;, referencedColumnName = &quot;parent_id&quot;)
private CurrentAddress currAddr;
@OneToMany
@JoinColumn(name = &quot;parent_id&quot;)
private List&lt;Addresses&gt; addresses;
}
@Entity
@Table(name=&quot;addresses&quot;)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING, name = &quot;type&quot;)
public class Addresses{
@Id
@GeneratedValue(strategy= GenerationType.SEQUENCE)
private int id;
@Any(metaColumn = @Column(name = &quot;parent_type&quot;))
@AnyMetaDef(idType = &quot;string&quot;, metaType = &quot;string&quot;,
metaValues = {
@MetaValue(targetEntity = Contact.class, value = &quot;Contact&quot;),
@MetaValue(targetEntity = Debtors.class, value = &quot;Debtor&quot;),
})
@JoinColumn(name = &quot;parent_id&quot;)
public Object parentItem;
}
@Entity
@DiscriminatorValue(&quot;CurrAddrStd&quot;)
public class CurrentAddressStd extends Addresses{
}
public interface AddressesRepository extends CrudRepository&lt;Addresses,Integer&gt; {
}

答案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 = &quot;id&quot;, referencedColumnName = &quot;parent_id&quot;)
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.

huangapple
  • 本文由 发表于 2020年8月17日 07:33:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63442879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定