Is Hibernate the best approach for a single Java class and multiple tables?

Put another way: how do you model / map a heavily reusable child class / table with many different parent objects?

I have several types of entities, each of which is stored in its own table:

class A โ†’ table A class B โ†’ table B ....

Now I need to make each of these classes the parent of a one-way child collection 1: M. The collection is the story of the endorsements the company has acquired over time. The child domain class is called "ApprovalItem". The approval class is exactly the same for all types of parents.

What is the best way to match this? If I create a separate table to store all the statements, then I cannot force the FK to PK relationship of the object and / or I am left with a poor database design.

On the other hand, I could create a statement table for each entity type (e.g. A_ApprovalItems, B_ApprovalItems, etc.). This seems like a good database-side schema, but it seems to me that I need to create separate domain classes in Java for each entity statement (for example, the AAprrovalItem class, the BApprovalItem class, etc.). There seems to be a lot of difficulty and difficulty in creating so many new classes in Java that do nothing but allow me to put different JPA image annotations.

Is there a matching method in Hibernate that will allow me to have one class in a Java map for several different tables depending on who is the parent of the collection?

+4
source share
2 answers

I could create an ApproignItem table for each entity type (e.g. A_ApprovalItem, B_ApprovalItem, etc.). This seems like a good database side schema.

But

It seems I need to create separate domain classes in Java for each entity statement (e.g. AAprrovalItem class, BApprovalItem class, etc.).

You do not need it. you can create one class ApprovedItem and create the @OneToMany relationship between the parent classes and your statement. Hibernate takes care of creating a linked table for each relationship.

@Entity public class ClassA { @Id @GeneratedValue private Integer id; // Hibernate will create CLASSA_APPROVALITEM to link both class @OneToMany private List<ApprovalItem> approvalItemList; } @Entity public class ClassB { @Id @GeneratedValue private Integer id; // Hibernate will create CLASSB_APPROVALITEM to link both class @OneToMany private List<ApprovalItem> approvalItemList; } 

And your class is ApprovalItem

 @Entity public class ApprovalItem { @Id @GeneratedValue private Integer id; // Nothing else } 

But let's see what Java Persistence with Hibernate says.

You may have shared links for Bid objects. As suggested earlier, a user can have a set of links to the Bid instances they made. You cannot delete an item and all its bids without deleting these links in the first place. You may get an exception if you try to complete a transaction , because the foreign key constraint may be violated .

Therefore, keep this in mind when working with shared links.

To see what the target diagram looks like, you can use the following

 AnnotationConfiguration configuration = new AnnotationConfiguration(); configuration .addAnnotatedClass(ClassA.class) .addAnnotatedClass(ClassB.class) .addAnnotatedClass(ApprovalItem.class) .setProperty(Environment.USER, <TYPE_YOUR_USER>) .setProperty(Environment.PASS, <TYPE_YOUR_PASSWORD>) .setProperty(Environment.URL, <TYPE_YOUR_URL>) .setProperty(Environment.DIALECT, <TYPE_YOUR_DIALECT>) .setProperty(Environment.DRIVER, <TYPE_YOUR_DRIVER>); SchemaExport schema = new SchemaExport(configuration); schema.setOutputFile("schema.sql"); schema.create(<DO_YOU_WANT_TO_PRINT_TO_THE_CONSOLE>, <DO_YOU_WANT_TO_EXPORT_THE_SCRIPT_TO_THE_DATABASE>); 

It will generate a file called schema.sql that contains the target schema.

Respectfully,

+3
source

Chapter 8. Displaying inheritance of Hibernate documentation can help.

Otherwise, I do not see a problem with several classes derived from the ApprovalItem class that "do nothing", as you say, because they differentiate the statement, it looks like type approval. If you look at your model, I would recommend using several classes, even if they inherit only your base class ApproIDItem.

Did I understand your question well, or did I miss something even more subtle?

+2
source

Source: https://habr.com/ru/post/1303574/


All Articles