I have a Spring CrudRepository that I am using to persist an Entity. However, my Entity has some transient fields that I do not want stored in the database, however, I don't want to lose them when I call save() but it appears I always get a brand new, different object back from the save() method. And thus I lose my transient values.
If I don't use the object that is returned and just keep using my original object that I called save() on, I can only call save() on it once, otherwise it complains the Entity was updated by another session.
Any idea how to either get save() to return me my same object back? Should I be using a different repository? Can I add some sort of annotations to change the behavior?
@Override
@Transactional
public TestRun saveTestRun(TestRun run)
{
logger.debug("Saving TestRun with Id: " + run.getId() + " " + run.toString());
logger.debug("Timeout 1: " + run.getTimeout());
run.setTimeout(10000000); // set transient field
logger.debug("Timeout 2: " + run.getTimeout());
TestRun r2 = this.repository.save(run);
logger.debug("Timeout 3: " + r2.getTimeout());
logger.debug("Saved TestRun with Id: " + r2.getId() + " " + r2.toString() );
return r2;
}
And this produces:
Saving TestRun with Id: 2 TestRun@4c4d810e
Timeout 1: 90
Timeout 2: 10000000
Payload no-arg constructor
TestRun constructor
TestRun no-arg constructor
Timeout 3: 90
Saved TestRun with Id: 2 TestRun@25a14d5c