关于hibernate的哪些事儿
2021-04-08 14:25
标签:password sql auto pre 格式化 one automatic 关于 nat hibernate与spring整合 hibernate.hbm2ddl.auto Automatically validate or export schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly. eg. validate | update | create | create-drop 其实这个参数的作用主要用于:自动创建|更新|验证数据库表结构。如果不是此方面的需求建议set value="none". 其它几个参数的意思,我解释一下: validate 加载hibernate时,验证创建数据库表结构 以上4个属性对同一配置文件下所用有的映射表都起作用 关于hibernate的哪些事儿 标签:password sql auto pre 格式化 one automatic 关于 nat 原文地址:https://www.cnblogs.com/codeleader/p/12460601.html方式一:无障碍整合:带Hibernate配置文件
xml version="1.0" encoding="UTF-8"?>
beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
property name="configLocation" value="classpath:hibernate.cfg.xml"/>
bean>
bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
property name="sessionFactory" ref="sessionFactory"/>
bean>
tx:annotation-driven transaction-manager="transactionManager"/>
bean id="customerAction" class="com.ssh.web.action.CustomerAction" scope="prototype">
property name="customerService" ref="customerService"/>
bean>
bean id="customerService" class="com.ssh.service.impl.CustomerServiceImpl">
property name="customerDao" ref="customerDao"/>
bean>
bean id="customerDao" class="com.ssh.dao.impl.CustomerDaoImpl">
property name="sessionFactory" ref="sessionFactory"/>
bean>
beans>
方式二:不带Hibernate核心配置文件,将核心配置文件给Spring管理
xml version="1.0" encoding="UTF-8"?>
beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
context:property-placeholder location="classpath:jdbc.properties"/>
bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
property name="driverClass" value="${jdbc.driverClass}"/>
property name="jdbcUrl" value="${jdbc.url}"/>
property name="user" value="${jdbc.user}"/>
property name="password" value="${jdbc.password}"/>
bean>
bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
property name="dataSource" ref="dataSource" />
property name="hibernateProperties">
props>
prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>
prop key="hibernate.show_sql">trueprop>
prop key="hibernate.format_sql">trueprop>
prop key="hibernate.hbm2ddl.auto">updateprop>
props>
property>
property name="mappingResources">
list>
value>com/ssh/domain/Customer.hbm.xmlvalue>
list>
property>
bean>
bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
property name="sessionFactory" ref="sessionFactory"/>
bean>
tx:annotation-driven transaction-manager="transactionManager"/>
bean id="customerAction" class="com.ssh.web.action.CustomerAction" scope="prototype">
property name="customerService" ref="customerService"/>
bean>
bean id="customerService" class="com.ssh.service.impl.CustomerServiceImpl">
property name="customerDao" ref="customerDao"/>
bean>
bean id="customerDao" class="com.ssh.dao.impl.CustomerDaoImpl">
property name="sessionFactory" ref="sessionFactory"/>
bean>
beans>
结果在测试时,老是发现数据库表数据丢失。这个参数以前没怎么用,查了一圈其它的东东,最后才定位到这个上面。赶紧查了一下Hibernate的参数配置,解释如下:
create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。
create-drop 加载hibernate时创建,退出是删除表结构
update 加载hibernate自动更新数据库结构