Persistence Query language
hi guys
I am using persistence query language, I'm trying to delete an entry from the database table. So far I tried 2 approaches
public void destroy(DeviceType deviceType)
{
em.merge(deviceType);
em.remove(deviceType);
}
public void delete(int pk)
{
em.createQuery("Delete from DeviceType dt where dt.deviceTypeID = '"+pk+"'");
}
both of these methods are not working for me.
please help
[471 byte] By [
shimi1a] at [2007-11-26 20:26:02]

# 2
I'm thinking you don't need that merge () call if what you're going to do is remove it anyway. Whatever changes merge () makes are going to disappear anyway.
Make sure you commit the transaction in which you do this. Try something likepublic void destroy (DeviceType deviceType) {
em.getTransaction ().begin ();
em.remove (deviceType);
em.getTransaction ().commit ();
}
If you already have an open transaction floating around, you may not want to do this.
# 3
Hi shim1,
1. If you're executing destroy in an extended persistence context, em.merge(deviceType) is not needed. In a transaction scoped persistence context, you must change destroy to:
public void destroy(DeviceType deviceType)
{
DeviceType mergedType = em.merge(deviceType);
em.remove(mergedType);
}
Please see chapter 3.2.4.1 of the JPA specification for more information about the merge operation.
2. Please change the delete method to:
public void delete(int pk)
{
em.createQuery("Delete from DeviceType dt where dt.deviceTypeID = ?1").setParameter(1, new Integer(pk)).executeUpdate();
}