无法在hibernate

时间:2016-01-29 11:03:09

标签: hibernate

在我的项目中有两个bean类Employee和Address

我想为此使用映射。下面是我想用hibernate创建的表

Employee table
-------------------------------------------------------------------------------
id       |    username   |  password  | permanent_address | residential_address
-------------------------------------------------------------------------------
1               abc            abc            1                   2


Address table
    -------------------------------------------------------------------------------
    id       |    address_line1   |  address_line2  | city | country| .....
    -------------------------------------------------------------------------------
    1               aaaaa           bbbbbb            ppp    india               

    2               ssss           ddddd              fff    india               

这里有两个地址一个是住宅,第二个是永久地址,两个都映射在员工表中,用于id 1和id 2

如何为此创建我的bean类。

下面是我正在尝试的

package com.perennial.beans.employee;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "employee")
public class Employee {

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

    @Column(name = "username", length = 50, unique = true, nullable = false)
    private String username;

    @Column(name = "password", length = 500, nullable = false)
    private String password;

    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="address1")
    private Address address1;

    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="address2")
    private Address address2;

    public Employee() {
    }

    public long getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

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

}

package com.perennial.beans.employee;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "address")
public class Address {

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

    @Column(name = "address_line1", length = 100)
    private String addressLine1;

    @Column(name = "address_line2", length = 100)
    private String addressLine2;

    @Column(name = "city")
    private String city;

    @Column(name = "state")
    private String state;

    @Column(name = "country")
    private String country;

    @Column(name = "zip_code")
    private String zipCode;


    private Employee employee;

    public long getId() {
        return id;
    }

    public String getAddressLine1() {
        return addressLine1;
    }

    public void setAddressLine1(String addressLine1) {
        this.addressLine1 = addressLine1;
    }

    public String getAddressLine2() {
        return addressLine2;
    }

    public void setAddressLine2(String addressLine2) {
        this.addressLine2 = addressLine2;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

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

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getZipCode() {
        return zipCode;
    }

    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }


}

任何人都可以告诉我我该怎么做

2 个答案:

答案 0 :(得分:1)

要准确映射到permanent_addressresidential_address,您需要以这种方式更改Employee映射

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "permanent_address")
private Address permanentAddress;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "residential_address")
private Address residentialAddress;

当然,您需要将permanentAddressresidentialAddress的getter和setter添加到Employee

<强>更新

需要从private Employee employee;类中删除此Address

答案 1 :(得分:1)

@ v.ladynev回答对于在Employee表中生成适当的列名是正确的。

但上述异常似乎是因为hibernate无法理解&#34;员工员工&#34;在你的地址类,因为你没有提供任何类型的。(如@ column / @ OneToOne / @ embeded)。

我认为你需要修改你的地址类

@OneToOne(mappedBy = "address1") // inverse reference for bi-directional mapping of "address1"
private Employee employee1;

@OneToOne(mappedBy = "address2")  // inverse reference for bi-directional mapping of "address2"
private Employee employee2;

但正如@hasnae所说,这似乎不是一个正确的方法,更好的是你在@oneToMany的Employee中创建一个收集类型的地址。

你可以在你的地址类中拥有员工的@ManyToOne。