码迷,mamicode.com
首页 > 系统相关 > 详细

hibernate-4.3.5安装配置

时间:2014-05-22 07:35:47      阅读:400      评论:0      收藏:0      [点我收藏+]

标签:hibernate

起初照着官方文档配,一直出错,貌似官方的文档时错的,查了很多资料,综合整理了一个可行的方案,如下:

0.1包结构

test.demo

test.domain  //实体类

test.util//工具类


0.2导如的jar包

hibernate-4.3.5的required包中的所有

optional包中的c3p0中的所有

下载slf4j,导入slf4j-api.jar 和 slf4j-log4j.jar

下载log4j, 导入log4j.jar

导入mysql-connector-java.jar


Log4j和slf4j的关系:http://blog.csdn.net/lifuxiangcaohui/article/details/7278595

apache-log4j-1.2.17         http://pan.baidu.com/share/link?shareid=121565833&uk=2047106924

slf4j-1.7.7                            http://pan.baidu.com/share/link?shareid=123250651&uk=2047106924



1.POJO如下

package test.domain;

public class Message {
	private Long id;
	private String text;
	private Message nextMessage;
	
	public Message(String text){
		this.text = text;
	}
		
	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();
	}
}


hibernate-4.3.5安装配置,布布扣,bubuko.com

hibernate-4.3.5安装配置

标签:hibernate

原文地址:http://blog.csdn.net/tao_sun/article/details/26148677

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!