Detailed introduction to the preparation of the Hibernate main list file,

Source: Internet
Author: User

Detailed introduction to the preparation of the Hibernate main list file,

Detailed introduction to the preparation of the Hibernate main list file

1. hi-fi configuration file

Method 1 create the hibernate. cfg. xml file under the src directory of the project

When hi-fi starts loading, the hibernate. cfg. xml file is scanned in the src directory of the project by default, and then the configuration is loaded.
Public class H3Utils {private static SessionFactory factory = new Configuration (). configure (). buildSessionFactory ();/*** get the thread-bound session * @ return */public static Session getCurrentSession () {return factory. getCurrentSession ();}}

Method 2: Create the hibernate. cfg. xml file in any directory of the project

In this way, you need to manually specify the path of the preparation file when using it.

Public class HBUtils {// provides a factory (chained operation) private static SessionFactory factory = new Configuration (). configure ("android/longs/study/config/hibernate. cfg. xml "). buildSessionFactory ();/*** get a new Session * @ return */public static Session openSession () {return factory. openSession ();}/*** get the bound session in the current thread * @ return */public static Session getCurrentSession () {return factory. getCurrentSession ();}}

2. hi-fi configuration file details

<? 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> <! -- 1 Basic 4 items --> <! -- 1.1 configure the load Driver --> <property name = "hibernate. connection. driver_class"> com. mysql. jdbc. Driver </property> <! -- 1.2 database address --> <! -- For example, jdbc: mysql: // 192.168.1.1: 3306/test_java_study? UseUnicode = true & characterEncoding = UTF-8 --> <property name = "hibernate. connection. url"> url </property> <! -- 1.3 login database username --> <property name = "hibernate. connection. username"> root </property> <! -- 1.3 login database Username password --> <property name = "hibernate. connection. password"> 123456 </property> <! -- 2 dialect --> <property name = "hibernate. dialect"> org. hibernate. dialect. MySQL5Dialect </property> <! -- 3 optimization settings during development --> <! -- 3.1 display the production SQL statement --> <property name = "hibernate. show_ SQL"> true </property> <! -- 3.2 display SQL in formatting mode --> <property name = "hibernate. format_ SQL"> true </property> <! -- 4 Table Creation --> <property name = "hibernate. hbm2ddl. auto"> update </property> <! -- 5 cancel bean validation --> <property name = "javax. persistence. validation. mode"> none </property> <! -- 6 bind the session to * hibernate session management in the local thread: only use. * When thread is configured in cfg. xml, SessionFactory provides getCurrentSession. * At the underlying layer of hibernate, The ThreadLocal Thread Local variable can be used to share data in a thread. * ** Get () # map. get (Thread) *** set (value) # map. put (Thread, value) *** remove () # map. remove (Thread) --> <property name = "hibernate. current_session_context_class "> thread </property> <! -- Integrate c3p0 --> <property name = "hibernate. connection. provider_class"> org. hibernate. connection. C3P0ConnectionProvider </property> <! -- Object Class ing file --> <mapping resource = "android/longs/study/home/servlet/model/MobleHomeModel. hbm. xml "/> </session-factory> 

Creating the fourth table

The value can be "create": The table is created every time. If the table already exists, the table is deleted. (TEST) The table exists after the program ends. Create-drop: Creates a table every time. If the table already exists, it is deleted. (TEST) after the program ends, the table will be deleted. Note: You must execute factory. close (). Otherwise, it is the same as "create". update: If the table does not exist, it will be created. If yes, the ing relationship (ing file-table) will be maintained. [] Note: Only add, but not delete. Validate: During running, the ing between the ing file and the table is verified. If one-to-one correspondence program runs normally, an exception is thrown if no correspondence exists.

Level 2 Cache preparation

<! -- Configure isolation level --> <property name = "hibernate. connection. isolation"> 4 </property> <! -- Enable Level 2 cache --> <property name = "hibernate. cache. use_second_level_cache"> true </property> <! -- Provider --> <property name = "hibernate. cache. provider_class"> org. hibernate. cache. EhCacheProvider </property> <! -- Enable query cache --> <property name = "hibernate. cache. use_query_cache"> true </property> <! -- Second-level cache monitoring --> <property name = "hibernate. generate_statistics"> true </property> <! -- Class cache --> <! -- The Customer class under the com package --> <class-cache usage = "read-write" class = "com. Customer"/> <! -- Order package under com --> <class-cache usage = "read-write" class = "com. Order"/> <! -- Set cache --> <! -- OrderSet set in the Customer class under the com package --> <collection-cache usage = "read-write" collection = "com. Customer. orderSet"/>

Note:

The first-level cache caches objects.

The second-level cache caches data.

If no class cache is performed for the objects in the cache in the second-level cache, the OID query is executed.

If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.