如何在hibernate中映射外键关系

时间:2013-09-09 13:15:16

标签: java hibernate

我有客户类和地址类,如下所示: Customer类中的officeAddressId,homeAddressId和secondaryAddressId用于表中的外键映射。

  public class customer implements serializable
    {
    private static final long serialVersionUID= -5830229553758180137L;
    int age;
    String officeAddressId= null;
    String homeAddressId= null;
    String secondaryAddressId= null;
    }

public class Address implements serializable
{
        private static final long serialVersionUID= -5130229553758180137L;
        private String              addressId           = null;
    private String              addressLine         = null;
    private String              cityName            = null;
    private String              stateName           = null;
    private String              countryName         = null;
    private String              pincode             = null;
}

我的数据库表很简单:

CREATE TABLE customer
(
customerID varchar(40) primary key,
officeAddressId varchar(40),
homeAddressId varchar(40),
secondaryAddressId varchar(40),
age int 
);

CREATE TABLE Address
(
addressID varchar(40) primary key,
addressLine varchar(40),
cityName varchar(40),
stateName varchar(40),
countryName varchar(40),
pincode varchar(10),
);

我在服务层和开放交易中创建地址对象(3个对象用于家庭,办公室和次级接触的地址1)和客户对象。我不知道如何在hbm映射文件中提供外键关系,以及如何保存这四个对象(3个地址对象和1个客户对象)以及外键关系在数据库中保持正确的顺序。

提前致谢....

2 个答案:

答案 0 :(得分:1)

首先,将客户类的名称更改为Customer。然后:

public Class Customer implements Serializable {
    ...

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "office_address_id")
    private Address officeAddress;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "home_address_id")
    private Address homeAddress;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "secondary_address_id")
    private Address secondaryAddress;

    ...
}

public Class Address implements Serializable {
    ...

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "officeAddress")
    private Set<Customer> officeCustomers = new HashSet<Customer>(0);

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "homeAddress")
    private Set<Customer> homeCustomers = new HashSet<Customer>(0);

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "secondaryAddress")
    private Set<Customer> secondaryCustomers = new HashSet<Customer>(0);

    ...
}

当然,您可以在Address类中为所有客户创建getter。

答案 1 :(得分:1)

这是一个更适合您问题的答案。

假设customer表中的* AddressId列可以是外键,那么您应该将关系映射为many-to-one Hibernate映射/类中的Customer。 (请注意,Java类应以大写字母开头。)

Customer课程中:

//each of these with getters/setters
Address officeAddress;
Address homeAddress;
Address secondaryAddress;

Customer.hbm.xml档案中:

<many-to-one name="officeAddress" class="[package.name.]Address" column="officeAddressId"/>
<many-to-one name="homeAddress" class="[package.name.]Address" column="homeAddressId"/>
<many-to-one name="secondaryAddress" class="[package.name.]Address" column="secondaryAddressId"/>

然后,创建/保存这些对象(可能在DAO方法中)的显式方法是访问Hibernate Session(通过SessionFactory),创建/保存{{1} }对象,在Address对象上设置它们,然后保存它。像这样:

Customer

如果您需要更新//in DAO create logic Session session = sessionFactory.getCurrentSession(); //or openSession() Address office = new Address(); Address home = new Address(); Address secondary = new Address(); //populate Address objects... session.saveOrUpdate(office); session.saveOrUpdate(home); session.saveOrUpdate(secondary); Customer customer = new Customer(); //populate Customer object... customer.setOfficeAddress(office); customer.setHomeAddress(home); customer.setSecondaryAddress(secondary); session.saveOrUpdate(customer); 个引用的Address个实体,然后Customer个对象,请再次设置正确的get个对象,然后保存Address }:

Customer

非常详细,但明确而直截了当。