简体   繁体   English

如何在JPA中使用联接的子实体获取继承实体?

[英]How to get inheritance entity by using joined sub entity in jpa?

Writing the case it will be more simple to explain. 编写案例将更容易解释。

I am using Seam 2.3.1 v Hibernate JPA 2.0 and in our project. 我在我们的项目中使用Seam 2.3.1 v Hibernate JPA 2.0。 I have a base Person Entity Class. 我有一个基本的Person Entity Class。

@Entity
@Name("person")
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "person")
public class Person extends BaseEntity  {

    private String name;
    private String surName;
    private String email;
    private String phone;

    // getter & setters
}

And I have 3 more Entity extends from Person as @Inheritance(strategy = InheritanceType.JOINED) : Personel , Musteri , DisPaydas 我有3个实体从人扩展为@Inheritance(strategy = InheritanceType.JOINED) PersonelMusteriDisPaydas

@Entity
@Name("personel")
public class Personel extends Person {

    private String appellation; 
    // getter & setters
}

I want to List personels, musteris and dispaydas in my bean however when I set them in my Group Entity, I want to save them as Person. 我想在我的bean中列出人员,鼬和disdisdas,但是当我在组实体中设置它们时,我想将它们另存为Person。

In fact in DB there is no difference between person_id and personel_id, they are same. 实际上,在DB中person_id和personel_id之间没有区别,它们是相同的。 However when I listing it, they are List<Personel> 但是,当我列出它时,它们是List<Personel>

In summary: 综上所述:

I want to get List<Person> from List<Personel> or Person from Personel object. 我想从List<Personel>获得List<Person> List<Personel>或从Personel对象获得Person

You're going to have to define an @Id and @Column in the Person class for the person_id . 您将必须在Person类中为person_id定义一个@Id@Column

However, since the column has a different name in the child entity, you'll need to change it using an @AttributeOverride to point to the personel_id @Column . 然而,由于该列中的子实体具有不同的名称,你需要使用去改变它@AttributeOverride指向personel_id @Column

It couldn't hurt to use a @DiscriminatorColumn in the parent and @DiscriminatorValue in the children, but I don't think Hibernate requires those. 它不能伤害到使用@DiscriminatorColumn父和@DiscriminatorValue的孩子,但我不认为Hibernate对那些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何过滤 JPA 中的连接实体 - How to filter a joined entity in JPA 如何使用JPA(Hibernate)在连接表上查找条件的实体? - How to find entity using JPA(Hibernate) with condition on joined tables? 无法使用JPA(Hibernate)在连接的继承链中按根类加载实体 - Cannot load entity by root class in joined inheritance chain with JPA (Hibernate) 在 joined inheritance 策略中查询基础实体怎么可能也获取子实体? - How is it possible that querying base entity in joined inheritance strategy also fetches sub entities? 使用 JPA Criteria,如何在不获取已加入实体的情况下获取已加入实体的子实体? - With JPA Criteria, how would I Fetch a child entity of a Joined entity without Fetching the Joined entity? JPA @Entity Inheritance - JPA @Entity Inheritance JPA实体映射继承 - JPA Entity Mapping Inheritance 使用带有&#39;wild&#39;DiscriminatorValue的DiscriminatorColumn的JPA实体继承 - JPA Entity inheritance using DiscriminatorColumn with a 'wild' DiscriminatorValue 如何使用 JPA 获取非相关实体 - How to get non related entity using JPA 使用JOINED继承策略并设置spring.jpa.properties.hibernate.jdbc.batch_size时部分保存的实体 - Entity partially saved when using JOINED inheritance strategy and setting spring.jpa.properties.hibernate.jdbc.batch_size
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM