休眠:许多额外的列和遗产

时间:2018-08-31 11:15:01

标签: hibernate many-to-many one-to-many hierarchy hibernate-5.x

如果有人可以帮助我,我将非常感激。

我的代码没有返回任何错误,但是在打印JSON时,employeeproject表的复合ID映射为空。

如果有人可以看到,我没有发现任何问题。

数据库表employeeproject的表为空,但employee或project等其他表正常工作。

EmployeeProjectPK:

    @Embeddable
public class EmployeeProjectPK implements Serializable{

    @JsonBackReference
    private Resource resource;
    @JsonBackReference
    private Project project;


    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    public Resource getResource() {
        return resource;
    }

    public void setResource(Resource resource) {
        this.resource = resource;
    }
    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

 Equals and HashCode ommited

EmployeeProject:

    @Entity
@Table(name = "employeeproject")
@AssociationOverrides({
    @AssociationOverride(name = "id.resource",
        joinColumns = @JoinColumn(name = "idResource")),
    @AssociationOverride(name = "id.project",
        joinColumns = @JoinColumn(name = "idProject")) })
public class EmployeeProject implements Serializable{


    @JsonManagedReference
    private EmployeeProjectPK id = new EmployeeProjectPK();

    private Double percentage;

    private Double rate;

    @Column(name = "percentage") 
    public Double getPercentage() {
        return percentage;
    } 

    public void setPercentage(Double percentage) {
        this.percentage = percentage;
    }

    @Column(name = "rate")
    public Double getRate() {
        return rate;
    }

    public void setRate(Double rate) {
        this.rate = rate;
    }
    @JsonManagedReference
    @Transient
    public Project  getProject() {
        return getId().getProject();
    }

    public void setProject(Project  project) {
        getId().setProject(project);
    }
    @JsonBackReference
    @Transient
    public Resource getResource() {
        return getId().getResource();
    }

    public void setResource(Resource  resource) {
        getId().setResource(resource);
    }

    @EmbeddedId
    public EmployeeProjectPK getId() {
        return id;
    }

    public void setId(EmployeeProjectPK id) {
        this.id = id;
    }
  Equals and HashCode ommited

员工:

@Entity
@PrimaryKeyJoinColumn(name="idEmployee")
public class Employee extends Resource implements Serializable{
    private String password;
    private Integer holidaysDays;
    private Integer holidaysAgreement;
    private Double salary;
    private Double food;
    private Double medicalInsurance;  
    private Double bonus;
    private Double transport;
    public Employee() {
    }

    public Employee( String password, Integer holidaysDays, Integer holidaysAgreement, Double salary, Double food, Double medicalInsurance, Double bonus, Double transport, String docId, String name, String surname, String email, String photo, String telephone, String gender, Date birthDate, String nationality, Boolean impairment, Boolean mobility, String address, Boolean driverLicense) {
        super( docId, name, surname, email, photo, telephone, gender, birthDate, nationality, impairment, mobility, address, driverLicense);

        this.password = password;
        this.holidaysDays = holidaysDays;
        this.holidaysAgreement = holidaysAgreement;
        this.salary = salary;
        this.food = food;
        this.medicalInsurance = medicalInsurance;
        this.bonus = bonus;
        this.transport = transport;
    }

    @Column(name = "password")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    @Column(name = "holidaysDays")
    public Integer getHolidaysDays() {
        return holidaysDays;
    }

    public void setHolidaysDays(Integer holidaysDays) {
        this.holidaysDays = holidaysDays;
    }
    @Column(name = "holidaysAgreement")
    public Integer getHolidaysAgreement() {
        return holidaysAgreement;
    }

    public void setHolidaysAgreement(Integer holidaysAgreement) {
        this.holidaysAgreement = holidaysAgreement;
    }
    @Column(name = "salary")
    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }
    @Column(name = "food")
    public Double getFood() {
        return food;
    }

    public void setFood(Double food) {
        this.food = food;
    }
    @Column(name = "medicalInsurance")
    public Double getMedicalInsurance() {
        return medicalInsurance;
    }

    public void setMedicalInsurance(Double medicalInsurance) {
        this.medicalInsurance = medicalInsurance;
    }
    @Column(name = "bonus")
    public Double getBonus() {
        return bonus;
    }

    public void setBonus(Double bonus) {
        this.bonus = bonus;
    }
    @Column(name = "transportbonus")
    public Double getTransport() {
        return transport;
    }

