MyEclipse generating Hibernate entity classes from database backwards

Source: Internet
Author: User
Tags tomcat server

This is divided into two operations: database operations and MYECLIPSE operations

Relational mappings are: Single bidirectional 1-n, N-1, N-n.

Mapping relationships are determined by table relationships in the database

Of course, you can work with just one table.

Single bidirectional 1-n, n-1 by two tables

Single bidirectional n-n consists of three tables, where the third table joins another two tables

The following is the creation of n-n myeclipse to reverse-generate hibernate entity classes from a database:

Database operations:

CREATE TABLE: Person_inf

Address_inf

Person_address

The relationship is: person_address create foreign key

It is important to note that the primary keys of the two tables, Person_inf and Address_inf, are identified as self-increment

MyEclipse Operation:

1. Create a new WEB project item

2. Create a new DB database connection:

Window->show view->other-> Search db

Right-click Space new to create a new DB database connection:

Finish finished

3. Right-click on the project name->myeclipse->add Hibernate capab ....

Next

Next

Next

Finish finished

4. Automatically generate entity classes:

Open DB Database connection

Locate the database name point, locate the dbo point, and find the table point-------Select tables right-click->hibernate Reverse Engineering.

Click Browse

Check

Next step

Next step

Click Person_inf and Address_inf two tables to select the value in the ID Generator:native


Click Finish to complete

Generated as follows:

5. Modify part of the code

(1). Mappings add AddressInf.hbm.xml and PersonInf.hbm.xml in Hibernate.cfg.xml

(2). Add cascade= "All" Cascade in the set label in AddressInf.hbm.xml and PersonInf.hbm.xml two configurations respectively

(3). Put the code in the Personinf.java:

Code in the Save () method

Switch

1      Public voidSave (Personinf transientinstance) {2Log.debug ("Saving Personinf instance");3         Try {4Transaction t=getsession (). BeginTransaction ();5 getsession (). Save (transientinstance);6 t.commit ();7Log.debug ("Save Successful");8}Catch(RuntimeException re) {9Log.error ("Save Failed", re);Ten             Throwre; One         } A}

You can then use the index.jsp call again.

The 6.index.jsp call uses the

Click, click Add to add a Tomcat server

Ok

Run the project running Tomcat server

Finally get:

MyEclipse generating Hibernate entity classes from database backwards

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.