Question

This is a part of my EclipseLink JPA Entity:

public class Task implements Serializable { 
@JoinColumn(name = "IdTransaction", referencedColumnName = "serial_no")
@OneToOne(cascade = CascadeType.ALL)
private Transaction idTransaction;

}

When i persist or merge the Task entity, the Transaction entity doesn't update the changes to it.

Somebody told me to use this : (cascade = CascadeType.ALL) but it didn't work.

So, how can i save changes to both entites with one persist/merge call ?

I update the database this way:

if (getEntityManager().isOpen()) {
            getTransaction().begin();
            entity = em.merge(entity);
            getTransaction().commit();
        }

"entity" is the Task Entity i was talking about, it updates properly but not the relations.

Était-ce utile?

La solution

I Found the solution, these are the available Cascades:

CascadeType.MERGE
CascadeType.PERSIST
CascadeType.DETACH
CascadeType.REMOVE
CascadeType.REFRESH
CascadeType.ALL

If you have an entity "USA" with a list of "States" Entites and you made changes to some states you just have to add:

@OneToMany(cascade = CascadeType.MERGE)
private State stateID;

And then just merge the USA Entity and not every changed state. With the CascadeType.MERGE, it should automatically update everything for you.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top