0

I want to remove an item from a list in an Entity. I have this Entity :

@Entity
public class PairingCommit extends Model
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long id;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "commit")
    public List<CommitItem> items;
}

I do the following for removing an Item :

commit.items.remove(item);
commit.update();

But it doesn't remove the object from the database. I suppose i missed something up...

EDIT : After some search, I'm not sure to use JPA... I'm working with Play framework 2 that use Ebean... But it seems that I have access to JPA annotation..

My first problem was that trying to directly delete the item like this :

CommitItem.byId(id).delete();

But it give a OptimisticLockException.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Emrys Myrooin
  • 2,179
  • 14
  • 39

4 Answers4

0

You should call EntityManager 's remove method on item.

EntityManager em;

item = em.merge(item); // Now item is attached
em.find(PairingCommit.class, [Pairing Commit PK]).items.remove(item);
em.remove(item);
Salih Erikci
  • 5,076
  • 12
  • 39
  • 69
0

Take a look at this question/answer. The CascadeType annotation will propagate EntityManager operations to the linked entities. The way your code is currently set up, calling

entityManager.remove(pairingCommit);

would also delete all of the CommitItems that the PairingCommit is linked to, but

commit.items.remove(item);

is not an EntityManager operation, so nothing gets propagated.

You can get rid of the linked items directly with the EntityManager.

Community
  • 1
  • 1
Alex Pruss
  • 533
  • 5
  • 15
  • My problem is that if i directly delete my item, I get an OptimisticLockException... – Emrys Myrooin Apr 10 '15 at 15:10
  • That should only happen if another process has modified your item sometime between you loading it and saving it. – Alex Pruss Apr 11 '15 at 16:51
  • There might be a problem in your transaction design or how you access the databank (DAOs, domain stores, etc.) – Alex Pruss Apr 11 '15 at 17:03
  • there is no reason for another thread to access to the database at this time. I only do one request for testing so it should only have on thread. And all the code is here... I use EBean/JPA to manage my database – Emrys Myrooin Apr 13 '15 at 07:33
0

The specification says:

It is particularly important to ensure that changes to the inverse side of a relationship result in appropriate updates on the owning side, so as to ensure the changes are not lost when they are synchronized to the database.

So, you must remove from the owning side of relation:

commitItem.setCommit(null);
Leonardo Cruz
  • 1,189
  • 9
  • 16
0

Ok so I have solved the problem of Optimistick Lock. It was that mysql failed to compare floating point number. I have passed to DECIMAL type and works fine now. But I d'ont understand why the removing of list don't works.

Here an article on how Optimistick lock works : http://www.avaje.org/occ.html

Emrys Myrooin
  • 2,179
  • 14
  • 39