How to set object property in transaction?

I’m trying to set a value in my object inside the transaction, because it would be nice to have it in 1 place instead of making it a method parameter in multiple methods.
I tried using filter, but I think it’s not exactly what I’m looking for.

Here’s an example of what I’m trying to do:

String myField;

//begin transaction here
EntityManager.unwrap(Session.class).//set myField = "some value"

Could anyone, please, point me how to do this or where should I read about how it should be done?

It’s very simple:

  1. You fetch the entity
  2. You modify the property.

That’s it. The Hibernate dirty checking mechanism will take care of the rest.

Thank you for the answer vlad.

I missed noting in my original message, that I’d like to do this when adding this object to DB for the first time. So it’s only on object creation. Also I’m using spring data, not sure if it makes any difference

Just set all the properties you want to be persisted and call persist. After Hibernate flushed the EntityManager, the database table row will have everything you set on the entity.

I’ll try to do that. Thank you