Search code examples
javaspring-bootjava-8hibernate-4.x

Auto Generating Classes for tables without any xml files


I have a DB with around 50+ tables. I plan to use spring-boot(1.2.4) with hibernate 4.3.10.Final and Java8 in my new project. Using Eclipse Luna SR2 as my IDE.

If i generate classes for tables using hibernate tool, they generate Classes and also *.hbm.xml. However, i do not want to use any kind of xml file in my project (one of the reasons i picked spring boot). If i want to use JPA annotations, do i need to write each and every class by myself? Is there any way i can generate classes with annotations without any xml files?

Is it possible?


Solution

  • In netbeans ide (version 8+ in my case) you right click on the package where you want to generate the entities. Select New -> Other and from Categories choose Persistence -> Entity Classes from Database. From database connection in the wizard you can create New database connection.. and follow the wizard to create the connection.

    After that you will see some Available tables that you can choose from to generate the java classes.

    I generate the classes often so every time I need to uncheck the Create Persistence unit because I already have one. I used to uncheck the Use Column Names in Relationships because I dont like the naming customerId of sssociated entity variable Customer. When I uncheck that option the variable looks like private Customer customer;

    Than click finish and you should have your entities with jpa annotations and named queries. Play around with the options as suits you. Than you can swich back to eclipse :)

    Here is the process tutorial