hibernate.cfg.xml(hibernate配置文件)
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--配置SQLServer连接属性//172.16.40.5-->
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.url">
jdbc:sqlserver://172.16.40.5:1433;databaseName=dev_his
</property>
<property name="connection.username">sa</property>
<property name="connection.password">bsoft</property>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<!--在控制台显示SQL语句-->
<property name="hibernate.show_sql">true</property>
<property name="show_sqlserver">true</property>
<!--根据需要自动生成、更新数据表-->
<property name="myeclipse.connection.profile">sqlserver</property>
<!--注册所有ORM映射文件-->
<mapping resource="com/bsoft/emrviewws/pojo/Order.hbm.xml" />
<mapping resource="com/bsoft/emrviewws/pojo/InspectionResult.hbm.xml" />
<mapping resource="com/bsoft/emrviewws/pojo/PlantResult.hbm.xml" />
<mapping resource="com/bsoft/emrviewws/pojo/BioResult.hbm.xml" />
<mapping resource="com/bsoft/emrviewws/pojo/AntiResult.hbm.xml" />
<mapping resource="com/bsoft/emrviewws/pojo/PatientInfo.hbm.xml" />
<mapping resource="com/bsoft/emrviewws/pojo/DescribeResult.hbm.xml" />
<mapping resource="com/bsoft/emrviewws/pojo/OrderDetail.hbm.xml" />
</session-factory>
</hibernate-configuration>
package com.bsoft.emrviewws.pojo;
import java.sql.Timestamp;
import java.util.List;
public class Order {
private String orderNumber;
private double orderStatus;
private Timestamp operateTime;
private String operator;
private List<OrderDetail> orderDetails;
public String getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
public double getOrderStatus() {
return orderStatus;
}
public void setOrderStatus(double orderStatus) {
this.orderStatus = orderStatus;
}
public Timestamp getOperateTime() {
return operateTime;
}
public void setOperateTime(Timestamp operateTime) {
this.operateTime = operateTime;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public List<OrderDetail> getOrderDetails() {
return orderDetails;
}
public void setOrderDetails(List<OrderDetail> orderDetails) {
this.orderDetails = orderDetails;
}
}
Order.hbm.xml(pojo对应数据库的xml配置文件):
<?xml version="1.0" encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.bsoft.emrviewws.pojo.Order" table="l_lis_sqd">
<id name="orderNumber" column="DOCTREQUESTNO" type="string"/>
<property name="orderStatus" column="SQDSTATUS" type="double"/>
<property name="operateTime" column="REQUESTTIME" type="timestamp"/>
<property name="operator" column="REQUESTER" type="string"/>
<list name="orderDetails" table="l_lis_sqdmx" cascade="all">
<key>
<column name="orderNumber"/>
</key>
<index column="[index]" type="string"/>
<one-to-many class="com.bsoft.emrviewws.pojo.OrderDetail"/>
</list>
</class>
</hibernate-mapping>
HibernateUtil.java(调用hibernate.cfg.xml产生Session):
package com.bsoft.emrviewws.util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static String CONFIG_FILE_LOCATION = "hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateUtil() {
}
public static synchronized Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void setConfigFile(String configFile) {
HibernateUtil.configFile = configFile;
sessionFactory = null;
}
public static Configuration getConfiguration() {
return configuration;
}
}