@OneToOne或@ManyToOne在x.person上引用一个未知的实体:y.Person - 继承问题 [英] @OneToOne or @ManyToOne on x.person references an unknown entity: y.Person - inheritance issue

查看:366
本文介绍了@OneToOne或@ManyToOne在x.person上引用一个未知的实体:y.Person - 继承问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Hibernate架构出了问题:

我有一个MappedSuperClass Person,一个Employee和一个Customer。

   - > Person.class 
@MappedSuperclass
@Audited
public class Person extends PersistentObject {

@Column(name =TITLE)
@Enumerated(EnumType .STRING)
私人标题标题;

@Column(name =FIRST_NAME)
private String fname = null;

@Column(name =LAST_NAME)
private String lname = null;

- > Employee.class
@Entity
@Table(name =TBL_EMPLOYEE)
@Audited
public class Employee extends Person {

- > Customer.class
@Entity
@Table(name =TBL_CUSTOMER)
@Audited
public class Customer extends Person {

这很好,但现在我的问题是:我已经使用项目列表(例如地址/联系人信息/ etc)扩展'Person',并使用所有地址相同的单一表(所有地址)。



仅将地址添加到客户时,这是没有问题的:

   - > Customer.java 
@OneToMany(cascade = {CascadeType.ALL},mappedBy =customer)
@LazyCollection(LazyCollectionOption.FALSE)
private List< Address>地址= null;

- > Address.java
@Entity
@Table(name =TBL_ADDRESSES)
@Audited
public class Address extends PersistentObject {
@ManyToOne(fetch = FetchType.LAZY )
@JoinColumn(name =CUSTOMER_ID)
私人客户客户;

现在我想将地址扩展到Employee。我认为,我可以使用超类型来保存地址,但这似乎并不正确:

   - > Person.java 
@OneToMany(cascade = {CascadeType.ALL},mappedBy =person)
@LazyCollection(LazyCollectionOption.FALSE)
private List< Address>地址= null;

- > Address.java
@Entity
@Table(name =TBL_ADDRESSES)
@Audited
public class Address extends PersistentObject {
@ManyToOne(fetch = FetchType.LAZY )
@JoinColumn(name =PERSON_ID)
私人人物;

运行时,出现以下错误: @OneToOne或@ManyToOne on at.test.Address.person引用一个未知实体:at.test.Person

是否有可能使两个实体使用相同的表的地址? ID不会冲突,因为员工和客户共享相同的顺序。或者我应该以另一种方式设计这个问题?



非常感谢您的提前和最好的问候!

解决方案

从Flo的链接中,我意识到用于解决问题的正确范例是每个子类一个表。感谢您的帮助!


i've got a problem with my Hibernate Architecture:

I have a MappedSuperClass Person, and an Employee and a Customer.

--> Person.class
@MappedSuperclass
@Audited
public class Person extends PersistentObject {

    @Column(name="TITLE")
    @Enumerated(EnumType.STRING)
    private Title title;

    @Column(name="FIRST_NAME")
    private String  fname       = null;

    @Column(name="LAST_NAME")
    private String  lname       = null;

--> Employee.class
@Entity
@Table(name="TBL_EMPLOYEE")
@Audited
public class Employee extends Person {

--> Customer.class
@Entity
@Table(name="TBL_CUSTOMER")
@Audited
public class Customer extends Person {

This works well, but now to my Problem: I've extended 'Person' with an List of Items, eg Addresses/Contact Informations/etc, and use a Single Table for all Items of the same Type (all Addresses).

When adding the Address to Customer only, this is no problem:

--> Customer.java
@OneToMany(cascade = {CascadeType.ALL}, mappedBy="customer")
@LazyCollection(LazyCollectionOption.FALSE)
private List<Address> addresses = null;

--> Address.java
@Entity
@Table(name="TBL_ADDRESSES")
@Audited
public class Address extends PersistentObject {
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="CUSTOMER_ID")
    private Customer customer;

Now i'd like to extend the addresses to the Employee also. I thought, that i could use the Supertype to hold the address, but this doesn't seem to work right:

--> Person.java
@OneToMany(cascade = {CascadeType.ALL}, mappedBy="person")
@LazyCollection(LazyCollectionOption.FALSE)
private List<Address> addresses = null;

--> Address.java
@Entity
@Table(name="TBL_ADDRESSES")
@Audited
public class Address extends PersistentObject {
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="PERSON_ID")
    private Person person;

When running, i get the following error: @OneToOne or @ManyToOne on at.test.Address.person references an unknown entity: at.test.Person

Is there a possibility to have both entities use the same table for their addresses? IDs wont conflict, because Employee and Customer share the same sequence. Or should i design this problem in another way?

Thank you very much in advance and best regards!

解决方案

From Flo's link I realized that the correct paradigm to use for my problem was the "one table per subclass" one. Thanks for the help!

这篇关于@OneToOne或@ManyToOne在x.person上引用一个未知的实体:y.Person - 继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