<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Hibernate on technocracy</title><link>https://www.ericsimmerman.com/tags/hibernate/</link><description>Recent content in Hibernate on technocracy</description><generator>Hugo</generator><language>en</language><lastBuildDate>Tue, 23 Feb 2010 00:00:00 +0000</lastBuildDate><atom:link href="https://www.ericsimmerman.com/tags/hibernate/index.xml" rel="self" type="application/rss+xml"/><item><title>Select max value of numeric CollectionOfElements with Hibernate</title><link>https://www.ericsimmerman.com/blog/2010/02/23/select-max-value-of-numeric-collectionofelements-with-hibernate/</link><pubDate>Tue, 23 Feb 2010 00:00:00 +0000</pubDate><guid>https://www.ericsimmerman.com/blog/2010/02/23/select-max-value-of-numeric-collectionofelements-with-hibernate/</guid><description>&lt;div class='post'&gt;
Consider a numeric CollectionOfElements held by a class Product such as:&lt;div class="CodeRay"&gt; &lt;div class="code"&gt;&lt;pre&gt;@Entity(name=&amp;quot;Product&amp;quot;)public class Product {protected Set serialNumbers = new HashSet();@CollectionOfElements@JoinTable(name=&amp;quot;product_serialnumbers&amp;quot;)public Set getSerialNumbers() { return serialNumbers;}public void setSerialNumbers(Set serialNumbers) { this.serialNumbers = serialNumbers;}}&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;You can use a NamedQuery to determine the maximum SerialNumber held by any Product like this:&lt;div class="CodeRay"&gt; &lt;div class="code"&gt;&lt;pre&gt;@Entity(name=&amp;quot;Product&amp;quot;)@NamedQueries ({ @NamedQuery( name = &amp;quot;maxSerialNumber&amp;quot;, query = &amp;quot;select max(elements(p.serialNumbers)) as value from Product p&amp;quot; )})public class Product {protected Set serialNumbers = new HashSet();@CollectionOfElements@JoinTable(name=&amp;quot;product_serialnumbers&amp;quot;)public Set getSerialNumbers() { return serialNumbers;}public void setSerialNumbers(Set serialNumbers) { this.serialNumbers = serialNumbers;}}&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description></item><item><title>Hibernate Annotations - IdentifierGenerationException: attempted to assign id from null one-to-one property</title><link>https://www.ericsimmerman.com/blog/2009/02/18/hibernate-annotations-identifiergenerationexception-attempted-to-assign-id-from-null-one-to-one-property/</link><pubDate>Wed, 18 Feb 2009 00:00:00 +0000</pubDate><guid>https://www.ericsimmerman.com/blog/2009/02/18/hibernate-annotations-identifiergenerationexception-attempted-to-assign-id-from-null-one-to-one-property/</guid><description>&lt;div class='post'&gt;
While using Hibernate annotations to implement a @OneToOne relationship where the entities are joined by primary key (@PrimaryKeyJoinColumn) I kept encountering a relatively infamous Exception:&lt;div class="CodeRay"&gt; &lt;div class="code"&gt;&lt;pre&gt;IdentifierGenerationException: attempted to assign id from null one-to-one property&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;I discovered that my issue could easily be resolved by adding "optional=false" to the dependent side of the relation. Here's a brief summary of the relevant related code.&lt;div class="CodeRay"&gt; &lt;div class="code"&gt;&lt;pre&gt;@Entitypublic class Profile implements Serializable { @OneToOne(mappedBy=&amp;quot;profile&amp;quot;, optional=false, fetch=FetchType.LAZY) public Hospital getHospital() { return hospital; } @Id @GeneratedValue(generator=&amp;quot;foreign&amp;quot;) @GenericGenerator(name=&amp;quot;foreign&amp;quot;, strategy = &amp;quot;foreign&amp;quot;, parameters={ @Parameter(name=&amp;quot;property&amp;quot;, value=&amp;quot;hospital&amp;quot;) }) public Long getId() { return this.id; }}@Entitypublic class Hospital implements Serializable { @OneToOne(cascade = CascadeType.ALL,fetch=FetchType.LAZY) @PrimaryKeyJoinColumn public Profile getProfile() { return profile; } @Id @GeneratedValue(strategy=GenerationType.AUTO) public Long getId() { return id; }}&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description></item></channel></rss>