How to generate automatically relationships with JPA with IntelliJ? - intellij-idea

I found this solution for a problem I have: how to generate entities with JPA annotations from a given database.
IntelliJ IDEA 10 generate entity (POJO) from DB model
Now with IntelliJ I'm given the possibility to create relationships between entities manually. Is there a way to generate them automatically as it did with entities?
I used the REFERENCES keyword when needed while creating the database. I suppose there should be an automatic mapping of relationships as well!

When Generating entities from DB Schema in dialog there is an option to 'Show default relationships' which when selected will display FK relationships when selecting tables to generate entities from:

Related

It's possible to detect a many-to-many relationship on a rlinq file?

I am working on a project manually mapping entities from a SQL db. My primary source of information about columns, keys, relationships, and others is the rlinq generate by Telerik from the db.
I wonder if it's possible to detect a many-to-many relationship based on only in this file.

How to generate relations between tables

How can I use Gii to create relations between the tables? Should I create them first in my database or they will be generated automatically?
Gii doesn't create a schema for you (like EclipseLink or Hibernate would in Java), it just helps you to create ActiveRecord classes based on the definition of your schema in database and the relationships that it can find between your tables and with the help of some conventions that the framework have. If you want to manage your database changes programmatically you should try Yii migrations.
Short answer, you must first create the schema and then use gii to generate active record classes.
Hope this helps.

CUBA : attributes for many-to-many association

Is there a standard way in CUBA to modelize attributes for many-to-many association ? Documentation omits the topic so I guess it is not. In this case, is this in the roadmap ?
For many-to-many association (e.g between Products and Providers), CUBA Studio generates automatically a link table (holding Provider ID and Product ID).
In order to handle specific attributes to this association (e.g Boolean preferredProvider) it would need to add the preferredProvider column in the link table and create a class holding the two IDs and the attribute.
It would also probably impact the platfom mechanism of fetching many-to-many associations.
I'm reasonably sure that CUBA Studio does not manage it as of 2.2.3 - no option in Studio GUI, nothing in doc. It is still of course possible to code the case manually but one would need to manually write JPQL. Not necessarily a big deal but losing a strong feature of the platform here just for one field.
So I created the preferredProvider field as a one-to-one association from Product to Provider, which is a valid workaround at the cost of an additional association.

Gii: generate CRUD application from existing database tables?

I have a mysql database with tables that have one to many relationships with foreign keys linking them.
How can I use gii to create CRUD form pages which will reflect the table relationships?
I suggest you to take a look at the following article which includes using yii's gii
Creating Your First Yii Application

Foreign Keys cascading in JPA or database Schema

I have a separate script that creates the database and tables for each database that we are supporting. I am using JPA to manipulate the data in the database, but JPA does not create the database or the tables.
I want to add a foreign key to a new table with a cascade property so that when a row is deleted in the parent table, the corresponding rows in the child table are also deleted.
I am aware of the annotations necessary to do this in JPA, however I can create the foreign keys and the cascade statements in the script I am using to create the databases.
My question is, since I am using a separate script to create the database tables, can I just add the foreign key / cascade statements in the script and then ignore all of the JPA relationship annotations? Is there advantages/disadvantages to adding this information in both the database script as well as in the JPA code?
You should always have a 2 level check. if you do not use the features of JPA, then it's a big waste of the functionality JPA provides. you should actually make sure that you JPA relations match your DB relations as closely as possible. It will help you a lot as JPA can cache data and even prevent unnecessary calls to DB.
eg if u have a not null constraint and you persist with no JPA constraint, your DB has to do all the work and throw the exception back.
normally in an application, the network and DB are the slowest factors in the app. so you should try mimicking the constraints in JPA to avoid unnecessary overhead.
also using such constraints you can form bidirectional relationships and have collections of associated entities and many more such advantages.