Hibernate教程-双向关联
双向关联允许我们从双方获取依赖对象的详细信息。在这种情况下,我们在两个类中都有对方的引用。
我们以 Employee 和 Address 为例,如果 Employee 类有一个 Address 的引用,并且 Address 有一个 Employee 的引用。此外,如果在映射文件中为这些类应用了一对一或一对多关系,则称为双向关联。
访问我们的一对一和一对多映射页面以了解更多信息。
示例:双向关联的持久类
Employee.java
package cn.javatiku;
import javax.persistence.*;
@Entity
@Table(name="emp")
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int employeeId;
private String name, email;
@OneToOne(mappedBy="employee", cascade=CascadeType.ALL)
private Address address;
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
Address.java
package cn.javatiku;
import javax.persistence.*;
@Entity
@Table(name="address")
public class Address {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int addressId;
private String addressLine1, city, state, country;
private int pincode;
@OneToOne
@JoinColumn(name="employee_id")
private Employee employee;
public int getAddressId() {
return addressId;
}
public void setAddressId(int addressId) {
this.addressId = addressId;
}
public String getAddressLine1() {
return addressLine1;
}
public void setAddressLine1(String addressLine1) {
this.addressLine1 = addressLine1;
}
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 int getPincode() {
return pincode;
}
public void setPincode(int pincode) {
this.pincode = pincode;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
}
配置文件
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 5.3//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">jtp</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping class="cn.javatiku.Employee"/>
<mapping class="cn.javatiku.Address"/>
</session-factory>
</hibernate-configuration>
存储和获取数据的用户类
Store.java
package cn.javatiku;
import org.hibernate.*;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class Store {
public static void main(String[] args) {
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();
SessionFactory factory = meta.getSessionFactoryBuilder().build();
Session session = factory.openSession();
Transaction t = session.beginTransaction();
Employee e1 = new Employee();
e1.setName("Ravi Malik");
e1.setEmail("ravi@gmail.com");
Address address1 = new Address();
address1.setAddressLine1("G-13, Sector 3");
address1.setCity("Noida");
address1.setState("UP");
address1.setCountry("India");
address1.setPincode(201301);
e1.setAddress(address1);
address1.setEmployee(e1);
session.persist(e1);
t.commit();
session.close();
System.out.println("success");
}
}
Fetch.java
package cn.javatiku;
import java.util.Iterator;
import java.util.List;
import javax.persistence.TypedQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class Fetch {
public static void main(String[] args) {
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();
SessionFactory factory = meta.getSessionFactoryBuilder().build();
Session session = factory.openSession();
TypedQuery query = session.createQuery("from Employee e");
List<Employee> list = query.getResultList();
Iterator<Employee> itr = list.iterator();
while (itr.hasNext()) {
Employee emp = itr.next();
System.out.println(emp.getEmployeeId() + " " + emp.getName() + " " + emp.getEmail());
Address address = emp.getAddress();
System.out.println(address.getAddressLine1() + " " + address.getCity() + " " +
address.getState() + " " + address.getCountry() + " " + address.getPincode());
}
session.close();
System.out.println("success");
}
}