配置持久性实体

时间:2017-05-15 14:19:25

标签: hibernate jpa jpa-2.0 openejb

enter image description here

我正在使用Java Persistence API用于已经存在的项目,如图所示,根据他的id,每个人都有一个相对的帮助。在我使用hibernate逆向工程来生成实体之前,但我没有知道如何处理这种情况。如果有可能帮助我将“Adminisitrateur”实体与其他方面的员工保证联系,那么每个“管理员”都有亲戚保证如果有可能我会感激不尽。

这里是实体的持久性代码: 用户实体:

package persistence;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity(name = "User")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;
    private String prenom;
    @Column(unique = true)
    private String matricule;
    private String password;
    @Column(unique = true)
    private String email;

    @OneToMany(mappedBy = "employer", fetch = FetchType.EAGER)
    private Set<Demande> demandes;
    private static final long serialVersionUID = 1L;

    public User() {
    }


    public User(String name, String prenom, String matricule, String password, String email) {
        super();
        this.name = name;
        this.prenom = prenom;
        this.matricule = matricule;
        this.password = password;
        this.email = email;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Set<Demande> getDemandes() {
        return demandes;
    }

    public void setDemandes(Set<Demande> demandes) {
        this.demandes = demandes;
    }

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }


    public String getMatricule() {
        return matricule;
    }


    public void setMatricule(String matricule) {
        this.matricule = matricule;
    }


    public static long getSerialversionuid() {
        return serialVersionUID;
    }

}

雇主实体:

package persistence;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;

/**
 * Entity implementation class for Entity: Employer
 *
 */
@Entity

public class Employer extends User implements Serializable {

    @OneToMany(mappedBy = "employerRelative", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<Assure> assures;
    private static final long serialVersionUID = 1L;

    public Employer() {

    }

    public Employer(String name, String prenom, String matricule, String password, String email) {
        super(name, prenom, matricule, password, email);
    }

    public List<Assure> getAssures() {
        return assures;
    }

    public void setAssures(List<Assure> assures) {
        this.assures = assures;
    }

    public void linkAssures(List<Assure> assures) {
        this.assures = assures;
        for (Assure a : assures) {
            a.setEmployerRelative(this);
        }
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

}

管理实体:

package persistence;

import java.io.Serializable;


import javax.persistence.*;
import persistence.User;

/**
 * Entity implementation class for Entity: Administrateur
 *
 */
@Entity

public class Administrateur extends User implements Serializable {


    private static final long serialVersionUID = 1L;


    public Administrateur() {
        super();
    }

    public Administrateur(String name, String prenom, String login, String password, String email) {
        super(name, prenom, login, password, email);
        // TODO Auto-generated constructor stub
    }





}

保证实体:     包持久性;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;

/**
 * Entity implementation class for Entity: Assure
 *
 */
@Entity

public class Assure extends User implements Serializable {
    @ManyToOne
    private Employer employerRelative;
    private String type;
    private static final long serialVersionUID = 1L;

    public Assure() {
        super();
    }

    public Assure(String name, String prenom, String matricule, String password, String email) {
        super(name, prenom, matricule, password, email);
    }

    public Employer getEmployerRelative() {
        return employerRelative;
    }

    public void setEmployerRelative(Employer employerRelative) {
        this.employerRelative = employerRelative;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

}

1 个答案:

答案 0 :(得分:0)

这里我们讨论的是用户类的扩展。使用hibernate用户类执行此操作不应使用@Entity进行注释。您应该使用@MappedSupperClass或更好的@Inheritance注释。