歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Hibernate中的不同主鍵生成策略下flush()方法的妙用

Hibernate中的不同主鍵生成策略下flush()方法的妙用

日期:2017/3/1 10:26:15   编辑:Linux編程

依舊讓代碼站出來說話。。這是一個Java Project。。

首先是位於src下的Hibernate核心配置文件hibernate.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://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  8. <property name="connection.url">jdbc:mysql://localhost:3306/jadyer?characterEncoding=UTF-8</property>
  9. <property name="connection.username">root</property>
  10. <property name="connection.password">jadyer</property>
  11. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  12. <property name="hibernate.show_sql">true</property>
  13. <property name="hibernate.format_sql">true</property>
  14. <!-- 批量讀取數據。建議值50。需要JDBC和底層數據庫的支持 -->
  15. <property name="hibernate.jdbc.fetch_size">50</property>
  16. <!-- 批量更新數據。建議值30 -->
  17. <property name="hibernate.jdbc.batch_size">30</property>
  18. <!-- 配置完這兩個屬性後,當我們向數據庫提交SQL時,就不會一次性把全部數據讀入內存 -->
  19. <!-- 而是按照一定的數量來批量讀取相應的數據,但最終是否會生效還取決於底層數據庫的支持 -->
  20. <!-- 有些數據庫就不支持這些參數。其中Oracle和SQLServer都支持,而MySQL貌似就不支持 -->
  21. <!-- 也可以通過以下方式編寫映射文件 -->
  22. <mapping resource="com/jadyer/hibernate/all.hbm.xml"/>
  23. <!--
  24. <mapping resource="com/jadyer/hibernate/User11.hbm.xml"/>
  25. <mapping resource="com/jadyer/hibernate/User22.hbm.xml"/>
  26. <mapping resource="com/jadyer/hibernate/User33.hbm.xml"/>
  27. -->
  28. </session-factory>
  29. </hibernate-configuration>

接下來是我們用到的三個實體類

  1. package com.jadyer.hibernate;
  2. import java.util.Date;
  3. public class User11 {
  4. private String id;
  5. private String name;
  6. private String password;
  7. private Date createTime;
  8. /*--三個屬性對應的setter和getter略--*/
  9. }
  10. package com.jadyer.hibernate;
  11. import java.util.Date;
  12. public class User22 {
  13. private int id;
  14. private String name;
  15. private String password;
  16. private Date createTime;
  17. /*--三個屬性對應的setter和getter略--*/
  18. }
  19. package com.jadyer.hibernate;
  20. import java.util.Date;
  21. public class User33 {
  22. private String id;
  23. private String name;
  24. private String password;
  25. private Date createTime;
  26. /*--三個屬性對應的setter和getter略--*/
  27. }
Copyright © Linux教程網 All Rights Reserved