Build hibernate5.0 Environment under eclipse

Source: Internet
Author: User

    1. Hibernate-introduced jar package: Hibernate-release-5.0.12.final.zip
    2. Database driver: mysql-connector-java-5.1.46

Two. Install Hibernate plugin

Open Eclipse, click Help-->eclipse Marketplace, enter: Hibernate tools, then click the Goa button to find JBoss tools

Click Install installation

Select Hibernate Tools and click Confrm Installation. Restart Eclipse when the installation is complete.

Three. Create a project

1. Create a new project Hibernatedemo, set up the Lib folder under Project. Open the directory for the jar package, import the jar package under lib/required and the database, add to build path

Create a new file under SRC

Click Next, default file name, click Next, configure database information

Select UTF-8 encoding, click Finish, generate the Hibernate.cfg.xml configuration file content as follows

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://www.hi Bernate.org/dtd/hibernate-configuration-3.0.dtd "><hibernate-configuration>    <session-factory>        < Propertyname= "Hibernate.connection.driver_class">Com.mysql.jdbc.Driver</ Property>        < Propertyname= "Hibernate.connection.password">A123</ Property>        < Propertyname= "Hibernate.connection.url">Jdbc:mysql://localhost:3306/tb_test</ Property>        < Propertyname= "Hibernate.connection.username">Sherman</ Property>        < Propertyname= "Hibernate.dialect">Org.hibernate.dialect.MySQLDialect</ Property>                    </session-factory></hibernate-configuration>

Note that the name attribute of <session-factory name= "MySQL"> is removed, Otherwise report org.hibernate.engine.jndi.JndiException exception, add some configuration in this file,

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://www.h Ibernate.org/dtd/hibernate-configuration-3.0.dtd "><hibernate-configuration>    <session-factory>        < Propertyname= "Hibernate.connection.driver_class">Com.mysql.jdbc.Driver</ Property>        < Propertyname= "Hibernate.connection.password">A123</ Property>        < Propertyname= "Hibernate.connection.url">Jdbc:mysql://localhost:3306/tb_test</ Property>        < Propertyname= "Hibernate.connection.username">Sherman</ Property>                <!--Configure database dialect -        < Propertyname= "Hibernate.dialect">Org.hibernate.dialect.MySQL5Dialect</ Property>        <!--Console Print SQL statements -        < Propertyname= "Show_sql">True</ Property>        <!--Formatting SQL -        < Propertyname= "Format_sql">True</ Property>        <!--Update the database at startup based on configuration -        < Propertyname= "Hibernate.hbm2ddl.auto">Update</ Property>        <!--Configure connection Pool connection number -        < Propertyname= "Connection.pool_size">20</ Property>                <!--Registering Entity Mapping classes -        <Mappingclass= "Com.gdut.app.entity.News"/>    </session-factory></hibernate-configuration>

Create a new package com.gdut.app.entity under SRC, storing the Persistence class News,news class code as follows

 Packagecom.gdut.app.entity;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;ImportJavax.persistence.GenerationType;Importjavax.persistence.Id;Importjavax.persistence.Table; @Entity @table (name= "News_info") Public classNews {@Id @generatedvalue (strategy=generationtype.identity)PrivateInteger ID;PrivateString title;PrivateString content; PublicNews () {} PublicNews (Integer ID, string title, string content) { This. ID =ID;  This. title =title;  This. Content =content;} PublicInteger getId () {returnID;} Public voidsetId (Integer id) { This. ID =ID;} PublicString GetTitle () {returntitle;} Public voidSettitle (String title) { This. title =title;} PublicString getcontent () {returncontent;} Public voidsetcontent (String content) { This. Content =content;} @Override PublicString toString () {return"News [id= + ID +", title= "+ title +", content= "+ content +"] ";}}

To write a test class:

 Packagecom.gdut.app.entity;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.Transaction;Importorg.hibernate.cfg.Configuration;Importorg.junit.Test; Public classbeantest {@Test Public voidbeantest () {//final Standardserviceregistry serviceregistry = new Standardserviceregistrybuilder ()//. Configure ("Hibernate.cfg.xml"). Build ();//        //sessionfactory SF = new Metadatasources (serviceregistry). Buildmetadata (). Buildsessionfactory (); //two ways to get sessionfactoryConfiguration cfg =NewConfiguration (). Configure (); Sessionfactory SF=cfg.buildsessionfactory (); Session Sess=sf.opensession (); Transaction Transaction=sess.begintransaction (); News N=NewNews (); N.setcontent ("Graduated from Guang Gong"); N.settitle ("Graduation Season");        Sess.save (n);        Transaction.commit ();            Sess.close (); }}

Tested successfully

or through the mapping file

Under the Com.gdut.app.entity package resume a News.hbm.xml mapping configuration file, modify the class property of Genarator to Active

<?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 2018-5-22 23:45:23 by Hibernate Tools 3.5.0.Final -<hibernate-mapping>    <classname= "Com.gdut.app.entity.News"Table= "NEWS">        <IDname= "id"type= "Java.lang.Integer">            <columnname= "ID" />            <Generatorclass= "Native"/>        </ID>        < Propertyname= "title"type= "Java.lang.String">            <columnname= "TITLE" />        </ Property>        < Propertyname= "Content"type= "Java.lang.String">            <columnname= "CONTENT" />        </ Property>    </class></hibernate-mapping>

Configuring in Hibernate.cfg.xml

<resource= "Com/gdut/app/entity/news.hbm.xml"/>

Test validation succeeded.

The entire engineering structure

Build hibernate5.0 Environment under eclipse

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.