Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>I could create an ApprovalItem table for each entity type (e.g. A_ApprovalItem, B_ApprovalItem, etc.). This seems like a good schema on the database side</p> </blockquote> <p><strong>But</strong></p> <blockquote> <p>It seems i need to create a separate domain classes in Java for each entity approval (e.g. AAprrovalItem class, BApprovalItem class, etc.).</p> </blockquote> <p>You do not need it. you can create a single ApprovalItem class and create a @OneToMany relationship between your parent classes and your ApprovalItem. Hibernate <strong>takes care to create</strong> a linked table for each relationship. </p> <pre><code>@Entity public class ClassA { @Id @GeneratedValue private Integer id; // Hibernate will create CLASSA_APPROVALITEM to link both class @OneToMany private List&lt;ApprovalItem&gt; approvalItemList; } @Entity public class ClassB { @Id @GeneratedValue private Integer id; // Hibernate will create CLASSB_APPROVALITEM to link both class @OneToMany private List&lt;ApprovalItem&gt; approvalItemList; } </code></pre> <p>And your ApprovalItem class</p> <pre><code>@Entity public class ApprovalItem { @Id @GeneratedValue private Integer id; // Nothing else } </code></pre> <p>But Let's see what Java Persistence with Hibernate book talks about it</p> <blockquote> <p>You may have <strong>shared references</strong> to the Bid objects. As suggested earlier, a User may have a collection of references to the Bid instances they made. You can’t delete an item and all its bids without removing these references first. You may get an exception if you try to commit this transaction, <strong>because a foreign key constraint may be violated</strong>.</p> </blockquote> <p>So keep it in mind when dealing with shared references.</p> <p>In order to see how the target schema looks like, you can use the following</p> <pre><code>AnnotationConfiguration configuration = new AnnotationConfiguration(); configuration .addAnnotatedClass(ClassA.class) .addAnnotatedClass(ClassB.class) .addAnnotatedClass(ApprovalItem.class) .setProperty(Environment.USER, &lt;TYPE_YOUR_USER&gt;) .setProperty(Environment.PASS, &lt;TYPE_YOUR_PASSWORD&gt;) .setProperty(Environment.URL, &lt;TYPE_YOUR_URL&gt;) .setProperty(Environment.DIALECT, &lt;TYPE_YOUR_DIALECT&gt;) .setProperty(Environment.DRIVER, &lt;TYPE_YOUR_DRIVER&gt;); SchemaExport schema = new SchemaExport(configuration); schema.setOutputFile("schema.sql"); schema.create(&lt;DO_YOU_WANT_TO_PRINT_TO_THE_CONSOLE&gt;, &lt;DO_YOU_WANT_TO_EXPORT_THE_SCRIPT_TO_THE_DATABASE&gt;); </code></pre> <p>It will generate a file called schema.sql, which contains your target schema</p> <p>regards,</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload