简体   繁体   English

Spring JPA抽象类行为

[英]Spring JPA abstract class behavior

I have question, what am I doing wrong, my entities are displayed not like I would like, when I go to the h2-console I can see 3 tables, persons employee manager when I go to the persons table I see all persons that extend this abstract class ( employee , manager ), but when I go to the other tables I can only see id of this classes. 我有问题,什么我做错了,我的实体都显示不一样,我想,当我去到h2-console我可以看到3个表, persons employee manager ,当我去到persons表我看到延长所有人这个abstract类( employeemanager ),但是当我转到其他表时,我只能看到此类的id My question is how to fix it, and also how to make each table to display the id staring from 1 because now the employee table has 3 ids 1,2,3 and the manager table has 4,5 , the person table is displayed correctly. 我的问题是如何修复它,以及如何使每个表显示从1的ID,因为现在employee表具有3个ID 1,2,3 ,manager表具有4,5 ,person表正确显示。 I would like to display 3 employees in the employee table, 2 managers in the managers table, and each entity in the persons table, and the ids staring counting from 1 in every table. 我想在employee表中显示3名雇员,在managers表中显示2位经理,在persons表中显示每个实体,并且每个表中的ID都从1开始计数。

Abstract class Person 抽象类人

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "descriminatorColumn")
@Table(name = "persons")
public abstract class Person {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

@Column(name = "first_name")
private String firstName;

@Column(name = "last_name")
private String lastName;

@Column(name = "email")
private String address;

@Column(name = "age")
private int age;

public Person(String firstName, String lastName, String address, int age) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.address = address;
    this.age = age;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

@Override
public String toString() {
    return "Person{" +
            "firstName='" + firstName + '\'' +
            ", lastName='" + lastName + '\'' +
            ", address='" + address + '\'' +
            ", age=" + age +
            '}';
  }
}

Manager class extends Person abstract class 经理类扩展了人员抽象类

@Entity
@Table(name = "managers")
public class Manager extends Person {

public Manager(String firstName, String lastName, String email, int age) {
    super(firstName, lastName, email, age);
}

@Override
public String toString() {
    return super.toString();
 }
}

Employee class extends Person abstract class 员工类扩展了人员抽象类

@Entity
@Table(name = "employees")
public class Employee extends Person {

public Employee(String firstName, String lastName, String address, int age) {
    super(firstName, lastName, address, age);
}

@Override
public String toString() {
    return super.toString();
  }
}

My question is how to fix it 我的问题是如何解决

There is absolutely nothing to fix as by using JOINED inheritance type it is doing exactly what you have specified, viz: 绝对没有什么可以修复的,因为使用JOINED继承类型可以完全按照您指定的方式进行操作,即:

  • save the data common to employeee and manager in persons table. 将员工和经理共同的数据保存在人员表中。
  • save the the data specific to employees in the employees table. 将特定于员工的数据保存在employee表中。
  • save the data specific to managers in the managers table. 将特定于经理的数据保存在manager表中。

https://en.wikibooks.org/wiki/Java_Persistence/Inheritance#Joined,_Multiple_Table_Inheritance https://zh.wikibooks.org/wiki/Java_Persistence/Inheritance#Joined,_Multiple_Table_Inheritance

Currently Employee and Manager define no additional fields and so only the IDs exist in these tables. 当前,Employee和Manager没有定义其他字段,因此这些表中仅存在ID。

If that is not to change then you would probably be better off using SINGLE_TABLE inheritance with a discriminator column so all the data will be in 1 table. 如果那没有改变,那么最好将SINGLE_TABLE继承与鉴别列一起使用,这样所有数据都将放在1个表中。

https://en.wikibooks.org/wiki/Java_Persistence/Inheritance#Example_single_table_inheritance_table_in_database https://zh.wikibooks.org/wiki/Java_Persistence/Inheritance#Example_single_table_inheritance_table_in_database

The final option is not to have any inheritance at all at the database level ie have 2 tables employees and managers which define all fields for each type. 最后的选择是在数据库级别完全没有继承,即拥有2个表employee和manager,它们定义每种类型的所有字段。 You can do that using @MappedSuperclass which prevents the need to duplicate the JPA mappings in the Java class hierarchy. 您可以使用@MappedSuperclass做到这@MappedSuperclass ,从而避免了在Java类层次结构中重复JPA映射的需要。

https://en.wikibooks.org/wiki/Java_Persistence/Inheritance#Mapped_Superclasses https://zh.wikibooks.org/wiki/Java_Persistence/Inheritance#Mapped_Superclasses

but be aware with this approach you cannot query for all persons but only for managers and employees separately. 但是请注意,使用这种方法时,您无法查询所有人,而只能分别查询经理和员工。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM