Why write this blog, because often have colleagues or netizens asked me Hiberante entity class generation problem. So next time someone asked me to save a lot of words, in fact, this is really very simple.
The application of hibernate in the project is now more extensive. I started to study it after hibernate started supporting annotation syntax. To tell the truth, the original hibernate generated a heap of XML files looked dizzy, as well as their own written SQL statement to the comfortable, so have been listening to friends and colleagues introduced, you can always disdain to use it.
But now it's blessed. Hibernate is one of the most common implementations of JPA, and hiberante itself supports annotation syntax. Now don't look at that heap of XML mappings anymore. And Eclipse also provides tools to generate the entity class directly from the database. Now that you can write less or not write SQL statements, that's why. Talk less and start doing it.
Or starting with the first step of the project, create a new project. Generally we use Java to do Web projects are file->new->dynamic Web project, such as:
The dialog box for the new project appears
Then click on the Modify button
The Java persistence is selected, which adds JPA features to the project, and other features are self-modifying according to the project needs. After clicking OK. Click Next several times (depending on your situation) to the JPA facet window, such as:
Because we're using Hibernate list not so platform choose Generic,type Choose Disable Library configuration, Because we put the required jar files directly into the Lib directory under Web-inf. So do not configure. The following connection this is a must be configured or how we generate from the database AH. Yes, No.
If you have previously configured a connection directly select it, if not configured click Add Connection.
Select the type of database you want to use, and then next, such as. In fact, this type does not work just a template to generate the connection string.
Choose the existing driver, if not what to do, nonsense, of course, continues to add. This piece is not detailed, click on the driver after the first button, according to the prompt to select the corresponding jar file.
Click Finish to finish the above operation. Go back to the JPA facet window. Select Discover annotated classes automatically, and the create orm.xml to be fished out. Then the finish project is completed.
Then, right-click on the project folder to select JPA tools->generate entities from Tables.
Choose a built-in connection if you don't have connect, connect. will be as shown (sometimes eclipse will have some small problems to see the table, you can restart the test, it is not possible to build a JPA project directly, create the entity and then copy to your own project.) Select the table where you want to generate the entity, Next:
The above is the relationship between the tables, if you can make the corresponding changes (using Ecipse 3.5 seems to eclipse3.4 not yet this function, Eclipse is getting stronger), and then next, such as:
(This seems to be the Eclipse 3.5 added) can see it. You can change it as needed, but not much. And next.
After, such as:
This is the adjustment of each property of some tables, and generally does not have to be tuned. The finish is good.
Hehe was finally finished. With so many pictures, I was exhausted. But for everyone to understand, or figure more than words can explain the situation, I worked hard to point it out.
Look at the Java files generated in the source code is not what you want, how to use the specific classes. It's not the question that this article is going to say. If you need me, I'll write next.
Hibernate entity classes are generated from the database in eclipse