代码实现如下:
package com.ssh.entities;
public class Department {
private Integer id;
private String departmentName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}
package com.ssh.entities;
import java.util.Date;
public class Employee {
private Integer id;
private String lastName;
private String email;
private Date birth;
private Date createTime;
private Department department;
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2020??1??9?? ????9:08:43 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.ssh.entities.Department" table="SSH_DEPARTMENT">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="departmentName" type="java.lang.String">
<column name="DEPARTMENT_NAME" />
</property>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2020??1??9?? ????9:08:43 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.ssh.entities.Employee" table="SSH_EMPLOYEE">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="lastName" type="java.lang.String">
<column name="LASTNAME" />
</property>
<property name="email" type="java.lang.String">
<column name="EMAIL" />
</property>
<property name="birth" type="java.util.Date">
<column name="BIRTH" />
</property>
<property name="createTime" type="java.util.Date">
<column name="CREATE_TIME" />
</property>
<many-to-one name="department" class="com.ssh.entities.Department">
<column name="DEPARTMENT_ID" />
</many-to-one>
</class>
</hibernate-mapping>
hibernate配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=Asia/Shanghai</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/ssh/entities/Department.hbm.xml"/>
<mapping resource="com/ssh/entities/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
数据操作
package com.ssh.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
@Transactional
public class BaseDao {
@Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Session getSession() {
return this.sessionFactory.getCurrentSession();
}
}
package com.ssh.dao;
import java.util.List;
import com.ssh.entities.Department;
public class DepartmentDao extends BaseDao {
public List<Department> getAll() {
String hql = "FROM Department";
return getSession().createQuery(hql).list();
}
}
package com.ssh.dao;
import java.util.List;
import com.ssh.entities.Employee;
public class EmployeeDao extends BaseDao{
public void delete(Integer id) {
String hql = "DELETE From Employee e where e.id=?";
getSession().createQuery(hql).setParameter(0,id).executeUpdate();
}
public List<Employee> getAll() {
//String hql = "From Employee e LEFT OUTER JOIN FETCH e.department";
String hql = "From Employee";
return getSession().createQuery(hql).list();
}
public void saveOrUpdate(Employee employee) {
getSession().saveOrUpdate(employee);
}
public Employee get(Integer id) {
return (Employee)getSession().get(Employee.class,id);
}
}