Select max value of numeric CollectionOfElements with Hibernate

Consider a numeric CollectionOfElements held by a class Product such as:
@Entity(name="Product")
public class Product {
protected Set serialNumbers = new HashSet();

@CollectionOfElements
@JoinTable(name="product_serialnumbers")
public Set getSerialNumbers() {
  return serialNumbers;
}
public void setSerialNumbers(Set serialNumbers) {
  this.serialNumbers = serialNumbers;
}

}
You can use a NamedQuery to determine the maximum SerialNumber held by any Product like this:
@Entity(name="Product")
@NamedQueries ({
    @NamedQuery(
        name = "maxSerialNumber",
        query = "select max(elements(p.serialNumbers)) as value from Product p"
        )
})
public class Product {
protected Set serialNumbers = new HashSet();

@CollectionOfElements
@JoinTable(name="product_serialnumbers")
public Set getSerialNumbers() {
  return serialNumbers;
}
public void setSerialNumbers(Set serialNumbers) {
  this.serialNumbers = serialNumbers;
}

}
Posted by Eric Simmerman
 

Hibernate Annotations - IdentifierGenerationException: attempted to assign id from null one-to-one property

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:
IdentifierGenerationException: attempted to assign id from null one-to-one property
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.
@Entity
public class Profile implements Serializable {
   @OneToOne(mappedBy="profile", optional=false, fetch=FetchType.LAZY)
    public Hospital getHospital() {
        return hospital;
    }
    @Id @GeneratedValue(generator="foreign")
    @GenericGenerator(name="foreign", strategy = "foreign", parameters={
            @Parameter(name="property", value="hospital")
    })
    public Long getId() {
        return this.id;
    }
}
@Entity
public 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;
    }
}
Posted by Eric Simmerman