Java 如何在hibernate中映射外键关系
我的客户类别和地址类别如下: Customer类中的officeAddressId、homeAddressId和secondaryAddressId用于表中的外键映射Java 如何在hibernate中映射外键关系,java,hibernate,Java,Hibernate,我的客户类别和地址类别如下: Customer类中的officeAddressId、homeAddressId和secondaryAddressId用于表中的外键映射 public class customer implements serializable { private static final long serialVersionUID= -5830229553758180137L; int age; String officeAddressId=
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),
);
我在服务层创建address对象(3个对象代表address,1个对象代表home、office和Secondary Contact)和customer对象,并打开事务。我不确定如何在hbm映射文件中给出外键关系,如何保存这四个对象(3个地址对象和1个客户对象),以及外键关系在数据库中正确保存的顺序
提前感谢….首先,将您的customer类的名称更改为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;
...
}
及
公共类地址实现可序列化{
...
@OneToMany(fetch=FetchType.LAZY,mappedBy=“officeAddress”)
私有集officeCustomers=新哈希集(0);
@OneToMany(fetch=FetchType.LAZY,mappedBy=“homeAddress”)
私有集homeCustomers=新哈希集(0);
@OneToMany(fetch=FetchType.LAZY,mappedBy=“secondaryAddress”)
私有集secondaryCustomers=新哈希集(0);
...
}
当然,您可以为Address类中的所有客户创建getter。这里有一个更适合您的问题的答案 假设
customer
表中的*AddressId列可以是外键,那么您应该在customer
Hibernate映射/class中将关系映射为多对一。(注意,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"/>
如果需要更新客户
引用的地址
实体,则获取
对象,再次设置正确的地址
对象,并保存客户
:
//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
非常详细,但明确明了。这是客户
(应大写)和地址
之间的多对一关系。在Customer
类中,将String officeAddress
更改为Address officeAddress
。我现在不知道您将在数据库中放入多少数据,但表中有一些冗余。因为对于每个地址,您都会保存城市和相应的州、pin码和国家名称。您可以创建另一个表,将pin码和国家名称作为键。或者,如果它不是一个世界范围的应用程序,那么您可以创建一个pin码。首先,感谢您提供的详细帮助。一个问题是,这是否意味着在使用Hibernate的情况下,我必须始终使用对象的组合?您不必这样做。您仍然可以将FK列映射为字符串或原语属性
(而不是多对一
),但这样做不允许您利用Hibernate进行自动关系查找或验证。每次获取客户
实体时,您都必须执行自己的地址
检索。但是,如果您使用地址es组合客户
,Hibernate将为您提取它们。所以这取决于你想做什么。构图必须使用hibernate吗?我们不能只使用Id字段吗?
//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);
//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