日期:2014-05-17 浏览次数:20770 次
public class HibernateUtils {
private static final SessionFactory sessionFactory;
static {
try {
Configuration cfg = new Configuration();
sessionFactory = cfg.configure().buildSessionFactory();
} catch (Throwable e) {
System.err.println("Initial SessionFactory creation failed" + e);
throw new ExceptionInInitializerError(e);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Session getSession() {
return sessionFactory.openSession();
}
public static void closeSession(Session session) throws HibernateException {
if (session != null) {
if (session.isOpen()) {
session.close();
}
}
}
public static void rollback(Transaction tran) {
try {
if (tran != null) {
tran.rollback();
}
} catch (HibernateException he) {
System.out.println("Rollback faild." + he);
}
}
}
package com.super120.Model;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="tb_Operation")
public class Operation {
private int ID;
private String OperationName;
private String Description;
private String Reserved;
private Set<Operation> operations = new HashSet<Operation>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
@OneToMany(cascade={CascadeType.REMOVE},fetch=FetchType.EAGER,mappedBy="oper")
public Set<Operation> getOperations() {
return operations;
}
public void setOperations(Set<Operation> operations) {
this.operations = operations;
}
public String getOperationName() {
return OperationName;
}
public void setOperationName(String operationName) {
OperationName&