Java uses MyEclipse to create hibernate projects a summary of many issues encountered

Source: Internet
Author: User

I've been doing this for two days. MyEclipse Create 1-to-many mappings for hibernate.

The lack of experience can be regarded as a terrible pit for me. The console is a constant error. ~ ~ ~ I almost collapsed.

1. Look at the MU-net hibernate one-to-many mapping tutorial, because the teacher MyEclipse Hibernate high version is 3.3, so the teacher himself

downloaded version 4.3, dragged to the project, and then all the files (Hibernate.cfg.xml;**.hbm.xml;sessionfactory tool class to

and the code for persistence classes and SQL generation tables) are handwritten ~ ~ ~

2. Then I took a look at the hibernate explanation of the horse soldier Teacher and read a chapter: whether to create a class first or create a table first. The conclusion is:

The theory is to create the class first and then create the table, but the actual project development will almost always first build the table and then build the class.

And MyEclipse is the ability to reverse-generate persisted classes based on tables (as well as various configuration files and tool classes), greatly simplifying the workload.

3. Then, first add hibernate rules for the project, automatically generate the Hibernate.cfg.xml tool class, and then build the table grade and student,

Grade's primary key is student foreign key, and then myeclipse connected to the database, click on the table right, select hibernate***, select Generate

The location of the persistence class, and then select the primary key generation mechanism: note here, though that native will automatically select the generator based on the underlying data base

System, however, it is best to explicitly select the mechanism (assigned: manually added; increment: auto increment; etc.).

Then select the Association for the table:

Note that the above two boxes, if ticked, will also help you to generate persistent classes and configuration files for the tables associated with this table, we tick! Save the table.

More words to operate more than a few times.

4. OK, so that the persistence class and configuration file corresponding to the database table have been generated

-----------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------

But the problem is here, we do a one-to-many mapping, Grade.hbm.xml has <one-to-many class= "file. Student "/> Genus

Sex is normal, because the class to correspond to more than one student, but myeclipse for us to generate Student.hbm.xml file, automatically added

<many-to-one name= "Grade" class= "file. Grade "fetch=" select ">
<column name= "gid"/>
</many-to-one>

Our demo does not need to be many-to-one, so this attribute field of this file should be deleted.

5. Let's look at Student.java, the Persistence class:

1     Private Integer SID; 2     Private Grade Grade; 3     Private String sname; 4     Private String sex;

We added a foreign key to the student table (corresponding to the primary key of the grade table), but the myeclipse generated the persistence class and added a

A grade class of attribute grade, and configured a getter, setter method, which is not required! Delete immediately! Grade parameters inside the construction method

Also want to delete!

6. In this way, we can write the test class with peace of mind

1  Public classTTest {2     PrivateSession session;3     PrivateTransaction Transaction;4 5 @Before6      Public voidinit () {7Session =hibernatesessionfactory.getsession ();8Transaction =session.begintransaction ();9     }Ten  One @After A      Public voiddistory () { - transaction.commit (); - session.close (); the     } -  - @Test -      Public voidtodo () { +Grade g =NewGrade (104, "Java", "Java Learning Class 1"); -Student s =NewStudent (12, "Orange Mulberry", "male"); + g.getstudents (). Add (s); A Session.save (g); at Session.save (s); -     } -}

This is the test class written with the JUnit Unit Test tool, the first two methods are some initialization, destroy operation, the actual operation details in the TODO () method

, let's take a look at this method:

First build a class, class number (GID) is the student table of the foreign key is 104;

Build another student number 12, note here is no foreign key field GID, the student table of the persistent class student inside is not;

Then get the class's students attribute (the persistence class is already generated, the type is a set table for the students who store the class), and add this

A student s, then Save,save.

Next look at the resulting database table:

------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------------

In this way, when adding students to the class table, it automatically matches the GID field value of the student table.

End of the flower ~~~~~~~~~

Java uses MyEclipse to create a summary of the issues encountered with hibernate projects

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.