My web application uses Spring (4.2.9), Spring Data (3.2.5), JPA, Hibernate (4.3.8), and MS SQL Server (2014). I am hoping to show the big picture and get the direction to fix this issue.
Here is the class affected:
Class Affected {
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long id;
@Version
private Long version = 1L;
//getters and setters of the above two fields.
}
Here is another class and saving its object causes the version of the Affected class increases. Note that there is not any kind of relationship between the two classes.
Class Reason {
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long id;
}
The service method to fetch an object of Affected objects:
@Override
@Transactional(readOnly = true)
public Survey getAffectedById(Long id) {
return affectedRepository.findOne(id);
}
This is the flow in a web controller method:
- load an Affected object
- Create and save an object of Reason in a transaction. In this step, the IP address is extracted from javax.servlet.http.HttpServletRequest and saved in the database to record what IP addresses visited the web application.
After the two steps, the version number of the loaded Affected object increases by 1. If I remove the second step, the version number of the Affected has no change.
I am not able to figure out the reason for this increase. I understand that more code or details are helpful, but I don't want to overwhelm folks here and also the actual code is much more complex than that.