I have a simple parent child object relationship.
The child is the owner of the relationship as the parent has the "mappedby" attribute set of the OneToMany Annotation.
The parent is pre existing in the database.
In a single transaction, I load the parent, create a new child, set the parent on the child and add the child to the parent's IndirectList.
The child is then saved.
Now this "works" however the parent has a Date field annotated with @LastModifiedDate.
Parent:
public class StoreCardInstance implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idStoreCardInstance")
@Getter @Setter
private Integer idStoreCardInstance;
@Basic(optional = false)
@Column(name = "updateDateTime")
@Temporal(TemporalType.TIMESTAMP)
@Getter @Setter
@CreatedDate
@LastModifiedDate
private Date updateDateTime;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "storeCardInstance")
@Getter @Setter
private Collection<UserStoreCardInstance> userStoreCardInstanceCollection;
public StoreCardInstance addUserStoreCardInstanceInstance(@NonNull UserStoreCardInstance userStoreCardInstance){
if(this.userStoreCardInstanceCollection==null){
this.userStoreCardInstanceCollection = new ArrayList<>();
}
this.userStoreCardInstanceCollection.add(userStoreCardInstance);
userStoreCardInstance.setStoreCardInstance(this);
return this;
}
Child
public class UserStoreCardInstance implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idUserStoreCardInstance")
@Getter @Setter
private Integer idUserStoreCardInstance;
@JoinColumn(name = "storeCardInstance", referencedColumnName = "idStoreCardInstance")
@ManyToOne()
@Getter @Setter
private StoreCardInstance storeCardInstance;
}
For some reason, the parent is updated with a new last modified date when saving the parent. It seems to think the parent is dirty but nothing has changed with the exception that the child has been added to the collection.
Where I am having trouble is if I run the code as a "main" class (with the exact same spring application context configuration) the code works as expected and only the new child is created.
If the code is run as part of a web application (either using Jetty or Tomcat 7/8) then the parent date time field is updated.
I am trying to prepare a simple unit test and some code examples, but this difference between running in and outside of a web application is making it very difficult to get a simple test case to illustrate the problem.
Using the latest Eclipse link (2.5.2) and spring (4.1.1)
Any help appreciated