📅  最后修改于: 2021-01-02 15:45:10             🧑  作者: Mango
在多对一映射中,各种属性只能称为一个属性。
在此示例中,每个员工只有一个公司地址,而一个地址属于许多员工。在这里,我们将使用XML执行多对一映射。
有两个持久性类Employee.java和Address.java。雇员类包含地址类引用,反之亦然。
package com.javatpoint;
public class Employee {
private int employeeId;
private String name,email;
private Address address;
//setters and getters
}
package com.javatpoint;
public class Address {
private int addressId;
private String addressLine1,city,state,country;
private int pincode;
private Employee employee;
//setters and getters
}
这两个映射文件是employee.hbm.xml和address.hbm.xml。
该文件包含有关数据库和映射文件的信息。
update
org.hibernate.dialect.Oracle9Dialect
jdbc:oracle:thin:@localhost:1521:xe
system
jtp
oracle.jdbc.driver.OracleDriver
package com.javatpoint;
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");
Employee e2=new Employee();
e2.setName("Anuj Verma");
e2.setEmail("anuj@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);
e2.setAddress(address1);
session.persist(e1);
session.persist(e2);
t.commit();
session.close();
System.out.println("success");
}
}
package com.javatpoint;
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 list=query.getResultList();
Iterator 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");
}
}