Hibernate environment Setup

Source: Internet
Author: User

---restore content starts---

1. Download the packages required by hibernate.

Note: sessionfactory CF = cfg.configure () in the higher version of Hibernate . buildsessionfactory (); This buildsessionfactory () method is discarded.
So when using a high-version hibernate package, load the hibernate configuration file and instantiate the Sessionfactory to swap the new method. There are tutorials online.

2. Add jar package to project Lib folder below, general project only need required the jar inside this package is enough (forget to add Hibernate3.jar this most important package)!

3. Because the underlying implementation of Hibernate is JDBC-based, you need to add the JDBC driver package for the database in Lib. Mine is Sqljdbc4.jar.

Note: There is also a small detail on the load of the package that will cause the program to appear abnormally. See Address

4. Below is a small chestnut used by hibernate!

Create Hibernate profile hibernate.cfg.xml (location under src folder)

1 <?XML version= ' 1.0 ' encoding= ' utf-8 '?>2 <!DOCTYPE hibernate-configuration Public3 "-//hibernate/hibernate Configuration DTD 3.0//en"4 "Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">5 6 <hibernate-configuration>7 8     <session-factory>9 Ten         <!--Database Connection Settings - One         < Propertyname= "Connection.driver_class">Com.mysql.jdbc.Driver</ Property> A         < Propertyname= "Connection.url">Jdbc:mysql://localhost/hibernate</ Property> -         < Propertyname= "Connection.username">Xxx</ Property> -         < Propertyname= "Connection.password">Xxxx</ Property> the  -         <!--JDBC Connection pool (use the built-in) - -         <!--  - <property name= "connection.pool_size" >1</property> +           - -         <!--SQL dialect - +         < Propertyname= "dialect">Org.hibernate.dialect.MySQLDialect</ Property> A  at         <!--Enable Hibernate ' s automatic session context management - -         < Propertyname= "Current_session_context_class">Thread</ Property> -  -         <!--Disable the Second-level cache - -         < Propertyname= "Cache.provider_class">Org.hibernate.cache.internal.NoCacheProvider</ Property> -  in         <!--Echo all executed SQL to stdout - -         < Propertyname= "Show_sql">True</ Property> to  +         <!--Drop and re-create the database schema on startup - -         <!--  the <property name= "Hbm2ddl.auto" >update</property> *           - $         <MappingResource= "Com/huxing/hibernate/model/student.hbm.xml"/>Panax Notoginseng  -     </session-factory> the  + </hibernate-configuration>

Note: Lines 26th and 27 cause an exception. For details, see

Establish the Student class:

1  Public classStudent {2      Private intID;3      PrivateString name;4      Private intAge ;5      Public intgetId () {6         returnID;7     }8      Public voidSetId (intID) {9          This. ID =ID;Ten     } One      PublicString GetName () { A         returnname; -     } -      Public voidsetName (String name) { the          This. Name =name; -     } -      Public intGetage () { -         returnAge ; +     } -      Public voidSetage (intAge ) { +          This. Age =Age ; A     } at       -}

Note: If the wrapper class that struts2 the form data is used directly for Hibernate's persistence class, there is no need to add an empty constructor to the class in accordance with the Struts2 wrapper class rule. In this example and for add!

Build the Student mapping file: Student.hbm.xml

<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/ Dtd/hibernate-mapping-3.0.dtd "><hibernate-mapping Package= "Com.huxing.hibernate.model">    <classname= "Student"Table= "Student">        <IDname= "id"column= "id">        </ID>        < Propertyname= "Name"type= "string"column= "Name"/>        < Propertyname= "Age"type= "int"column= "Age"/>    </class></hibernate-mapping>

Note: The above code is my entry when the online down code, there is a particularly important point is the ID of this tag, not set the type of this property, at runtime will cause an exception! See details

Other property tags can not set the type attribute!

Final Test:

1 Importorg.hibernate.Session;2 Importorg.hibernate.SessionFactory;3  import org.hibernate.cfg.Configuration;  5 Importcom.huxing.hibernate.model.Student;6 7  Public classStudenttest {8      Public Static voidMain (string[] args) {9Student A =NewStudent ();TenA.setid (123); OneA.setage (32); AA.setname ("Hello hibernate!"); -          -         Configuration cfg = new  configuration (); Sessionfactory CF =   cfg.configure (). Buildsessionfactory ();  -Session session =cf.opensession (); - session.begintransaction (); - Session.save (a); + session.gettransaction (). commit (); - session.close (); + cf.close (); A     } at}

Note: 1. Here say a I write this code encountered a problem is, import imports the problem of the package, the configuration class has multiple, exist in a number of different class package, import package must pay attention to which class package, or the program throws an exception is also extremely difficult to find, later in the import package should not ignore the arbitrary import!

2. One of the things I mentioned earlier, in the high version of Hibernate, 14 rows and 15 lines to load the configuration file and create the Sessionfactory method are discarded, to change the other way!

Hibernate environment Setup

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.