Hibernate basic configuration (scaffolding), hibernate scaffolding

Source: Internet
Author: User

Hibernate basic configuration (scaffolding), hibernate scaffolding

1. Four steps are required for hibernate Configuration

1. Configure the hibernate. cfg. xml file.Basic Database Connection, There are some other examples:Dialect, format_ SQL, show_ SQL, hbm2ddl. autoAnd so on.

2. Create an object class.

3. Create a ing file for the object class. For example, if the object class is News, the ing file for the object class is News. hbm. xml.

4. perform the test.

 

1. Configure the hibernate. cfg. xml file

<Hibernate-configuration> <session-factory> <! -- Configure basic information about hibernate0 --> <property name = "hibernate. connection. username "> root </property> <property name =" hibernate. connection. password "> root </property> <property name =" hibernate. connection. driver_class "> com. mysql. jdbc. driver </property> <property name = "hibernate. connection. url "> jdbc: mysql: // test </property> <! -- Configure other information of hibernate0 --> <property name = "hibernate. dialect "> org. hibernate. dialect. mySQL5InnoDBDialect </property> <property name = "hibernate. format_ SQL "> true </property> <property name =" hibernate. show_ SQL "> true </property> <property name =" hibernate. hbm2ddl. auto "> update </property> <! -- Maps to News. hbm. xml --> <mapping resource = "org/blanck/entities/News. hbm. xml "/> </session-factory> 

2. Create an object class:

Public class News {private Integer id; private String title; private Date newsTimes ;}

// Omit the get/set Method
// Omit the parameter/parameter-free Constructor

 

3. Configure the ing file for the object class (you can generate a configuration file using the Eclipse hibernatetools-Update-4.1.1.Final plug-in)

<Hibernate-mapping> <class name = "org. blanck. entities. news "table =" T_NEWS "> <id name =" id "type =" java. lang. integer "> <column name =" ID "/> <! -- Database Generation Policy --> <generator class = "native"/> </id> <property name = "title" type = "java. lang. string "> <column name =" TITLE "/> </property> <property name =" newsTimes "type =" java. util. date "> <column name =" NEWSTIMES "/> </property> </class> 

 

4. The following is a test class:

Public class HibernateTest {// three hibernate0 interfaces, Session factory, session, Transaction private SessionFactory sessionFactory; private Session session; private transaction Transaction;/*** before testing, initialization Method */@ Before public void init () {System. out. println ("initialization method... "); // 1. initialize the Configuration object and create the Configuration configuration = new Configuration () object required to build SessionFactory (). configure (); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder (). applySettings (configuration. getProperties ()). buildServiceRegistry (); // 2. use ServiceRegistry to build the SessionFactory object sessionFactory = configuration. buildSessionFactory (serviceRegistry); // use sessionFactory to open a Session and enable transaction session = sessionFactory. openSession (); transaction = session. beginTransaction ();}/*** the test is completed. Destroy the object */@ After public void destory () {System. out. println ("destruction method... "); // submit the transaction, close the Session, and close SessionFactory transaction. commit (); session. close (); sessionFactory. close ();}/*** Test Method */@ test public void Test () {System. out. println ("Test Method"); // create a News object to test News news = new News ("helloKitty", new Date (); session. save (news );}}

 

5. Error. if:You have an error in your SQL syntax;

Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Type = innodb' at line 6

Put:

<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

Modify:

<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

This is mainly because of database version problems.

MySQL5InnoDBDialect can be applied to Versions later than mysql 5.1.

If the databaseOriginally existed., The above error will not appear.

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.