java - Hibernate with user defined IdentifierGenerator UUID -
i working environment using hibernate 3.6. trying implement mysql table uses uuids java.util pk, these uuids generated multiple strings defined entityimage. here code using generate keys.
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; } } and here hibernate
<class name="entityimage" table="entity_image"> <id name="id" column="id" type="entityuuid" unsaved-value="null"> <generator class="com.covelo.energy.score.db.entityidgenerator" /> </id> <property name="entityid" type="int"/> <property name="entitytype" type="string" length="45" /> <property name="filename" type="string" length="90" /> <property name="filetype" type="string" length="5"/> <property name="imagedata" type="binary" column="image_data"/> </class> with current setup can work 1 way, generate uuid myself , check if key exists in hibernate call save or update accordingly (or set uuid null or value respectively)
this way seems wrong having pk generator implemented. ideally hibernate generate pk using identifiergenerator check if exists , update or save it. going architecture wrong or there method in hibernate?
note: aware of how saveorupdate works hibernate documentation
-thanks in advance. ned
Comments
Post a Comment