模型
Java代碼
package Domain;
public class Employee {
public int getId() {
return id;
}
public void setId(int id) {
this
}
public String getName() {
return name;
}
public void setName(String name) {
this
}
public Department getDepart() {
return depart;
}
public void setDepart(Department depart) {
this
}
private int id;
private String name;
private Department depart;
}
Java代碼
package Domain;
import java
public class Department {
public int getId() {
return id;
}
public void setId(int id) {
this
}
public String getName() {
return name;
}
public void setName(String name) {
this
}
public Set<Employee> getEmps() {
return emps;
}
public void setEmps(Set<Employee> emps) {
this
}
private int id;
private String name;
private Set<Employee> emps ;
}
Xml代碼
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<many
</class>
</hibernate
Xml代碼
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<set name=
<key column=
<one
</set>
</class>
</hibernate
Java代碼
package Dao;
import Domain
public interface EmployeeDAO {
public void saveEmployee(Employee emp);
public Employee findEmployeeByName(String name);
public Employee findEmployeeById(int id);
public void updateEmployee(Employee emp);
public void removeEmployee(Employee emp);
}
Java代碼
package Dao;
import Domain
public interface DepartmentDAO {
public void saveDepartment(Department depart);
public Department findDepartmentByName(String name);
public Department findDepartmentById(int id);
public void updateDepartment(Department depart);
public void removeDepartment(Department depart);
}
Java代碼
package Dao
import org
import org
import org
import org
import Utils
import Dao
import Domain
public class EmployeeDAOImpl implements EmployeeDAO {
// 保存員工
public void saveEmployee(Employee emp) {
Session s = null;
Transaction tx = null;
try{
s = hibernateUtil
tx = s
s
mit();
}catch (HibernateException e) {
if(tx != null){
tx
}
throw e;
}finally{
if(s != null){
s
}
}
}
// 根據姓名查詢員工
public Employee findEmployeeByName(String name) {
Session s = null ;
try{
s = hibernateUtil
String hql =
Query query = s
query
Employee emp = (Employee) query
return emp;
}finally{
if(s != null){
s
}
}
}
// 根據員工id查詢員工
public Employee findEmployeeById(int id) {
Session s = null ;
try{
s = hibernateUtil
Employee emp = (Employee) s
return emp;
}finally{
if(s != null)
{
s
}
}
}
// 更新員工信息
public void updateEmployee(Employee emp) {
Session s = null;
Transaction tx = null;
try{
s = hibernateUtil
tx = s
s
mit();
}catch (HibernateException e) {
if(tx != null){
tx
}
throw e;
}finally{
if(s != null){
s
}
}
}
// 刪除員工
public void removeEmployee(Employee emp) {
Session s = null;
Transaction tx = null;
try{
s = hibernateUtil
tx = s
s
mit();
}catch (HibernateException e) {
if(tx != null){
tx
}
throw e;
}finally{
if(s != null){
s
}
}
}
}
Java代碼
package Dao
import org
import org
import org
import org
import Dao
import Domain
import Utils
public class DepartmentDAOImpl implements DepartmentDAO {
// 保存部門
public void saveDepartment(Department depart) {
Session s = null;
Transaction tx = null;
try{
s = hibernateUtil
tx = s
s
mit();
}catch (HibernateException e) {
if(tx != null){
tx
}
throw e;
}finally{
if(s != null){
s
}
}
}
// 根據name查找部門
public Department findDepartmentByName(String name) {
Session s = null ;
try{
s = hibernateUtil
String hql =
Query query = s
query
Department depart = (Department) query
return depart;
}finally{
if(s != null){
s
}
}
}
// 根據id查找部門
public Department findDepartmentById(int id) {
Session s = null ;
try{
s = hibernateUtil
Department depart = (Department) s
return depart;
}finally{
if(s != null)
{
s
}
}
}
// 更新部門
public void updateDepartment(Department depart) {
Session s = null;
Transaction tx = null;
try{
s = hibernateUtil
tx = s
s
mit();
}catch (HibernateException e) {
if(tx != null){
tx
}
throw e;
}finally{
if(s != null){
s
}
}
}
// 刪除部門
public void removeDepartment(Department depart) {
Session s = null;
Transaction tx = null;
try{
s = hibernateUtil
tx = s
s
mit();
}catch (HibernateException e) {
if(tx != null){
tx
}
throw e;
}finally{
if(s != null){
s
}
}
}
}
Java代碼
package Dao
import org
import org
import Utils
import Domain
import Domain
public class Many
public static void main(String[] args) {
add();
}
public static Department add(){
Session s = null ;
Transaction tx = null;
try{
Department depart = new Department();
depart
Employee emp = new Employee();
emp
emp
s = hibernateUtil
tx = s
s
s
mit();
return depart;
}finally{
if(s != null){
s
}
}
}
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28141.html