The first hibernate project under Intellij idea

Source: Internet
Author: User

Reference: IntelliJ configuring Hibernate auto-generated hbm.xml files create hibernate projects from scratch with IntelliJ IDEA14

What I'm going to talk about here is how it's created, probably more primitive, more like the way you create hibernate projects under Eclipse, and I think it helps to understand how to create a hibernate project under IntelliJ idea.

You first need to create a project under Intellij idea, equivalent to the workspace (workspace) under Eclipse, of course, if you choose how to create a hibernate project at this point, Intellij This hibernate project is created under project after the project is created successfully. It might look a little strange, okay, we can delete the default creation, and then create our module, which is equivalent to project under Eclipse.

Create Module--"Select the Java Enterprise tab, click on the right, tick Web application and Hibernate, as follows:

Select Configure ... in the lower right corner to select the module Library:

Click Next, enter the name of the module, here I named: hibernate_00, such as:

Wait for Hibernate related jar package to download ...

You will also need to add the Mysql-jdbc jar package and the JUnit jar package (the JUnit jar package can actually be added without adding it as needed):

Right-click on hibernate_00, select Open Module Settings, or press F4:

Select from Maven.

Download the JUnit jar package in the same way and add it:

To make it easier to create hibernate projects later, you can create templates for hibernate profiles:

The contents of the Hibernate.cfg.xml template are as follows (the corresponding modifications should be made in practice):

<?xml version= ' 1.0 ' encoding= ' utf-8 '? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration dtd//en" "Http://www.hibernate.  Org/dtd/hibernate-configuration-3.0.dtd ">


       Similarly, create an object/Relationship mapping profile template, where the Entity2.hbm.xml template is created as follows (and should be modified in practice):

<?xml version= ' 1.0 ' encoding= ' utf-8 '? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">
Now that we are ready to do the work, we can create hibernate profiles under the template under SRC Hibernate.cfg.xml: (reference: Creating hibernate.cfg.xml)
<?xml version= ' 1.0 ' encoding= ' utf-8 '? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration dtd//en" "Http://www.hibernate.  Org/dtd/hibernate-configuration-3.0.dtd ">
Create the Students class under SRC:
Import java.util.date;/** * Created by Dreamboy on 2016/5/15. *///student public class Students {//1. Must be a common class//2. You must provide a public default constructor with no parameters//3. Property Private//4. Properties Setter/getter Encapsulation Priva te int sid; Study number private String sname; Name Private String gender; Sex private Date birthday; Date of birth private String address;         Address public Students () {} public Students (int sid, String sname, String gender, Date birthday, string address) {        This.sid = SID;        This.sname = sname;        This.gender = gender;        This.birthday = Birthday;    this.address = address;    } public int GetSID () {return SID;    } public void Setsid (int sid) {this.sid = SID;    } public String Getsname () {return sname;    } public void Setsname (String sname) {this.sname = sname;    } public String Getgender () {return gender;    } public void Setgender (String gender) {This.gender = gender;     } public Date Getbirthday () {   return birthday;    } public void Setbirthday (Date birthday) {this.birthday = birthday;    } public String getaddress () {return address;    The public void setaddress (String address) {this.address = address; } @Override Public String toString () {return "students{" + "sid=" + Sid + ", s                Name= ' + sname + ' \ ' + ', gender= ' + gender + ' \ ' + ', birthday= ' + birthday +    ", address= ' + address + ' \ ' + '} '; }}

Create a configuration file for the object/relationship map under SRC (created from the template) Students.hbm.xml
<?xml version= ' 1.0 ' encoding= ' utf-8 '? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">


Add mappings for students objects in the Hibernate.cfg.xml configuration file. (added in the previous configuration file)

<mapping resource= "Students.hbm.xml"/>


Create the test directory under module, in order to be able to create Java classes in this directory, you need to modify the directory to Sources root:

at this point, you can create a Java class: Studentstest.java

(This is used for annotations in JUnit @Test, @Before, @After)

Import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.transaction;import Org.hibernate.cfg.configuration;import Org.hibernate.service.serviceregistry;import Org.hibernate.service.serviceregistrybuilder;import Org.junit.after;import Org.junit.before;import Org.junit.Test ; Import java.util.date;/** * Created by Dreamboy on 2016/5/15.    *///test class public class Studentstest {private Sessionfactory sessionfactory;    Private session session;    Private Transaction Transaction;        @Before public void init () {//Create config Object configuration config = new configuration (). Configure (); Create a Service registration object Serviceregistry Serviceregistry = new Serviceregistrybuilder (). Applysettings (Config.getproperties ()). Bui        Ldserviceregistry ();        Create Session Factory Object sessionfactory = Config.buildsessionfactory (serviceregistry);        Conversation Object session = Sessionfactory.opensession ();    Open transaction Transaction = Session.begintransaction (); } @After Public void Destory () {transaction.commit ();//Submit Transaction session.close ();//Close session sessionfactory.close (); /close Session factory} @Test public void Testsavestudents () {//Generate student object Students s = new Students (1, "Zhang Sanfeng", "male",        New Date (), "Wudangshan"); Session.save (s); Save object into Database}}


In the Hibernate.cfg.xml configuration file, you can know that here I configure the database to use, called hibernate, so we need to use MySQL to create a hibernate database before running the program, without creating a students table.

Run Studentstest.java, you may receive the following error:

There is probably an error in the location of the Java compiler output, so configure the output path:

Select here under the current module under the Classes folder:

Run the program again:

The program runs successfully, we can look at the database, we will find that the program has helped us to automatically create the data table, and added a piece of data:

The Hibernate project runs successfully.



The first hibernate project under Intellij idea

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.