起初照着官方文档配,一直出错,貌似官方的文档时错的,查了很多资料,综合整理了一个可行的方案,如下:
0.1包结构
test.demo
test.domain //实体类
test.util
0.2导如的jar包
hibernate-4.3.5的required包中的所有
optional包中的c3p0中的所有
下载slf4j,导入slf4j-api.jar 和 slf4j-nop.jar
导入mysql-connector-java.jar
1.POJO如下
package test.domain;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Message {
private Long id;
private String text;
private Message nextMessage;
public Message(String text){
this.text = text;
}
@Id
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Message getNextMessage() {
return nextMessage;
}
public void setNextMessage(Message nextMessage) {
this.nextMessage = nextMessage;
}
}
这边要注意的是要加注解
2.与之对应的映射文件Message.hbm.xml,注意,要与POJO放在一起
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="test.domain.Message" table="MESSAGES"> <id name="id" column="MESSAGE_ID"> <generator class="increment"></generator> </id> <property name="text" column="MESSAGE_TEXT"></property> <many-to-one name="nextMessage" cascade="all" column="NEXT_MESSAGE_ID" foreign-key="FK_NEXT_MESSAGE"></many-to-one> </class> </hibernate-mapping>3.hibernate.cfg.xml,即hibernate的配置文件, 放在src的根目录下
<?xml version='1.0' encoding='utf-8'?> <!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> <!-- Database connection settings --> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="connection.url"> jdbc:mysql://127.0.0.1:3306/hibernate </property> <property name="connection.username">root</property> <property name="connection.password">1234</property> <!-- Use the c3p0 connection pool provider --> <property name="hibernate.c3p0.min_size">5</property> <property name="hibernate.c3p0.max_size">20</property> <property name="hibernate.c3p0.timeout">300</property> <property name="hibernate.c3p0.max_statements">50</property> <property name="hibernate.c3p0.idle_test_period">3000</property> <!-- Show and print nice SQL on stdout --> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="hbm2ddl.auto">update</property> <!-- List of XML mapping files --> <mapping resource="test/domain/Message.hbm.xml" /> </session-factory> </hibernate-configuration>4.HibernateUtil.java工具类
package test.util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration
.buildSessionFactory(serviceRegistry);
return sessionFactory;
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
5.测试
package test.demo;
import org.hibernate.Session;
import test.domain.Message;
import test.util.HibernateUtil;
public class HelloWorld {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Message message = new Message("Hello,world");
session.save(message);
session.getTransaction().commit();
}
}