    public void setTransport(Double transport) {
        this.transport = transport;
    }

      Equals and HashCode ommited

}

资源:

@Entity
@Table(name = "resource")
@Inheritance(strategy=InheritanceType.JOINED)
public class Resource implements Serializable {

    private Long id;
    private String docId;
    private String name;
    private String surname;
    private String email;
    private String photo;
    private String telephone;
    private String gender;
    private Date birthDate;
    private String nationality;
    private Boolean impairment;
    private Boolean mobility;
    private String address;
    private Boolean driverLicense;


    private Set<EmployeeProject> employeeProjects= new HashSet<EmployeeProject>(); 

    public Resource() {
    }

    public Resource( String docId, String name, String surname, String email, String photo, String telephone, String gender, Date birthDate, String nationality, Boolean impairment, Boolean mobility, String address, Boolean driverLicense) {
        this.docId = docId;
        this.name = name;
        this.surname = surname;
        this.email = email;
        this.photo = photo;
        this.telephone = telephone;
        this.gender = gender;
        this.birthDate = birthDate;
        this.nationality = nationality;
        this.impairment = impairment;
        this.mobility = mobility;
        this.address = address;
        this.driverLicense = driverLicense;

    }

        public void addProject(EmployeeProject project) {
        this.employeeProjects.add(project);
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "idResource")
    public Long getId() {
        return id;
    }

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

    @Column(name = "docId")
    public String getDocId() {
        return docId;
    }

    public void setDocId(String docId) {
        this.docId = docId;
    }

    @Column(name = "name")
    public String getName() {
        return name;
    }

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

    @Column(name = "surname")
    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    @Column(name = "email")
    public String getEmail() {
        return email;
    }

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

    @Column(name = "photo")
    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    @Column(name = "telephone")
    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    @Column(name = "gender")
    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Column(name = "birthdate")
    public Date getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    @Column(name = "nationality")
    public String getNationality() {
        return nationality;
    }

    public void setNationality(String nationality) {
        this.nationality = nationality;
    }
    @Column(name = "impairment")
    public Boolean getImpairment() {
        return impairment;
    }

    public void setImpairment(Boolean impairment) {
        this.impairment = impairment;
    }
    @Column(name = "mobility")
    public Boolean getMobility() {
        return mobility;
    }

    public void setMobility(Boolean mobility) {
        this.mobility = mobility;
    }
    @Column(name = "address")
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    @Column(name = "driverLicense")
    public Boolean getDriverLicense() {
        return driverLicense;
    }

    public void setDriverLicense(Boolean driverLicense) {
        this.driverLicense = driverLicense;
    }
    @JsonManagedReference
    @OneToMany(mappedBy = "id.resource",cascade = CascadeType.ALL)
    public Set<EmployeeProject> getEmployeeProjects() {
        return employeeProjects;
    }

    public void setEmployeeProjects(Set<EmployeeProject> projects) {
        this.employeeProjects = projects;
    }

 public void addEmployeeProject(EmployeeProject employeeProject) {
        this.employeeProjects.add(employeeProject);
    } 

项目:

 @Entity
@Table(name = "project")
public class Project implements Serializable {


    private Long id;
    private Date startDate;
    private Date endDate;
    private String name;
    private String state;
    private Double amount;


    private Set<EmployeeProject> employeeProjects= new HashSet<EmployeeProject>(); 

    public Project() {
    }

    public Project( Date startDate, Date endDate, String name, String state, Double amount) {

        this.startDate = startDate;
        this.endDate = endDate;
        this.name = name;
        this.state = state;
        this.amount = amount;

    }
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "idProject")
    public Long getId() {
        return id;
    }

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

    @Column(name = "startDate")
    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    @Column(name = "endDate")
    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }
    @Column(name = "name")
    public String getName() {
        return name;
    }

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

    @Column(name = "state")
    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    @Column(name = "amount")
    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }
    @JsonBackReference
    @OneToMany(mappedBy = "id.project",
            cascade = CascadeType.ALL)
    public Set<EmployeeProject> getEmployeeProjects() {
        return employeeProjects;
    }

    public void setEmployeeProjects(Set<EmployeeProject> projects) {
        this.employeeProjects = projects;
    }

 public void addEmployeeProject(EmployeeProject employeeProject) {
        this.employeeProjects.add(employeeProject);
    }

}

0 个答案:

没有答案