mvb*_*b13 6 java spring hibernate spring-transactions
在我的例子中,我有一个Hibernate实体和一个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);
}
}
Run Code Online (Sandbox Code Playgroud)
当我以这种方式在服务中使用DAO时
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
数据库中只保存一行(Action2).当我删除注释时,两行(Action1和Action2)都被保存(这是我需要的行为).我的问题是服务层上的Transactional注释如何影响事务(方法executeTransaction())执行.为什么没有服务层上的Transactional注释,两行都保存在数据库中,只有最后一行保存在这个注释中?
没有 myEntity = new MyEntity();
您在数据库中的记录更新,未插入,因为它是相同的实体.我想<property name="show_sql">true</property>
在hibernate conf中设置.这将告诉你发生了什么.
归档时间: |
|
查看次数: |
3825 次 |
最近记录: |