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