java - Spring transaction in service and DAO layers -
in example have 1 hibernate entity , 1 dao.
@entity @table(name="myentity") public class myentity { @id @generatedvalue(strategy = generationtype.identity) @column(name="id") private long id; @column(name="action") private string actionname; } ................... @repository("mydao") @transactional(propagation = propagation.required) public class mydao { @autowired private sessionfactory sessionfactory; public void saveobject(myentity myentity){ sessionfactory.getcurrentsession().save(myentity); } }
when use dao in service in such manner
@service("myservice") @transactional(propagation = propagation.required) public class myservice { @autowired private mydao mydao; public void executetransaction(){ myentity myentity = new myentity(); myentity.setactionname("action1"); mydao.saveobject(myentity); // myentity = new myentity(); myentity.setactionname("action2"); mydao.saveobject(myentity); } }
only 1 row(action2) saved in database. when remove comment both rows(action1 , action2) saved(this behaviour need). question how transactional annotation on service layer influences on transaction(method executetransaction()) execution. why without transactional annotation on service layer both rows saved in database , last saved annotation?
without myentity = new myentity();
record in database updated, not inserted, because it's same entity. sugest set <property name="show_sql">true</property>
in hibernate conf. show happening.
Comments
Post a Comment