簡體   English   中英

如何在休眠模式下映射外鍵關系

[英]How to map foreign key relations in hibernate

我有如下的客戶類和地址類:客戶類中的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),
);

我在服務層創建地址對象(家庭,辦公室和輔助聯系人的三個地址對象)和客戶對象,並進行交易。 我不確定如何在hbm映射文件中指定外鍵關系,以及如何保存這四個對象(3個地址對象和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。

這是一個更適合您問題的答案。

假設customer表中的* AddressId列可以是外鍵,那么您應該在Customer休眠映射/類中將關系映射為many-to-one關系。 (請注意,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 ),創建/保存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);

如果您需要更新Customer引用的Address實體,則get對象,再次設置正確的Address對象,然后保存Customer

//in DAO update logic
Session session = sessionFactory.getCurrentSession(); //or openSession()
Customer customer = (Customer) session.get(Customer.class, customerId);
Address address = (Address) session.get(Address.class, addressId);
customer.setOfficeAddress(address);
session.saveOrUpdate(customer); //updates officeAddressId column to value of addressId

非常冗長,但明確而直接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM