天天看點

Hibernate5學習筆記

1

hibernate自增MySQLIntegrityConstraintViolationException:

換一種儲存方式:

http://tanglei528.blog.163.com/blog/static/43353399201071313110854/

2 hibernate自動創表

<!-- validate 加載hibernate時,驗證建立資料庫表結構
create 每次加載hibernate,重新建立資料庫表結構,這就是導緻資料庫表資料丢失的原因。
create-drop 加載hibernate時建立,退出是删除表結構
update 加載hibernate自動更新資料庫結構 -->
<!-- 指定hibernate啟動時自動建立表結構 注意:使用create會在第一次編譯jsp時重新建立表,如果存在會被删除重建-->
<property name="hbm2ddl.auto">validate</property>
           

3

spring整合hibernate繼承HibernateDaoSupport報錯:‘sessionFactory’ or ‘hibernateTemplate’ is required

hibernate5以前可使用:

<!-- 下面的方式hibernate5沒效
<bean id="catServiceImpl" class="cn.yunlingfly.xxx.service.impl.CatSHServiceImpl">
	<property name="sessionFactory" ref="sessionFactory"></property>
</bean> -->
           

hibernate5可在DAO層加入:

@Autowired
public void setSessionFacotry(SessionFactory sessionFacotry) {
    super.setSessionFactory(sessionFacotry);
}
           

4 指定MySQL高本版和hibernate方言:

<!-- 指定使用MySQL資料庫格式的sql,指定高版本的MySQL:MySQL5Dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
           

5 Hibernate5 配置實體類mapping失效:

<!-- 下面的配置在hibernate4及以上失效,要将hbm和實體類放一起 -->
<!-- 指定Cat類為hibernate實體類 -->
<!-- <mapping class="cn.yunlingfly.xxx.bean.Cat"/> -->
<!-- 下面是使用xml檔案配置實體類映射 -->
<!-- <mapping resource="Cat.hbm.xml"/> -->
           

換成在HibernateUtil裡配置:

Configuration config = new Configuration()
												.configure("hibernate.cfg.xml")
												.addClass(Cat.class);
												//加載配置檔案
//			動态加載實體類配置檔案
//			Configuration config = new Configuration()
//													.addResource("Cat.hbm.xml")
//													.addResource("Student.hbm.xml")
//													.addClass("Cat.class");
//													.addClass("Student.class");
           

6 hibernate實體類 注解與hbm

@配置中,如果某屬性沒有配置,則預設該屬性名與資料表列名相同;而xml配置中,如果某屬性沒有配置,則認為該屬性沒有對應的資料庫列,不參與持久化。二者是截然不同的。

7 與Spring的結合

<bean id="dataSource"
	class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
	<property name="driverClassName"
		value="com.mysql.jdbc.Driver" />
	<property name="url"
		value="jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxxxx?useSSL=false" />
	<property name="username" value="root"></property>
	<property name="password" value="root"></property>
</bean>

<bean id="sessionFactory"
	class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
	destroy-method="destroy">
	<property name="dataSource" ref="dataSource" />
	<!-- 該package下的所有類都會被當成實體類加載 -->
	<property name="annotatedPackages" value="cn.yunlingfly.xxx.bean" />
	<!-- 使用@注解配置實體類 -->
	<property name="annotatedClasses">
		<list>
			<value>cn.yunlingfly.xxx.bean.Cat</value>
			<value>cn.yunlingfly.exam3.bean.Person</value>
			<value>cn.yunlingfly.exam3.bean.Email</value>
		</list>
	</property>
	<!-- 使用hbm來配置hibernate實體類 -->
	<property name="mappingLocations">
    	<value>classpath:/cn/yunlingfly/exam3/bean/*.hbm.xml </value>
	</property>
	<!-- hibernate屬性 -->
	<property name="hibernateProperties">
		<props>
			<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
			<prop key="hibernate.show_sql">true</prop>
			<prop key="hibernate.format_sql">true</prop>
			<prop key="hibernate.hbm2ddl.auto">validate</prop>
		</props>
	</property>
</bean>
           

8 org.hibernate.Query “UnusedDeclaration”

在hibernate5之後,這個包下的Query整個類都被廢棄了,根據代碼提示Deprecated. (since 5.2) use org.hibernate.query.Query instead,建議我們使用org.hibernate.query.Query這個類,使用方法例如:

List<TestEntity> testList=session.createQuery(hql,TestEntity.class).getResultList();
           

繼續閱讀