Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

eclipse generating entity classes from database is not pulling associations

I am working on rewriting an application using hibernate with existing database.This application has all select queries it is basically read only application.

I am trying to create entity classes from tables using JPA tools in eclipse, as there are no constraints defined on database , the generated model classes have no relationships.There are around 100 tables using by the application in that database.

I tried to figure out the relationships between tables from the existing sql queries and also using data in database.But as there are lot of tables it is hard to do it manually.

As the database used by number of applications there is no way of enforcing relationships on database

Question:

1.Is there any way of to generate entities with relationships if there are no constraints on database?

2.If it needs to be done manually, what is the best approach?

like image 258
Chris Avatar asked Mar 04 '17 20:03

Chris


People also ask

How can we generate JPA entities from database tables with Eclipse and spring boot?

Right-click the JPA project in the Project Explorer and select JPA Tools > Generate Entities from Tables. On the Select Tables page of the Generate Entities from Tables wizard, select your database connection and schema. To create a new database connection, click Add connection.


2 Answers

1.Is there any way of to generate entities with relationships if there are no constraints on database?

Even if it was possible, it would be very error prone. On which rule, the relationships could be generated in a idiomatic and reliable way ?

2.If it needs to be done manually, what is the best approach?

If I was to your place, I would do the things in a safety way.
Adding manually all relationships between entities after entities be generated may be error-prone and cumbersome.

Suppose that you make mistakes during the generation phase of the entities (and you could seeing the number of tables) and you want to generate them again while you already added dozen and dozen of relationships manually in your entities.
By starting again the generation, you will lose all these manually added relationships in the entities.
You have to start again from zero.

I think that you should do the things in the reverse way.

You could for example create a copy of the database (I refer to a copy as I suppose that if you have no constraints on the tables, it is intentional) and add constraints on the tables of the database copy.
Then, from these tables with specified constraints, you could generate entities with all required relationships.

This way provides two advantages :

  • ability to have a fast feedback whether the PK/FK constraints that you want to add one the tables are compatible with your existing data.

  • ability to proceed in an incremental way and be able to do some steps back if required.
    For example if you make some mistakes during the generation phase of the entities (and you could make them seeing the number of tables), you could repeat the generation phase without losing the automatic generated relationship resulting of PK/FK constraints you added on the tables.

like image 161
davidxxx Avatar answered Sep 23 '22 11:09

davidxxx


There is no direct way to create all entity classes with required relationship .

But if you wanna add relationships in generated entities the most simplest way which i guess is provide relationship in Table association.

You can follow the link for more reference.

http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jpt.doc.user%2Ftasks024.htm

like image 25
Amit Gujarathi Avatar answered Sep 25 '22 11:09

Amit Gujarathi