问题收集
1
8月 09, 2019 8:18:02 上午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.17.Final}
8月 09, 2019 8:18:02 上午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.format_sql=true, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;LOCK_TIMEOUT=10000, hibernate.max_fetch_depth=5, hibernate.generate_statistics=true, hibernate.connection.driver_class=org.h2.Driver, hibernate.connection.password=****, hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.pool_size=5, hibernate.service.allow_crawling=false}
此问题已解决:该版本的hibernate适用于jdk7和jdk8
详情请转
2
八月 09, 2019 11:54:07 上午 org.hibernate.internal.SessionFactoryRegistry addSessionFactory
WARN: HHH000277: Could not bind factory to JNDI
org.hibernate.engine.jndi.JndiException: Error parsing JNDI name []
<session-factory name="">改为 <session-factory>
详情请转
3、
org.hibernate.MappingException: Unknown entity: pojo.UserBean
at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:625)
原因:1、映射文件的表名与列名,要与数据库中匹配
2、映射文件要被配置文件所加载
3、读取配置的类要确保数据准确提取配置文件
4、
the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
<!-- 指定自动生成数据表的策略:在运行数据库的时候hibernate会为我们在数据库自动生成数据表的策略 -->
<!-- <property name="hibernate.hbm2ddl.auto">update</property> -->
原因:这个属性的配置将会产生一个数据表,而已经在数据库创建了数据表,就不用再创建数据表,所以,在有数据标的情况下配置此属性,将会导致事务提交失败,故,去掉该属性,问题解决
配置文件读取:
package loadhibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class Loadconfig {
// 创建会话工厂
private static SessionFactory sessionFactory;
//创建局部变量保存session
private static final ThreadLocal<Session> THREAD_LOCAL = new ThreadLocal<Session>();
//读取配置文件
static {
try {
// 注册配置文件
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure("/hibernate.cfg.xml")
.build();
// 创建会话工厂
sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//重建sessionFactory
public static void rebuildSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure("/hibernate.cfg.xml");
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory();
}
//创建sessionfactory实例
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
//访问线程中session
public static Session getSession() throws HibernateException {
Session session = (Session) THREAD_LOCAL.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
// 创建session
session = (sessionFactory != null) ? sessionFactory.openSession() : null;
THREAD_LOCAL.set(session);
}
return session;
}
//关闭session
public static void closeSession() {
// 获取session——保证session不为空,以便关闭session
Session session = (Session) THREAD_LOCAL.get();
// 将session设置为空
THREAD_LOCAL.set(null);
// 关闭session
if (session != null) {
session.close();
}
}
// 关闭缓存和连接池
public static void shutdown() {
getSessionFactory().close();
}
}
配置文件
<?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 name="">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root1</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mysql?serverTimezone=Asia/Shanghai&useSSL=false</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="connection.pool_size">1</property>
<mapping class="pojo.User" resource="pojo/UserBean.hbm.xml"/>
</session-factory>
</hibernate-configuration>