今天学习了Hibernate JPA ,也就是元注解吧,现在总结一下:
hibernate.cfg.xml 的配置:
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!-- 显示实际操作数据库时的SQL -->
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<!--<property name="dialect">org.hibernate.dialect.OracleDialect</property>-->
<property name="connection.url">jdbc:oracle:thin:@192.9.206.168:1521:orcl</property>
<property name="connection.username">omsystem</property>
<property name="connection.password">tj123456</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="myeclipse.connection.profile">orcl</property>
<!--<mapping resource="Struts2/Dao/TTestUser.hbm.xml"/>-->
<!-- 实体类 -->
<mapping class="Struts2.Dao.d.TTestUser"/>
</session-factory>
</hibernate-configuration>
使用元注解就省去了TTestUser.hbm.xml这种文件的配置了,
只需要在JavaBean中加注解就可以了:
TTestUser.java 这个文件可以通过MyEclipse自动生成:
package Struts2.Dao.d;
import javax.persistence.Column;
/**
* TTestUser entity. @author MyEclipse Persistence Tools
*/
@SuppressWarnings("serial")
@Entity
@Table(name = "T_TEST_USER")
public class TTestUser implements java.io.Serializable {
// Fields
private String id;
private String name;
private String pwd;
private String sex;
// Constructors
/** default constructor */
public TTestUser() {
}
/** minimal constructor */
public TTestUser(String id) {
this.id = id;
}
/** full constructor */
public TTestUser(String id, String name, String pwd, String sex) {
this.id = id;
this.name = name;
this.pwd = pwd;
this.sex = sex;
}
// Property accessors
@Id
@Column(name = "ID", unique = true, nullable = false, length = 20)
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
@Column(name = "NAME", length = 30)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "PWD", length = 30)
public String getPwd() {
return this.pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Column(name = "SEX", length = 20)
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
帮助类HibernateDao.java 文件:
package Struts2.Util;
import org.hibernate.HibernateException;
public class HibernateDao {
protected SessionFactory sessionFactory;// 会话工厂,用于创建会话
protected Session session;// hibernate会话
protected Transaction transaction; // hiberante事务
private static AnnotationConfiguration configuration = new AnnotationConfiguration();
public HibernateDao() throws HibernateException {
this.initHibernate();
}
// 帮助方法
protected void initHibernate() throws HibernateException {
// 装载配置,构造SessionFactory对象
//sessionFactory = new Configuration().configure().buildSessionFactory();
sessionFactory = configuration.configure().buildSessionFactory();
}
/**
*开始一个hibernate事务
*/
protected void beginTransaction() throws HibernateException {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
/**
*结束一个hibernate事务。
*/
protected void endTransaction(boolean commit) throws HibernateException {
if (commit) {
transaction.commit();
} else {
// 如果是只读的操作,不需要commit这个事务。
transaction.rollback();
}
session.close();
}
}
这个文件主要注意的是 装载配置,构造SessionFactory对象//sessionFactory = new Configuration().configure().buildSessionFactory();
需要修改为
private static AnnotationConfiguration configuration = new AnnotationConfiguration();
sessionFactory = configuration.configure().buildSessionFactory();
最后开始测试吧:
package Struts2.Action;
import Struts2.Dao.d.TTestUser;
import Struts2.Server.TestUserService;
public class TestUserAction {
public static void main(String[] args){
TestUserService tu = new TestUserService();
TTestUser tuser= new TTestUser();
tuser.setId("3");
tuser.setName("刘利明3");
tuser.setPwd("1233");
tuser.setSex("男3");
tu.addUser(tuser);
}
}
到这里就结束了,要导入AnnotationConfiguration 的jar包。