Hibernate Basic Configuration (scaffolding)

Source: Internet
Author: User

One. Hibernate configuration takes 4 steps

1. Configure the Hibernate.cfg.xml file, configure the basic database connection , and some other such as: dialect, Format_sql, Show_sql, Hbm2ddl.auto and so on.

2. Create the entity class.

3. Create a mapping file for the entity class, such as: if the entity class is News, then the Entity class mapping file is: News.hbm.xml.

4. Perform the test.

1. Configuring the Hibernate.cfg.xml File

<hibernate-configuration>    <session-factory>        <!--Configure basic information for HIBERNATE0 -        < Propertyname= "Hibernate.connection.username">Root</ Property>        < Propertyname= "Hibernate.connection.password">Root</ Property>        < Propertyname= "Hibernate.connection.driver_class">Com.mysql.jdbc.Driver</ Property>        < Propertyname= "Hibernate.connection.url">Jdbc:mysql:///test</ Property>                <!--Configuring HIBERNATE0 Additional information -<property name= "Hibernate.dialect" >org.hibernate.dialect.MySQL5InnoDBDialect</property>< Propertyname= "Hibernate.format_sql">True</ Property>        < Propertyname= "Hibernate.show_sql">True</ Property>        < Propertyname= "Hibernate.hbm2ddl.auto">Update</ Property>                <!--map to News.hbm.xml -        <MappingResource= "Org/blanck/entities/news.hbm.xml"/>    </session-factory></hibernate-configuration>

2. Create the Entity class:

 Public class News {        private  Integer ID;     Private String title;     Private Date Newstimes;}

Omit Get/set method
Omitted parameter/parameterless constructor

3. Configure the mapping file for the entity class (using the Eclipse hibernatetools-update-4.1.1.final plugin, you can generate the configuration file)

<hibernate-mapping>    <classname= "Org.blanck.entities.News"Table= "T_news">        <IDname= "id"type= "Java.lang.Integer">            <columnname= "ID" />                       <!--the generation strategy of the database -            <Generatorclass= "Native" />        </ID>        < Propertyname= "title"type= "Java.lang.String">            <columnname= "TITLE" />        </ Property>        < Propertyname= "Newstimes"type= "Java.util.Date">            <columnname= "Newstimes" />        </ Property>    </class></hibernate-mapping>

4. The following is the test class:

 Public classHibernatetest {//HIBERNATE0 3 interfaces, session factory, session, transaction    Privatesessionfactory sessionfactory; PrivateSession session; PrivateTransaction Transaction; /*** Before testing, Initialize method*/@Before Public voidinit () {System.out.println ("Initialize Method ..."); //1. Initialize the Configuration object to create the objects needed to build the SessionfactoryConfiguration Configuration =NewConfiguration (). Configure (); Serviceregistry Serviceregistry=NewServiceregistrybuilder (). Applysettings (Configuration.getproperties ()). Bui        Ldserviceregistry (); //2. Building Sessionfactory objects with ServiceregistrySessionfactory =configuration.buildsessionfactory (serviceregistry); //open a Session with sessionfactory, turn on the transactionSession =sessionfactory.opensession (); Transaction=session.begintransaction (); }        /*** Test complete, destroy object*/@After Public voiddestory () {System.out.println ("Destruction Method ..."); //COMMIT TRANSACTION, close Session, close sessionfactoryTransaction.commit ();        Session.close ();    Sessionfactory.close (); }        /*** Test Method*/@Test Public voidTest () {System.out.println ("Test Method"); //Create a News object to testNews news =NewNews ("Hellokitty",NewDate ());    Session.save (news); }}

5. Error, if it appears: You have a error in your SQL syntax;

Check the manual-corresponds to your MySQL server version for the right syntax-use-near ' type=innodb ' on line 6

Please put:

<name= "Hibernate.dialect"> Org.hibernate.dialect.MySQLInnoDBDialect</Property>

Modified to:

<name= "Hibernate.dialect"> Org.hibernate.dialect.MySQL5InnoDBDialect</Property>

This is primarily due to a database version issue, using

The mysql5innodbdialect can be applied to versions after MySQL 5.1.

If the database was originally present , the above error will not occur.

Hibernate Basic Configuration (scaffolding)

Related Article

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.