Well, I did some digging around and came up with an interim solution that seems
to work pretty well. I have multiple databases so I picked my common module to
manage autogenerated id's for compount primary keys. The first is the entity
object I generated in the database. The second is the IDGenerator class for my
composite-id entity objects. The third is the basic logic behind the
generator. Anyway, I just thought I'd post this since there isn't a lot of
information out there on GenericGenerators. If I have something bad in here,
let me know...
Thanks
Nic
------------------------------------- Entity on the table that manages the
id's -----------------------------
@Entity
@Table(name="sec_uuid")
public class SecUUID implements java.io.Serializable
{
private String entity;
private Long value;
public SecUUID()
{
}
public SecUUID(String entity, Long value)
{
this.entity = entity;
this.value = value;
}
@Id
@Column(name="entity", unique = true, nullable = false, insertable =
true, updatable = true, length = 45)
public String getEntity()
{
return entity;
}
public void setEntity(String entity)
{
this.entity = entity;
}
@Column(name="value", unique = false, nullable = true, insertable =
true, updatable = true)
public Long getValue()
{
return value;
}
public void setValue(Long value)
{
this.value = value;
}
}
--------------------------------------- Id generator class
-------------------------------------------
/**
*
* The following is an example annotation to be used on the class declaration
of the entity.
* Column name(In this case customerId) is the name of the property inside the
embedded id that needs to be auto-incremented.
* The column also needs to be of type Long.
* @GenericGenerator(name = "KeyGenerator", strategy = "EmbeddedIDGenerator",
* parameters = {
* @Parameter (name=EmbeddedIDGenerator.COLUMN_NAME,
value="customerId")
* }
* )
*
*
* The following is the tag that needs to put on the Id field of the entity.
This field must
* be an embedded id and the name must be id.
* @GeneratedValue(generator = "KeyGenerator")
*/
public class EmbeddedIDGenerator
implements IdentifierGenerator, Configurable
{
private static final Log log =
LogFactory.getLog(EmbeddedIDGenerator.class);
public static final String COLUMN_NAME = "uuid_column";
private String entityName;
private String columnName;
public Serializable generate(SessionImplementor sessionImplementor,
Object arg1)
throws HibernateException
{
log.debug("Generating id for entity " + entityName);
Object entity = arg1;
try
{
log.debug("Working on entity " +
entity.getClass().getName());
Serializable key =
(Serializable)PropertyUtils.getProperty(entity, "id");
log.debug("I have key for entity " +
key.getClass().getName());
log.debug("Looking up the next uuid for key " + key);
// This is where you will make a call or execute sql
to get your next id.
Long id =
ProxyFactory.getProxy().getNextUUID(entityName);
log.debug("Setting the value " + id + " on property " +
columnName);
PropertyUtils.setProperty(key, columnName, id);
return key;
}
catch (IllegalAccessException e)
{
log.error(e);
throw new TessRuntimeException("There was an error
generating the id for " + entityName);
}
catch (InvocationTargetException e)
{
log.error(e);
throw new TessRuntimeException("There was an error
generating the id for " + entityName + ". Key must have a setter for column "
+ columnName);
}
catch (NoSuchMethodException e)
{
log.error(e);
throw new TessRuntimeException("There was an error
generating the id for " + entityName + ". Entity must have a getId() method.");
}
}
public void configure(Type type, Properties properties, Dialect dialect)
throws MappingException
{
entityName = properties.getProperty(ENTITY_NAME);
columnName = properties.getProperty(COLUMN_NAME);
}
}
---------------------------------------------------------- UUID Generation
Logic --------------
public Long getNextUUID(String entityName)
{
String QUERY = "from SecUUID as uuid where uuid.entity =
:entityName";
SecUUID uuid;
try
{
log.debug("Looking up entity " + entityName);
uuid =
(SecUUID)pm.getEntityManager().createQuery(QUERY).setParameter("entityName",
entityName).getSingleResult();
uuid.setValue(uuid.getValue() + 1L);
log.debug("Incrementing the uuid by 1");
log.debug("updating value to " + uuid.getValue());
pm.getEntityManager().flush();
return uuid.getValue();
}
catch (NoResultException e)
{
log.debug("No uuid fond for entity " + entityName);
pm.insert(new SecUUID(entityName, 1L));
log.debug("Inserted entry for " + entityName);
return 1L;
}
}
-------------------------------- Example Entity
-------------------------------------
@Entity
@GenericGenerator(name = "KeyGenerator", strategy =
"com.bla.util.EmbeddedIDGenerator",
parameters = {
@Parameter (name=EmbeddedIDGenerator.COLUMN_NAME, value="customerId")
})
@Table(name = "customer", catalog = "tescsm", uniqueConstraints = {})
public class Customer
{
@EmbeddedId
@GeneratedValue(generator = "KeyGenerator")
@AttributeOverrides( {
@AttributeOverride(name = "customerId", column = @Column(name =
"customer_id", unique = false, nullable = false, insertable = true, updatable =
true)),
@AttributeOverride(name = "carrierId", column = @Column(name =
"carrier_id", unique = false, nullable = false, insertable = true, updatable =
true))})
public CustomerId getId()
{
return this.id;
}
}
-------------------------------------- Embeddable Id
------------------------------
@Embeddable
public class CarrierCustomerId
implements java.io.Serializable
{
// Fields
private Long customerId;
private Integer carrierId;
// Constructors
/** default constructor */
public CarrierCustomerId()
{
}
/** full constructor */
public CarrierCustomerId(Long customerId, Integer carrierId)
{
this.customerId = customerId;
this.carrierId = carrierId;
}
// Property accessors
@Column(name = "customer_id", unique = true, nullable = false, insertable =
true, updatable = true)
@GeneratedValue
public Long getCustomerId()
{
return this.customerId;
}
public void setCustomerId(Long customerId)
{
this.customerId = customerId;
}
@Column(name = "carrier_id", unique = false, nullable = false, insertable =
true, updatable = true)
public Integer getCarrierId()
{
return this.carrierId;
}
public void setCarrierId(Integer carrierId)
{
this.carrierId = carrierId;
}
}
View the original post :
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3928306#3928306
Reply to the post :
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3928306
-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
JBoss-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jboss-user