hibernate3.6 环境搭建
今天下午弄了一个下午,终于把hibernate3.6的开发环境搭建好了,主要步骤如下:
1、新建实体类
?
public class Event {
?private int id;
?private String title;
?private Date date;
?public int getId() {
??return id;
?}
?public void setId(int id) {
??this.id = id;
?}
?public String getTitle() {
??return title;
?}
?public void setTitle(String title) {
??this.title = title;
?}
?public Date getDate() {
??return date;
?}
?public void setDate(Date date) {
??this.date = date;
?}
建立映射文件
?
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
????? "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
????????? "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.chapterone.pojo">
?<class name="Event" table="EVENTS">
?<id name="id" column="EVENT_ID">
?<generator type="timestamp" column="EVENT_DATE"/>
?<property name="title"/>
?</class>
</hibernate-mapping>
?
3、新建
hibernate.cfg.xml
文件内容如下:
<!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.connection.url">
??jdbc:mysql://localhost/hibernate
?</property>
?<property name="hibernate.connection.driver_class">
??com.mysql.jdbc.Driver
?</property>
?<property name="hibernate.connection.username">root</property>
?<property name="hibernate.connection.password">vertrigo</property>
?<property name="hibernate.dialect">
??org.hibernate.dialect.MySQLDialect
?</property>
?<property name="hibernate.show_sql">true</property>
?<mapping resource="com/hibernate/chapterone/pojo/Event.hbm.xml" />
</session-factory>
</hibernate-configuration>
编写处理类:
?
public class HibernateUtil {
?private static final SessionFactory sessionFactory = buildSessionFactory();
?private static SessionFactory buildSessionFactory() {
?try {
?// Create the SessionFactory from hibernate.cfg.xml
?return new Configuration().configure().buildSessionFactory();
?}
?catch (Throwable ex) {
?// Make sure you log the exception, as it might be swallowed
?System.err.println("Initial SessionFactory creation failed." + ex);
?throw new ExceptionInInitializerError(ex);
?}
?}
?public static SessionFactory getSessionFactory() {
?return sessionFactory;
?}
}
?
public class EventManager {
?
?
public void? createtable(){
?
? Configuration cfg=new Configuration().configure();
? SchemaExport? export=new SchemaExport(cfg);
? export.create(true, true);
}
/**
?* 创建和添加数据
?* @param title
?* @param theDate
?*/
private void createAndStoreEvent(String title, Date theDate) {
?Session session = HibernateUtil.getSessionFactory().getCurrentSession();
?session.beginTransaction();
?Event theEvent = new Event();
?theEvent.setTitle(title);
?theEvent.setDate(theDate);
?session.save(theEvent);
?session.getTransaction().commit();
?}
?
?public static void main(String[] args) {
? EventManager event=new EventManager();
? event.createtable();
??
?}
}
?
在src目录下,除需要加入hibernate.cfg.xml文件外,由于需要插入日志,需要添加log4j.properties文件
?
接下来就是导入的jar包了,这个问题花了我一下午的时间:
需要导入的包如下:
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate3.jar
javax.persistence.jar 这个包在下载的文件中是没有的,需要自己下载。可以在附件中下载,也可以通过导入用户库
javassist-3.12.0.GA.jar
jta-1.1.jar
slf4j-api-1.6.1.jar
?
有这些包,运行程序就没有问题了
?
?
?