Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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
Inheritance JPA使用现有超类对象创建对象_Inheritance_Jpa_Merge - Fatal编程技术网

Inheritance JPA使用现有超类对象创建对象

Inheritance JPA使用现有超类对象创建对象,inheritance,jpa,merge,Inheritance,Jpa,Merge,我对我的JPA项目有点意见。我有两个班,员工班和会员班。这对JPA来说很好,但当我试图使用现有的Person对象(实际上是一个成员)合并一名员工以创建一名员工时,我遇到了重复条目异常。我如何处理这个问题?我不知道 @Entity @Inheritance(strategy = InheritanceType.JOINED) public class Person implements Serializable{ @Id @GeneratedValue(strategy =

我对我的JPA项目有点意见。我有两个班,员工班和会员班。这对JPA来说很好,但当我试图使用现有的Person对象(实际上是一个成员)合并一名员工以创建一名员工时,我遇到了重复条目异常。我如何处理这个问题?我不知道

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Person implements Serializable{
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      protected int id;
      @Column(name = "last_name")
      private String name;
      @Column(name = "first_name")
      private String firstName;
      @Column
      private String address;
      @Column
      private String tel;
      @Column
      private String email;
      @Column(name = "bar_code")
      private String barCode;
      @Column(name = "birth_day")
      private java.sql.Date birthDay;
      private int sex = 1;

      Person(Person p){
            this.id = p.id;
            this.barCode = p.barCode;
            this.address = p.address;
            this.birthDay = p.birthDay;
            this.firstName = p.firstName;
            this.name = p.name;
            this.email = p.email;
            this.sex = p.sex;
            this.tel = p.tel;
      }
      ....
}

@Entity
public class Employee extends Person implements Serializable{
    public Employee(String name, String firstName, String address, String numTel,
        String email, Date birthDay){
        super(name, first_name, address, numTel, email, birthDay);
    }

    public Employee(Person p){
        super(p);
    }
}


@Entity
public class Member extends Person implements Serializable{
    public Member(String name, String firstName, String address, String numTel,
        String email, Date birthDay){
        super(name, first_name, address, numTel, email, birthDay);
    }

    public Member(Person p){
        super(p);
    }
}


 Person p = em.createQuery("SELECT p FROM Person p WHERE p.id = 1", Person.class).getResultList().get(0);
 Employee e= new Employee(p);
 em.getTransaction().begin();
 em.merge(e);
 em.getTransaction().commit();

文字无法解释代码能做什么。下面是一些代码,我希望能有所帮助。这是一个好的开始:)你能在你实际合并的地方添加代码吗?我不太明白“使用现有人员对象(实际上是成员)合并员工以创建员工”是什么意思。很抱歉迟到了。我添加了合并实体的代码。在Person的复制构造函数中,您将副本的id设置为原始实体的id。如果要创建新记录,请不要设置id,让持久性提供程序来执行。