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

时间:2014-01-24 15:12:26

标签: inheritance jpa merge

我的JPA项目有点问题。我有两个类Employee和Member扩展Person。这适用于JPA,但是当我尝试使用现有Person对象(实际上是成员)合并Employee时创建一个Employee,我有Duplicate Entry Exception。我该如何处理这个问题?我什么都不知道!!

@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();

0 个答案:

没有答案