0

I am using EclipseLink and I was wondering if there is a way to avoid JPA operations on a collection of an Entity when the Entity is updated or created. I have an Entity with the following collection:

@OneToMany(mappedBy = "pageItemAssignment")
private Collection<AllowedUrl> allowedUrls;

Anything that gets added or removed from the collection will have a jpa persist / remove operation when I update the parent entity. I don't want any of that to happen. I only want the collection populated when the parent entity is retrieved and all other operations ignored. I will have code that will persist / update / remove the entities in the collection.

Is there a configuration that I can configure on the relationship to ignore operations?

Thanks.

3 Answers 3

0

What I understand you don't want to persist certain field state to DB. you can use annotation @Transient.

Transient entity fields are fields that do not participate in persistence and their values are never stored in the database.

@Entity
public class MyEntity  {
     ....
    @Transient 
     String url; // not persistent because of @Transient
} 
1
  • This is a collection that i need to pull from the database so @Transient wouldn't work here. Oct 14, 2015 at 15:09
0

I think I have been going about this the wrong way. The automatic persist on my new collection items was leaving the collection item without an id value. I wanted to control the save / updating by avoiding that process.

Instead i have opted to do a refresh of the parent entity so that the collection gets refreshed so each one of the new ones have an id after persisting. Now the only code i need to write is an update as that is the only thing not automatically triggered.

0

Assuming there are already entities in the database you can avoid flushing in-memory changes by defining a given field as read-only. In your particular case this may look as follows:

@OneToMany(mappedBy="pageItemAssignment")      //non-owning side of the relationship
private Collection<AllowedUrl> allowedUrls;

@ManyToOne                                     //owning side of the relationship
@JoinColumn(insertable=false, updatable=false) //read-only field
private PageItemAssigment pageItemAssignment;

This prevents JPA provider from doing anything except reading the relationship from the database (FK will not be inserted, but other fields will). Such an approach works with the following annotations: @Column, @OrderColumn, @JoinColumn, @MapKeyColumn and @MapKeyJoinColumn.

1
  • I tried that before I posted my question. The parent entity update will still try to persist an object in the collection. Setting the insertable to false will ignore the value for the foreign key and try to create a row in the table with all the other fields. That ofcourse fails because of foreign key validation. Oct 14, 2015 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.