在Hibernate中创建POJO

时间:2017-12-25 13:44:26

标签: hibernate pojo

我正在努力创造pojo's。我的要求是根据策略ID检索用户信息。每个客户只有一个地址和一个政策。

所以我创造了三个POJO。 Address.java,Customer.java,Policy.java。

Address.java

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


    @Column(name="ADDRESS_STREET")
    private String address_street;

    @Column(name="ADDRESS_CITY")
    private String address_city;

    @Column(name="ADDRESS_STATE")
    private String address_state;

    @Column(name="ADDRESS_ZIPCODE")
    private String address_zipcode;

    @GenericGenerator(name = "generator", strategy = "foreign",
    parameters = @Parameter(name = "property", value = "CUSTOMER"))
    @Id
    @GeneratedValue(generator = "generator")
    @Column(name="CUSTOMER_ID")
    private int customer_id;
    public String getAddress_street() {
        return address_street;
    }
    public void setAddress_street(String address_street) {
        this.address_street = address_street;
    }
    public String getAddress_city() {
        return address_city;
    }
    public void setAddress_city(String address_city) {
        this.address_city = address_city;
    }
    public String getAddress_state() {
        return address_state;
    }
    public void setAddress_state(String address_state) {
        this.address_state = address_state;
    }
    public String getAddress_zipcode() {
        return address_zipcode;
    }
    public void setAddress_zipcode(String address_zipcode) {
        this.address_zipcode = address_zipcode;
    }
    public int getCustomer_id() {
        return customer_id;
    }
    public void setCustomer_id(int customer_id) {
        this.customer_id = customer_id;
    }



}

Customer.java

@Entity
@Table(name="CUSTOMER")
public class Customer {

    private int customer_id;
    private String customer_name;
    private Date customer_dob;
    private Address address;
    private Policy policy;

    @OneToOne
    @JoinColumn(name = "CUSTOMER_ID")
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    @OneToOne
    @JoinColumn(name = "CUSTOMER_ID")
    public Policy getPolicy() {
        return policy;
    }
    public void setPolicy(Policy policy) {
        this.policy = policy;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "CUSTOMER_ID")
    public int getCustomer_id() {
        return customer_id;
    }
    public void setCustomer_id(int customer_id) {
        this.customer_id = customer_id;
    }

    @Column(name = "CUSTOMER_NAME")
    public String getCustomer_name() {
        return customer_name;
    }
    public void setCustomer_name(String customer_name) {
        this.customer_name = customer_name;
    }

    @Column(name = "CUSTOMER_DOB")
    public Date getCustomer_dob() {
        return customer_dob;
    }
    public void setCustomer_dob(Date customer_dob) {
        this.customer_dob = customer_dob;
    }

}

Policy.java

@Entity
@Table(name="CUSTOMER_POLICY_DETAILS")
public class Policy implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="POLICY_ID")
    int policy_id;

    @Column(name="POLICY_START_DATE")
    String policyStartDate;

    @Column(name="POLICY_END_DATE")
    String policyEndDate;

    @GenericGenerator(name = "generator", strategy = "foreign",
    parameters = @Parameter(name = "property", value = "CUSTOMER"))
    @Id
    @GeneratedValue(generator = "generator")
    @Column(name="CUSTOMER_ID")
    int customer_id;

    public int getCustomer_id() {
        return customer_id;
    }

    public void setCustomer_id(int customer_id) {
        this.customer_id = customer_id;
    }

    public Policy() {
        super();
    }

    public int getPolicy_id() {
        return policy_id;
    }



    public void setPolicy_id(int policy_id) {
        this.policy_id = policy_id;
    }



    public String getPolicyStartDate() {
        return policyStartDate;
    }

    public void setPolicyStartDate(String policyStartDate) {
        this.policyStartDate = policyStartDate;
    }

    public String getPolicyEndDate() {
        return policyEndDate;
    }

    public void setPolicyEndDate(String policyEndDate) {
        this.policyEndDate = policyEndDate;
    }


}

我应该在Customer类中包含Address对象和Policy对象吗? 由于我在Address和Policy类中有customer_id属性,我是否不需要将它包含在Customer类中?

2 个答案:

答案 0 :(得分:0)

我从您的描述中了解到。客户与地址之间的关系是一对一的,客户必须拥有属于一个客户的地址和地址以及政策。因此,无论客户是在地址还是在地址中,客户都必须这样做。客户pojo。

所以在你的情况下,你将从同一个对象获得客户对象,然后是地址和政策。我希望回答你的问题。

答案 1 :(得分:0)

是。在客户实体中包含地址政策实体是基于 policy_id address_id <提取客户信息的简单方法之一/ em>的。为此,您需要在客户实体中包含地址政策实体的主键。根据您提供的代码,地址实体的连接列必须是 Address_id ,策略实体的连接列必须是 policy_id

  1. 您为客户中的两个地图指定了相同的名称 实体。由于这些映射代表关系和外来 密钥,各个实体的主键列的名称必须是 你的加入专栏。

  2. 由于您只有 one-one 映射到其他实体双向     映射不会在这里造成任何问题。但你应该非常     小心其他关系映射中的双向映射。