Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to generate entities from an existing database model in JHipster?

Is there a way to generate entities from an existing database model or do I have to create all entities with yeoman (yo) on my own?

I've heard about such a technique from the Spring Roo project.

like image 537
seveves Avatar asked Apr 24 '15 06:04

seveves


People also ask

How do you create a many to many relationship in JHipster?

JHipster UML and JDL Studio If you want to create many entities and relationships, you might prefer to use a graphical tool. In that case, three options are available: JDL Studio, our online tool to create entities and relationships using our domain-specific language.

What is JHipster generator?

JHipster, or “Java Hipster,” is a handy application generator that will create for you a Spring Boot (that's the Java part) and AngularJS (that's the hipster part) application.

What is JHipster used for?

JHipster is a free and open-source application generator used to quickly develop modern web applications and Microservices using Angular or React (JavaScript library) and the Spring Framework.


3 Answers

Please check this helper

https://www.npmjs.com/package/generator-jhipster-db-helper

as per the description of this helper "This JHipster module makes mapping on an existing database easier."

also you can use this tool :

https://github.com/Blackdread/sql-to-jdl

which will convert your sql files into idl files , then you can use jHipster import-idl functionality to import the database.

that will help you speed up the process of generating hipster application with an existing database

like image 143
Ayman Avatar answered Oct 22 '22 19:10

Ayman


no you cannot, because the jhipster yeoman generator "only" scaffolds the entities according to the templates + the given parameters/choices. It does not ask external sources like databases in this step.

The generator creates all files for jpa, angular and liquibase changelogs. Finally, liquibase creates the tables using the changelogs during startup, if they don't exist yet.

So, you can say that jhipster uses an "entity first" instead of a "table first" approach.

Although it would be a nice feature, I don't think that it will be integrated into jhipster, because existing databases are so different that it would be too difficult to handle each possibility. There are different choices of primary keys, different datatypes, different realizations of many-to-many relations or generalizations and so on. Or you can request a new feature on Github and maybe it will be implemented...

But, to give some directions: I also had a same situation where I tried to migrate an existing database with about 50 tables and with lots of data to jhipster (this was jhipster 1.6 or so) and I also thought of a "database refactoring" [1]. However, my "solution" was to create a new database using jhipster and then migrate the data from the old database to the new one by using some sql statements. The main reasons:

  • I had another database model that differs from the jhipster expected model (e.g. I used other primary keys and references)
  • No repositories nor angular stuff (which was my main reason to use jhipster) are generated
  • The liquibase changelogs are missing [2]
  • Such a special refactoring causes a lot of additional changes afterwards, when you try to generate a jhipster entity against an existing table. These changes could be more time-consuming than creating new entities with jhipster.
  • These changes could also cause problems in feature upgrades

and yes, roo has such a technique for reverse engineering or refactoring a database (http://docs.spring.io/spring-roo/reference/html/base-dbre.html). AFAIK, it only creates roo-conform entities that are based on JPA. So, it also differs to spring data JPA that is used by jhipster (same problem like other jpa-refectoring tools like [1])

[1] I used an eclipse JPA plugin that can create jpa entity classes from an existing database in another dropwizard-based project before. But, I don't tried it in combination with Spring/Jhipster.

[2] It is possible to create liquibase changelogs from an existing database: http://www.liquibase.org/documentation/generating_changelogs.html

like image 7
Indivon Avatar answered Oct 22 '22 18:10

Indivon


Spring Roo includes the DBRE addon, a great tool for database reverse engineering that generates your domain entities automatically.

@eplog you are wrong, the DBRE lets you to use the --repository option to create Spring Data JPA Repositories for each entity. Take a look at http://docs.spring.io/spring-roo/docs/1.3.1.RELEASE/reference/html/base-dbre.html#d4e1765

Imho, the benefits that the DBRE provides you are:

  • You don't need to search, test and learn 3rd party tools, Roo does it for you.
  • Reverse egineering is a powerful tool to migrate legacy applications in those environments in which you cannot migrate the database

Hope it helps. Enjoy with Roo!

like image 4
eruiz Avatar answered Oct 22 '22 20:10

eruiz