Detailed introduction to the Hibernate main manifest file configuration
1 Hibernate manifest configuration file
Method 1 Create a file in the project src directory
Hiernate When loading starts,Will go to the project by defaultsrcScan in the directory document,Then load the preparation
public class H3Utils { private static SessionFactory factory = new Configuration().configure().buildSessionFactory(); /** * Get the thread-bound session * @return */ public static Session getCurrentSession(){ return (); } }
Method 2 Create a file in any directory in the project
In this way, you need to manually specify the path to the configuration file when using it.
public class HBUtils { //Providing a factory (chain operation) private static SessionFactory factory = new Configuration() .configure("android/longs/study/config/") .buildSessionFactory(); /** * Get a new session * @return */ public static Session openSession(){ return () ; } /** * Get the session bound in the current thread * @return */ public static Session getCurrentSession(){ return (); } }
2 Hibernate manifest configuration file details
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "/dtd/hibernate-configuration-3."> <hibernate-configuration> <session-factory> <!-- 1 Basic4item --> <!-- 1.1 Load driver configuration --> <property name=".driver_class"></property> <!-- 1.2 Database address --> <!-- like jdbc:mysql://192.168.1.1:3306/test_java_study?useUnicode=true&amp;characterEncoding=UTF-8--> <property name="">url</property> <!-- 1.3 Log in to the database username --> <property name="">root</property> <!-- 1.3 Log in to the database username密码 --> <property name="">123456</property> <!-- 2 dialect --> <property name="">.MySQL5Dialect</property> <!-- 3 During development,Optimize settings --> <!-- 3.1 Show productionsqlStatement --> <property name="hibernate.show_sql">true</property> <!-- 3.2 Format displaysql --> <property name="hibernate.format_sql">true</property> <!-- 4 Table creation --> <property name="hibernate.">update</property> <!-- 5 Cancelbeancheck --> <property name="">none</property> <!-- 6 WillsessionBind when in the local thread * hibernate session manage : 只Will使用。 * When Configuration thread,SessionFactorysupply getCurrentSession() Will可以使用。 * hibernateLow-level use ThreadLocal Thread local variables,Data can be shared in one thread。 *** get() ##(Thread) *** set(value) ##(Thread,value) *** remove() ##(Thread) --> <property name="hibernate.current_session_context_class">thread</property> <!-- Integrationc3p0 --> <property name=".provider_class">.C3P0ConnectionProvider</property> <!-- Object class Mapping files --> <mapping resource="android/longs/study/home/servlet/model/" /> </session-factory> </hibernate-configuration>
About Item 4 The creation of the table
The value can be create : Tables will be created every time,If the table already exists, delete。(test)After the program ends,The table exists。 create-drop:Tables will be created every time,If the table already exists, delete。(test)After the program ends,The table will be deleted。 Notice:Must be executed () Otherwise with“create”same update : If the table does not exist,Will create。If there is,Will maintain the corresponding relationship(Mapping files - surface)【】 Notice:Only responsible for adding,But no deletion。 validate : Runtime,Will verify Mapping files and surface Corresponding relationship,If the corresponding program is running normally,If the exception is not thrown。
Level 2 cache configuration
<!-- Configure isolation level --> <property name="">4</property> <!-- Turn on Level 2 cache --> <property name=".use_second_level_cache">true</property> <!-- Provider --> <property name=".provider_class"></property> <!-- Turn on query cache --> <property name=".use_query_cache">true</property> <!-- Level 2 cache monitoring --> <property name="hibernate.generate_statistics">true</property> <!-- Class cache --> <!-- comPackedCustomerkind --> <class-cache usage="read-write" class=""/> <!-- comPackedOrderBag --> <class-cache usage="read-write" class=""/> <!-- Collection cache --> <!-- comPackedCustomerkind中的orderSetgather --> <collection-cache usage="read-write" collection=""/>
Notice
The first-level cache caches the object
The secondary cache caches data
If the objects in the collection cache in the secondary cache are not cached in class, the OID query will be performed.
If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!