Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The mapping you've posted and the description you provided conflict with each other:</p> <ol> <li>Mapping join column on inverse association as non-insertable/updatable is not going to work. How is that column going to be updated (and your parent-child association maintained)?</li> <li>Your child mapping defines Child's PK as childId (using @Id annotation). That's NOT how a composite key is mapped; Hibernate will consider childId to be a simple primary key.</li> <li>Your criteria doesn't match your mapping (ParentTable vs Parent, etc...) which is probably a conversion artifact, but other then that looks OK.</li> </ol> <p>I'd use aliases, though:</p> <pre><code>Criteria criteria = session.createCriteria(Parent.class) .add(Restrictions.eq("policyNo", "1234")) .createAlias("child", "children") .add(Restrictions.ne("children.transType", "VIEW")); </code></pre> <p><strong>Update</strong> (based on criteria / generated SQL):</p> <p>Take a look at the SQL generated by Hibernate based on your criteria (shortened for readability):</p> <pre><code>SELECT this_.*, ts1_.*, copolicypo4_.* FROM CoPolicyPointer this_ INNER JOIN CoTransactionSummary ts1_ ON this_.systemAssignId=ts1_.systemAssignId LEFT JOIN CoPolicyPointer copolicypo4_ ON ts1_.systemAssignId=copolicypo4_.systemAssignId </code></pre> <p>You'll notice that your "parent" table (<code>CoPolicyPointer</code>) is joined <strong>twice</strong> even though your criteria only joins the "children" table. That happens because you're specifying entity names in <code>@Entity</code> annotations but creating your criteria using <strong>original</strong> class name, making Hibernate think you're joining three separate entities:</p> <ol> <li>Criteria is based on <code>CoPolicyPointerRedo.class</code></li> <li>It joins to <code>CoTransactionSummaryRedo.class</code> whose entity name is <code>CoTransactionSummary</code> </li> <li>Which joins back to <code>CoPolicyPointerRedo.class</code> as ``CoPolicyPointer<code>using</code>@ManyToOne`</li> </ol> <p>You need to be consistent with naming. Either remove <code>name</code> from <code>@Entity</code> declarations (you can use <code>@Table</code> instead to specify table name) or create your criteria using <a href="http://docs.jboss.org/hibernate/stable/core/api/org/hibernate/Session.html#createCriteria(java.lang.String)" rel="nofollow noreferrer"><code>session.createCriteria(String entityName)</code></a> method which takes entity name rather than class name as parameter.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COI have removed the insertable=false and updatable=false from the annotation as suggested. In regards to your second point, you are correct that the @ID annotation was incorrect and my code actually has @EmbeddedId. I had to create a seperate example because I'm not supposed to share the companies code on the internet and didn't realize I used the wrong annotation. Sorry for the confusion. I changed the criteria code to what you have above and I still get back 3 of the Parent objects instead of just 1 with the child records attached. Any other ideas?
      singulars
    2. COI sympathize with "not sharing the code" but I don't think posting just the relevant fragments (and changing entity names like you did) would be objectionable - otherwise it's hard to suggest the solution without seeing the exact problem. @EmbeddedId you've mentioned should be (is?) applied to another (component) class. How is the cihld's part of that composite id assigned? Did you enable SQL output in Hibernate? If so, what query is generated? Does it look reasonable based on your data or do you see something obviously wrong?
      singulars
    3. COI have tried what you suggested, but I still get an extra left outer join tacked onto the end of the SQL. I was able to get a distinct result back by adding the following to the criteria statement: criteria .setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE); However, that is less than idea because all of the records are returned from the database and hibernate has to determine which record to use, so I would still like to find another option.
      singulars
 

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