Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何创建具有相同实体的两个类?_Java_Hibernate_Jpa_Inheritance - Fatal编程技术网

Java 如何创建具有相同实体的两个类?

Java 如何创建具有相同实体的两个类?,java,hibernate,jpa,inheritance,Java,Hibernate,Jpa,Inheritance,让我们假设我有一个表person,我有一个实体类,有很多字段,一些@暴露的,一些@暂时的,一些@暂时的列。问题是加载nPerson项需要很长时间,因为每个记录都有一些字段会降低性能 作为优化的想法,我创建了一个PersonSimplified类,定义了Person的所有成员,除了“slow”字段和方法。一切工作都完美无缺,但不幸的是,这个解决方案不是很优雅,因为像name这样的东西都被重新定义了,而代码重复会导致灾难。我的想法是确保Person扩展PersonSimplified,但不知道如何做

让我们假设我有一个
person
,我有一个实体类,有很多字段,一些
@暴露的
,一些
@暂时的
,一些
@暂时的
列。问题是加载n
Person
项需要很长时间,因为每个记录都有一些字段会降低性能

作为优化的想法,我创建了一个
PersonSimplified
类,定义了
Person
的所有成员,除了“slow”字段和方法。一切工作都完美无缺,但不幸的是,这个解决方案不是很优雅,因为像
name
这样的东西都被重新定义了,而代码重复会导致灾难。我的想法是确保
Person扩展PersonSimplified
,但不知道如何做到这一点。我有三个重要的课程,我将在这里给出它们的骨架:

BaseEntity.java

@MappedSuperclass
@EntityListeners({BaseEntityListener.class})
public abstract class BaseEntity implements Cloneable {
    //Some fields and methods
}
PersonSimplified.java

@SuppressWarnings("serial")
@Entity(name="PersonSimplified")
@Table(name="person")
@Inheritance(strategy=InheritanceType.JOINED)
public class PersonSimplified extends BaseEntity implements Auditable, Serializable {
    //Lots of stuff
}
@SuppressWarnings("serial")
@Entity(name="PersonSimplified")
@Table(name="person")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class PersonSimplified extends BaseEntity implements Auditable, Serializable {
    //Lots of stuff
}
Person.java

@SuppressWarnings("serial")
@Entity(name="Person")
@Table(name="person")
@Inheritance(strategy=InheritanceType.JOINED)
public class Person extends PersonSimplified implements Auditable, Serializable {
    //Some slow stuff
}
@SuppressWarnings("serial")
@Entity(name="Person")
@Table(name="person")
public class Person extends PersonSimplified implements Auditable, Serializable {
    //Some slow stuff
}
我得到的错误是:

外键循环依赖关系涉及下表:person,person


但是,我不打算拥有任何外键。我只想重新组织代码,以确保当我需要一个简化的人时,它是可用的,当我需要它以及慢速字段时,那么我也可以实现这一点,并且我不必重复我的代码。如何实现这一点?

我认为您的问题与@heritation有关,请尝试以下方法:

PersonSimplified.java

@SuppressWarnings("serial")
@Entity(name="PersonSimplified")
@Table(name="person")
@Inheritance(strategy=InheritanceType.JOINED)
public class PersonSimplified extends BaseEntity implements Auditable, Serializable {
    //Lots of stuff
}
@SuppressWarnings("serial")
@Entity(name="PersonSimplified")
@Table(name="person")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class PersonSimplified extends BaseEntity implements Auditable, Serializable {
    //Lots of stuff
}
Person.java

@SuppressWarnings("serial")
@Entity(name="Person")
@Table(name="person")
@Inheritance(strategy=InheritanceType.JOINED)
public class Person extends PersonSimplified implements Auditable, Serializable {
    //Some slow stuff
}
@SuppressWarnings("serial")
@Entity(name="Person")
@Table(name="person")
public class Person extends PersonSimplified implements Auditable, Serializable {
    //Some slow stuff
}
您必须知道,在引擎盖下,Hibernate正在为Person生成一个鉴别器列和鉴别器值

编辑

实际的解决方案是创建一个中间
,它扩展了
基本实体
,并从中扩展了其他两个类:

@MappedSuperclass
@EntityListeners({BaseEntityListener.class})
public abstract class BasePerson extends BaseEntity {
    //Everything which was part of PersonSimplified
}
然后我确保扩展这个

@SuppressWarnings("serial")
@Entity(name="PersonSimplified")
@Table(name="person")
@Inheritance(strategy=InheritanceType.JOINED)
public class PersonSimplified extends BasePerson implements Auditable, Serializable {

    public PersonSimplified() {
    }

}


结果非常好,优化非常成功。

嗨,Villat,我尝试了你的建议,但出现了以下错误:“字段列表”中的未知列“this_uu.DTYPE”,很抱歉回复太慢,昨天解决了这个问题。我从BaseEntity继承了一个名为BasePerson的新类,设置为@MappedSuperclass@EntityListeners({BaseEntityListener.class})并将所有内容从PersonSimplify移动到BasePerson。最后,我确保PersonSimplify和Person扩展了BasePerson。由于我发现的所有源代码对我的案例都没有帮助,我已切换到方案B,并在BaseEntity和实际实体类之间创建了一个新类,因此这个新类包含了com的所有内容mon对于任何人来说,基本上PersonSimplified包含的所有内容。@LajosArpad太棒了!我考虑过这种方法,但它不是你想要的。请在这篇文章中分享你的答案:)我已经相应地编辑了你的答案,以确保未来的读者知道如何解决它。