hibernate_5.2.9 and Mysql_5.7.17 encountered a series of errors ...

Source: Internet
Author: User
Tags mysql version

Written in front of the words: watching video, reading, about hibernate and MySQL are some of the old version of the information. Now it's not so easy to find hibernate and MySQL, so it's a lot of trouble to deal with the new version in the old version. Ask Niang, ask Google finally the whole good, simple introduction of their own mental journey.

IDE version: spring-tool-suite-3.8.2.release-e4.6.1-win32-x86_64;

Hibernate version: Hibernate-release-5.2.9.final;

MySQL version: mysql-5.7.17-winx64;

1. First build a simple Java project, mainly in order to learn hibernate, should not build Dynamic Web project it, Bo Master coquettish to the project took a name, called Hibernate, haha;

2. Create a new Lib folder in hibernate to place the required jar packages;

3. Copy the 10 cute jar packages in the \hibernate-release-5.2.9.final\lib\required package to the Lib folder, not yet ... Find my collection for a long time Mysql-connector-java-5.1.40-bin.jar bag, also copied into the Lib folder, the package has been and mysql_5.7.17 a number of basic love collision, certainly no problem;

4. Select the 11 packages, right-bulid path-add to build path and add to the development environment;

5. LUN home to start the configuration of Hibernate.cfg.xml, here, the first incompatibility occurred----MySQL dialect problem;

<?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 "><property name= "Hibernate.dialect" >org.hibernate.dialect.MySQL5InnoDBDialect</property><property name= "Hibernate.hbm2ddl.auto" >create</property><property name= "Hibernate.current_session_context_class" >thread</property><property name= "Hibernate.show_sql" >true</property> <mapping resource= "Com/model/user.hbm.xm L "/> </session-factory>

It is the line marked with blue (the red line is to be said later)! Be sure to note that if it is a version of MySQL after 5.2, the dialect should be written org.hibernate.dialect.MySQL5InnoDBDialect instead of Org.hibernate.dialect.MySQLInnoDBDialect; Why, look at the SQL statements displayed on the console. I know, mysql back pot ...

6. Create a package---com.model, set up the user class in the package, with the following code:

Package Com.model;public class User {private int id;private string Username;private string Password;private string Nicknam e;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;} Public String Getnickname () {return nickname;} public void Setnickname (String nickname) {this.nickname = nickname;}}

7. Build Users.hbm.xml in the Com.model package, and rub with the user class!

<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd "><!--Generated 2017-4-4 15:59:01 by Hibernate Tools 3.5.0.Final--><            hibernate-mapping> <class name= "Com.model.User" table= "User" > <id name= "id" type= "int" > <column name= "ID"/> <generator class= "native"/> </id> <property name= "us Ername "type=" java.lang.String "> <column name=" USERNAME "/> </property> <proper        Ty name= "password" type= "java.lang.String" > <column name= "password"/> </property> <property name= "nickname" type= "java.lang.String" > <column name= "nickname"/> </property > </class>

8. Write the test class in the---com.test package, and test if hibernate and MySQL are related (sexual) relationship, haha!

Package Com.test;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;import Org.junit.test;import Com.model.user;public class TestUser {@Testpublic void test01 () {Configuration configuration = new configuration (). Configure ();  Sessionfactory Sessionfactorn = Configuration.buildsessionfactory (); Session session = Sessionfactorn.getcurrentsession (); Open transaction session.begintransaction ();//Instantiate user U = New user (), U.setusername ("HLS"); U.setnickname ("Han Yu yarn"); U.setpassword ("123");//Save Data Session.save (U);//COMMIT Transaction; Session.gettransaction (). commit ();}}

  

The Focus!!! The Red Background section generates a session for the hibernate--5.x version, and 4. The X version generates the session in a way that requires serviceregistry. Remember the scarlet Letter above, when 5. X the above version needs to be added in the configuration file. The session generated in this way is Transaction.commit () and will be closed automatically!

9. Run this test class and discover it's ready to use! LUN Home is the new, great God to point guidance, Meng new can follow my method a try! Do you? ~ ~

Complement diagram:

File Structure Diagram---

Run the result diagram---

hibernate_5.2.9 and Mysql_5.7.17 encountered a series of errors ...

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.