Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling creation of ORM objects prior to persistence/generation of primary keys?
    text
    copied!<p>Bear with me as I try to simplify my issue as much as possible.</p> <p>I am creating a new ORM object. This object has an auto generated primary key which is created on the database using as an identity. Within this object, is a child object with a many to one relationship with the parent object. <strong>One of the attributes I need to set to create the child object is primary key of the parent object, <em>which has not been generated yet</em>.</strong> It is important to note that the primary key of the child object is a <strong>composite key that includes the primary key of the parent object</strong>.</p> <p><a href="http://xs941.xs.to/xs941/09291/fieldrule.1degree221.png" rel="nofollow noreferrer">Diagram http://xs941.xs.to/xs941/09291/fieldrule.1degree221.png</a></p> <p>In this diagram FieldRule is the <em>child table</em> and SearchRule is the <em>parent table</em>. The problem is that SearchRuleId has not been generated when I am creating FieldRule objects. So there is no way to link them.</p> <p>How do I solve this problem?</p> <hr> <p>Here is are some relevant snippets from the entity classes, which use annotation based mappings.</p> <p>From <em>SearchRule.java</em> (Parent Class):</p> <pre><code>public class SearchRule implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = true) @Column(name = "ID") private Integer id; @Basic(optional = false) @Column(name = "Name", unique = true) private String name; @Basic(optional = false) @Column(name = "Threshold") private int threshold; @Basic(optional = false) @Column(name = "LastTouched", insertable = false, updatable = false) @Temporal(TemporalType.TIMESTAMP) private Date lastTouched; @Column(name = "TouchedBy") private String touchedBy; @OneToMany(cascade = CascadeType.ALL, mappedBy = "searchRule", fetch = FetchType.LAZY) private Collection&lt;FieldRule&gt; fieldRuleCollection; @JoinColumn(name = "IndexTemplateId", referencedColumnName = "ID") @ManyToOne(optional = false, fetch = FetchType.LAZY) private IndexTemplate indexTemplateId; </code></pre> <p>From <em>FieldRule.java</em> (Child Class):</p> <pre><code>public class FieldRule implements Serializable { private static final long serialVersionUID = 1L; @EmbeddedId protected FieldRulePK fieldRulePK; @Basic(optional = false) @Column(name = "RuleValue") private String ruleValue; @JoinColumns({@JoinColumn(name = "IndexTemplateId", referencedColumnName = "IndexTemplateId", insertable = false, updatable = false), @JoinColumn(name = "FieldNumber", referencedColumnName = "FieldNumber", insertable = false, updatable = false)}) @ManyToOne(optional = false, fetch = FetchType.LAZY) private Field field; @JoinColumn(name = "SearchRuleId", referencedColumnName = "ID", insertable = false, updatable = false) @ManyToOne(optional = false, fetch = FetchType.LAZY) private SearchRule searchRule; </code></pre> <p>From <em>FieldRulePK.java</em> (Child PK Class):</p> <pre><code>@Embeddable public class FieldRulePK implements Serializable { @Basic(optional = false) @Column(name = "IndexTemplateId") private Integer indexTemplateId; @Basic(optional = false) @Column(name = "FieldNumber") private Integer fieldNumber; @Basic(optional = false) @Column(name = "SearchRuleId") private Integer searchRuleId; </code></pre>
 

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