关联映射(多对一单向外键)
映射文件:<many-to-one name=”depart” column=”depart_id” />
hibernate.cfg.xml
<mapping resource="org/kevin/domain/Department.hbm.xml" /> <mapping resource="org/kevin/domain/Employee.hbm.xml" />
Department.java
public class Department { private int id; private String name; public Department() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Employee.java
public class Employee { private int id; private String name; private Department depart; public Employee(){} public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Department getDepart() { return depart; } public void setDepart(Department depart) { this.depart = depart; } }
Department.hbm.xml
<hibernate-mapping> <class name="org.kevin.domain.Department"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> </class> </hibernate-mapping>
Employee.hbm.xml
<hibernate-mapping> <class name="org.kevin.domain.Employee"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <many-to-one name="depart" column="depart_id" /> </class> </hibernate-mapping>
HibernateUtil.java
package org.kevin.util; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public final class HibernateUitl { private static SessionFactory sessionFactory; private HibernateUitl() { } static { Configuration cfg = new Configuration(); cfg.configure(); sessionFactory = cfg.buildSessionFactory(); } public static SessionFactory getSessionFactory() { return sessionFactory; } public static Session getSession() { return sessionFactory.openSession(); } }
TestManyToOne.java
新建Source Folder test
新建包和src 实体类相同的包路径
@Test public void addTest(){ Department dep = new Department(); dep.setName("depart name"); Employee emp = new Employee(); emp.setName("emp name"); emp.setDepart(dep); Session s = null; Transaction tx = null; try { s = HibernateUitl.getSession(); tx = s.beginTransaction(); s.save(dep); s.save(emp); tx.commit(); } catch (HibernateException e) { e.printStackTrace(); }finally{ if(s!=null) s.close(); } }
@Test public void getTest(){ int empId=1; Session s = null; try { s = HibernateUitl.getSession(); Employee e = (Employee) s.get(Employee.class, empId); //System.out.println( e.getName() ); System.out.println( e.getDepart().getName() ); } finally { if (s != null) s.close(); } }
create table Department (id integer not null auto_increment, name varchar(255), primary key (id))
create table Employee (id integer not null auto_increment, name varchar(255), depart_id integer, primary key (id))
alter table Employee add index FK4AFD4ACE275992EB (depart_id), add constraint FK4AFD4ACE275992EB foreign key (depart_id) references Department (id)
10:02:50,092 INFO SchemaUpdate:217 - schema update complete
Hibernate:
insert
into
Department
(name)
values
(?)
Hibernate:
insert
into
Employee
(name, depart_id)
values
(?, ?)