Hi,

Instead of creating a new Department with the ID of the department
that you want to delete, you should use the getReference() method as
such:

Department toRemove = em.getReference( Department.class, "1234" );
em.getTransaction().begin();
em.remove( toRemove );
em.getTransaction().commit();

Hope this helps!

Christian

On 10/26/07, Tom Mutdosch <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I have a scenario where I have a Department entity, and then wish to delete 
> it.
>  I thought that I should be able to create a new Department instance and set
> its primary key to that of the department I wish to delete, and then merge 
> that
> entity and remove it:
> __
> EntityManager em = getEntityManager();
> Department newdepartment = new Department();
> newdepartment.setDeptno("1234");
>
> em.getTransaction().begin();
> Department department = em.merge(newdepartment);
> em.remove(department);
> em.getTransaction().commit();
> __
>
> This code executes but does not remove the specific entity.  From reading the
> JPA spec, I thought that if there is no version field on the entity, then the
> merge() call will look for an entity that has the same ID (primary key) as the
> instance you pass in.  So I thought that the above code would work.
>
> If instead of creating a new instance I retrieve the entity via find():
> department = (Department) em.find(Department.class, "1234");
>
> Then it is removed properly as expected. Does anyone know if the above code
> should work, or if I have just misinterpreted the spec?
>
> Thanks
> Tom
>

Reply via email to