hibernate入门---配置【第一天】

本文先说认识、配置前提及必须配置的一个步骤。再说后续配置出现的问题,最后才是示例。即可参考一、二、四,进行配置学习。

四为最新完整版本的配置【请直接跳过一、二、三】

如出现问题,再参考三

一、hibernate的认识:

全自动化、操纵数据库、配置需注意,和类头信息有关的,请ctrl+鼠标点击查看是否拼写错误【可跳转则无误】、拼写字符时请注意一些。

二、需要注意的是,在编写配置文件的时候配置:

首先:

在和src同级下建resource文件夹---》新建hibernate.cfg.xml文件,先写前面的内容:

<?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">

其次:ctrl+鼠标点击将文件下载到某个目录:

http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd

最后:

在eclipse下,window->preference,再进行如下操作:

 

 

 然后:(由于已经Add,所以显示的是我重新编辑的截图,第一行,下载的文件路径,key选择URL,key写的是正确的URL地址)

 

三、注意:后续编写完程序代码出现的问题:

最后发现是,类里面读取hibernate.cfg.xml的路径写错了以及关系映射的xml文件Customer.hbm.xml。

改正为:

 

运行后出现:

对于出现的[INFO] Environment - -hibernate.properties not found可以先不管,查阅资料说的是没有日志配置的情况下时非必须的。

现在解决后面的报错问题:

 问题主要是配置文件中 <property name="hibernate.Dialect">org.hibernate.dialect.MySQL5Dialect</property>改为

 <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

以及如<hibernate-mapping package="bean" auto-import="false">等,参考配置文件。 auto-import="false"后<class name="bean.Customer" table="customers" catalog="test">中要把name写完整。

后续出现:

java.lang.IncompatibleClassChangeError: class net.sf.cglib.core.DebuggingClassWriter has interface org.objectweb.asm.ClassVisitor as super class

查了资料说是加入cglig2.2就可以解决【更新】

接着,又出现如下错误:

 根据异常,发现又是缺少了包,实际上是我的包的版本太低【更新中...】

当出现警告:

[WARN] Configurator - -No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath:jar:file:/C:/javaee/myHibernate/WebContent/WEB-INF/lib/ehcache-1.0.jar!/ehcache-failsafe.xml

在classes下写:

ehcache-failsafe.xml

内容:(解压ehcache-1.0.jar得到,将其复制到classes下)

发现还是不行,网上查阅说的是缺少jta.jar【后文待续】

东拼西凑,发现问题很多,而且还浪费了很多的时间。于是,决定下载完整的文档、配置等进行自己学习。实在是度娘的答案大多都是错的,或者是以前版本。而我凑的结果就是问题不断,时间也浪费了。

 题外话:搭建maven

四、完整的配置文件及示例:

 1、下载地址:https://sourceforge.net/projects/hibernate/files/hibernate3/3.6.10.Final/

解压后:

 

我导入的包有:

 以及下面两个文件中所有的包:

后续再进行具体学习,现在对本文一配置相同。然后进行xml文件配置和类的书写:

1、src下新建文件夹resource,再新建hibernation.cfg.xml,内容:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6 <!-- 配置文件标签顺序property*,mapping*,(class-cache|collection-cache),event,listener* -->
 7 <session-factory>
 8 <!-- 设置访问mysql数据库的驱动描述 -->
 9 <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
10 <!-- 设置数据库的url -->
11 <property name="connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
12 <!-- 指定登录数据库用户账户 -->
13 <property name="connection.username">root</property>
14 <!-- 指定登录数据库用户密码 -->
15 <property name="connection.password">123456</property>
16 
17 <!-- 设置访问数据库的方言,提高数据库访问性能 -->
18 <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
19 <!-- 设置ddl -->
20 <!-- <property name="hbm2ddl.auto">auto</property> -->
21 
22 <!-- 指出映射文件 -->
23 <mapping resource="resource/Customer.hbm.xml"/>
24 </session-factory>
25 </hibernate-configuration>

 

2、src下新建Customer.hbm.xml,是java类与数据库映射的文件:

<?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 package="bean" auto-import="false">
<!-- POJO类映射表及某表的关系-->
<class name="bean.Customer" table="customers" catalog="test">
<id name="customerID" type="java.lang.String">
<column name="customerID" length="8"/>
<generator class="assigned"></generator>
</id>
<!-- 映射表中name字段 -->
<property name="name" type="java.lang.String">
<column name="name" length="40"/>
</property>
<!-- 映射表中phone字段 -->
<property name="phone" type="java.lang.String">
<column name="phone" length="16"/>
</property> 
</class>
</hibernate-mapping>

 

3、新建映射表类:

package bean;

public class Customer {
private String customerID,name,phone;

public String getCustomerID() {
return customerID;
}

public void setCustomerID(String customerID) {
this.customerID = customerID;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}
}

 

4、HibernateSessionFactory 类,用于对会话操作,即数据库连接等存放的会话。

package hibernate.factory;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import bean.Customer;

public class HibernateSessionFactory {
private static String configfile = "resource/hibernate.cfg.xml";
/**ThreadLocal是一个本地线程**/
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration config;
private static SessionFactory sessionFactory;
/**读取配置文件,创建一个工厂会话,这段代码为静态块,编译后已经运行**/
static{
try {
config = new Configuration().configure(configfile);
sessionFactory = config.buildSessionFactory();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**通过会话工厂打开会话,就可以访问数据库了**/
public static Session getSession(){
Session session = (Session)threadLocal.get();
if (session==null||!session.isOpen()) {
if (sessionFactory==null) {
rebuildSessionFactory();
}
session = (sessionFactory!=null)?sessionFactory.openSession():null;
}
return session;
}
/**重新创建一个会话工厂**/
public static void rebuildSessionFactory() {
try {
config.configure(configfile);
sessionFactory = config.buildSessionFactory();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**关闭与数据库的会话**/
public static void closeSession() {
Session session = (Session)threadLocal.get();
threadLocal.set(null);
if (session!=null) {
session.close();
}
}
}

 

5、测试类:

package bean;

import java.util.List;

import hibernate.factory.HibernateSessionFactory;

 

import org.hibernate.Session;
import org.hibernate.query.Query;

public class Test {
@SuppressWarnings("rawtypes")
public static void main(String[] args) {
/**由会话工厂类创建一个会话Session对象**/
Session session = HibernateSessionFactory.getSession();
/**由会话session对象创建一个查询对象**/
Query query = session.createQuery("from bean.Customer");
List list = query.list();
for (int i = 0; i < list.size(); i++) {
Customer customer = (Customer)list.get(i);
System.out.println(customer.getCustomerID()+customer.getName()+customer.getPhone());
}

}
}

 

五:总结:

配置文件需要写正确,路径需要注意,以及读取配置文件的Factory要正确。导包也需要注意导那个jar包下的包,或者哪个包下的类。最后就是,如四,最好统一下载某个版本所需要的所有包,然后进行配置学习。

Query query = session.createQuery("from bean.Customer");测试的时候,知道这里的查询不是直接查询数据库表,而是类【映射类】,类前加不加包名,自行在不同情况写实验,后续自己会继续实验,如有更新,将在本文后续新增。

虽然走了点弯路,但自己收获还是很多的,同时也能够大致报错是由于什么原因引起,怎么查找错误都有了一定的认识。

 

posted @ 2019-05-29 10:48  CiscoLee  阅读(278)  评论(0编辑  收藏  举报