Hibernate入门指南笔记html
Hibernate5.0.7java
CREATE DATABASE myblog;
USE myblog;
CREATE TABLE IF NOT EXISTS cust_customer(
cust_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '用户id',
cust_name VARCHAR(32) NOT NULL COMMENT '客户名称(公司名称)',
cust_source VARCHAR(32) DEFAULT NULL COMMENT '客户信息来源',
cust_industry VARCHAR(32) DEFAULT NULL COMMENT '客户所属行业',
cust_level VARCHAR(32) DEFAULT NULL COMMENT '客户级别',
cust_phone VARCHAR(64) DEFAULT NULL COMMENT '固定电话',
cust_mobile VARCHAR(14) DEFAULT NULL COMMENT '移动电话'
);
复制代码
mysql-connector-java-5.1.43-bin.jarmysql
在工程目录下面新建一个lib目录将所须要的包拷贝到lib文件夹下 git
package com.whong;
public class cust_customer {
private long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_phone;
private String cust_mobile;
public long getCust_id() {
return cust_id;
}
public void setCust_id(long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
}
}
复制代码
映射文件是将刚才的实体cust_customer映射到数据库中的 由于安装了hibernate的插件,因此在这里能够直接选择建立hbm文件,不须要手动去配置 github
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Feb 28, 2018 2:40:14 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.whong.cust_customer" table="CUST_CUSTOMER">
<id name="cust_id" type="long">
<column name="CUST_ID" />
<generator class="assigned" />
</id>
<property name="cust_name" type="java.lang.String">
<column name="CUST_NAME" />
</property>
<property name="cust_source" type="java.lang.String">
<column name="CUST_SOURCE" />
</property>
<property name="cust_industry" type="java.lang.String">
<column name="CUST_INDUSTRY" />
</property>
<property name="cust_level" type="java.lang.String">
<column name="CUST_LEVEL" />
</property>
<property name="cust_phone" type="java.lang.String">
<column name="CUST_PHONE" />
</property>
<property name="cust_mobile" type="java.lang.String">
<column name="CUST_MOBILE" />
</property>
</class>
</hibernate-mapping>
复制代码
在第五步骤中的映射文件反应的是持久化类和数据库表的映射信息,而Hibernate的配置文件则主要是用来配置数据库链接以及Hibernate运行时所须要的各个属性的值,在src文件夹下建立cfg类型的文件 sql
?usessl=true
完整的url为
jdbc:mysql://localhost:3306/myblog?usessl=true
测试代码以下所示数据库
package com.testPackage;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.whong.cust_customer;
public class testHibernate {
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
cust_customer cust_customer = new cust_customer();
cust_customer.setCust_name("伟鸿");
cust_customer.setCust_phone("18*******969");
session.save(cust_customer);
transaction.commit();
session.close();
}
}
复制代码
若是这个时候试运行会报有以下的错误apache
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.whong.cust_customer
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:781)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1520)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:100)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:679)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666)
at com.testPackage.testHibernate.main(testHibernate.java:21)
复制代码
这里最主要的问题就是log日志文件的问题 解决方式是须要添加一个有关日志的配置文件,具体能够看一下这个连接的文章log4j WARN 和 SLF4J WARN 解决办法 在src目录下添加log4j.properties配置文件bash
hadoop.root.logger=DEBUG, console
log4j.rootLogger=INFO, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n
复制代码
再次运行会报以下的错误session
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.whong.cust_customer
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:781)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1520)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:100)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:679)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666)
at com.testPackage.testHibernate.main(testHibernate.java:21)
复制代码
这个问题时由于核心配置文件cfg和实体映射文件hbm之间没有对应起来,因此找不到那个cust_customer实体 解决方式以下 拷贝hbm文件的路径
<mapping resource="/LearnHibernate/src/com/whong/cust_customer.hbm.xml"/>
可是这个地方须要去掉src前面的路径 所以正确的路径是
<mapping resource="com/whong/cust_customer.hbm.xml"/>
再次运行的到正确的结果 并打印以下的一些日志记录
18/02/28 16:21:05 INFO hibernate.Version: HHH000412: Hibernate Core {5.0.7.Final}
18/02/28 16:21:05 INFO cfg.Environment: HHH000206: hibernate.properties not found
18/02/28 16:21:05 INFO cfg.Environment: HHH000021: Bytecode provider name : javassist
18/02/28 16:21:05 INFO common.Version: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
18/02/28 16:21:05 WARN orm.deprecation: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-mapping. Use namespace http://www.hibernate.org/dtd/hibernate-mapping instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
18/02/28 16:21:06 WARN orm.connections: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
18/02/28 16:21:06 INFO orm.connections: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/myblog?useSSL=true]
18/02/28 16:21:06 INFO orm.connections: HHH10001001: Connection properties: {user=root, password=****}
18/02/28 16:21:06 INFO orm.connections: HHH10001003: Autocommit mode: false
18/02/28 16:21:06 INFO internal.DriverManagerConnectionProviderImpl: HHH000115: Hibernate connection pool size: 20 (min=1)
18/02/28 16:21:07 INFO dialect.Dialect: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
复制代码
再数据库中查询cust_customer表也是能够查询到这一条记录的 至此hibernate的入门配置笔记完毕