Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think the problem is with the way you're doing your mapping, where you're trying to pull too many database concepts into your OO model. ORM was a little confusing to me as well, when I started doing it. What you need to understand is that the concept of a primary key field is a database concept and not an OO concept. In OO, each object reference is unique, and that's what you use to <em>identify</em> instances.</p> <p>Object references do not really map well to the database world, and that's why we have primary key properties. With that said, the use of primary key properties should be kept to a minimal. What I find helpful is to minimize the type of primary key <em>properties</em> that map directly to the primary key <em>columns</em> (usually, integer properties that map to a primary key column).</p> <p>Anyway, based on that, here's how I think you should do your mapping (changes highlighted with horizontal separators):</p> <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; </code></pre> <hr> <pre><code> // Removed field and searchRule mapping as those are already in the // primary key object, updated setters/getters to pull properties from // primary key object public Field getField() { return fieldRulePK != null ? fieldRulePK.getField() : null; } public void getField(Field field) { // ... parameter validation ... if (fieldRulePK == null) fieldRulePK = new FieldRulePK(); fieldRulePK.setField(field); } public SearchRule getSearchRule() { return fieldRulePK != null ? fieldRulePK.getSearchRule() : null; } public void setSearchRule(SearchRule searchRule) { // ... parameter validation ... if (fieldRulePK == null) fieldRulePK = new FieldRulePK(); fieldRulePK.setSearchRule(searchRule); } </code></pre> <hr> <p>From <code>FieldRulePK.java</code> (Child PK Class):</p> <pre><code>@Embeddable public class FieldRulePK implements Serializable { </code></pre> <hr> <pre><code> // Map relationships directly to objects instead of using integer primary keys @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> <hr> <p><code>SearchRule.java</code> should be fine as it is.</p> <p>I hope this all makes sense.</p> <p><em>Note that this is untested, it would take too much time for me to set up a test database and create all the necessary test code, but I hope it gives you an idea on how to proceed.</em></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