As we all know, hibernate can reverse generate entity classes and configuration files, but the forward may not be a lot of people, what is positive, what is reverse, positive refers to the entity Class (entity), Then the automatic creation of database tables is implemented by Hibernate forward engineering according to your entity class. The so-called reverse, as the name implies is to have the data table, and then according to hibernate reverse engineering to implement the project's entity class automatic generation and mapping file (xx.hbm.xml), then I will give you a brief introduction.
Believe that most beginners are implemented by reverse, the reverse is also very simple, so here I do not introduce specific reverse steps.
Now let's look at the forward project:
Before you create an entity class, let's get ready for work and what to do. ———— you first create the table space and the user, and authorize it, next look at the code that creates the user:
1. Creation is an entity Class (entity), where I created two entity classes, student classes (Student) and classroom classes (Teacher), I have created, the source code is also put up,
Student entity class Access address: Student,
Teacher entity class Access address: Teacher
Here are the getter and setter methods, please create them yourself.
2. Create Hibernate.cfg.xml configuration file, this file if you directly use the MyEclipse directly generated on it, here is not demonstrated, access to the profile address: Hibernate.cfg.xml
Be sure to pay attention to the red box in the picture and the following tips, I have a bug in this piece of time to solve the problem of Cai resolved off ...
3. Then create the Student class mapping file (Student.hbm.xml) and the Teacher class mapping file (Teacher.hbm.xml), (Here are examples of many to many cases, a one-to-many and more pairs of the same, the same, we extrapolate can)
Student-Class mapping file access address: Student.hbm.xml
Mapping file access address for teacher class: Teacher.hbm.xml
4.OK, now the preparation is complete, let's write a test class to execute it.
Test class Access Address: Teststu.java
This test method is to create a table inside the database
Then test the data to see if the relationship between the two tables added in the database is correct
Test run successfully, then please look at the database table, is not already data added, and the corresponding relational table is clear.
Information about the Student table:
Information about the Classroom table:
Information about the relational table:
Since I tested two times before, so the data is two times the data, the last 4 records are the test, that is, the initialization of the teachers and students are 9,10