Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate with user defined IdentifierGenerator UUID
    primarykey
    data
    text
    <p>I am working with a environment using Hibernate 3.6. I am trying to implement a MySQL table that uses UUIDs from java.util as the PK, these UUIDs are generated from multiple Strings defined by the EntityImage. Here is the code I am using to generate those keys.</p> <pre><code>public class EntityIDGenerator implements IdentifierGenerator { ... private UUID generateUUID(String str) { return UUID.nameUUIDFromBytes(str.getBytes()); } public UUID generateUUID(EntityImage image) { return generateUUID(image.getEntityId()+image.getEntityType().toUpperCase()+image.getFilename().toUpperCase()); } ... public Serializable generate(SessionImplementor session, Object object) throws HibernateException { if(object instanceof EntityImage) { return generateUUID((EntityImage)object); } return null; } } </code></pre> <p>and here his my hibernate</p> <pre><code>&lt;class name="EntityImage" table="entity_image"&gt; &lt;id name="id" column="id" type="entityUUID" unsaved-value="null"&gt; &lt;generator class="com.covelo.energy.score.db.EntityIDGenerator" /&gt; &lt;/id&gt; &lt;property name="entityId" type="int"/&gt; &lt;property name="entityType" type="string" length="45" /&gt; &lt;property name="filename" type="string" length="90" /&gt; &lt;property name="fileType" type="string" length="5"/&gt; &lt;property name="imageData" type="binary" column="image_data"/&gt; &lt;/class&gt; </code></pre> <p>With my current setup I can only get it to work one way, generate the UUID myself and check if a key exists in the Hibernate then call a save or update accordingly (or just set the UUID to null or the value respectively)</p> <p>This way seems wrong especially for having this PK generator implemented. Ideally Hibernate would generate a PK using my IdentifierGenerator then check if it exists and update or save it. Am I going about my architecture wrong or is there a method to do this in hibernate?</p> <p><strong>Note:</strong> I am well aware of how saveOrUpdate works <a href="http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/objectstate.html#objectstate-saveorupdate" rel="nofollow">Hibernate documentation</a></p> <p>-Thanks in advance. Ned</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. This table or related slice is empty.
 

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