前面提到做项目使用了spring提供的OpenSessionInViewFilter。用了这个过滤器之后延迟加载是好使了,添加也好使。可是更新和删除却不好使了,也没报任何错误。。。。怪呀。我DAO层的代码是这么写的
public void deleteEmployee(Employee employee) {
this.getHibernateTemplate().delete(employee);
}
public void updateEmployee(Employee employee) {
this.getHibernateTemplate().update(employee);
}
这两个方法就没执行(控制台没有打印出删除或者更新的语句)。后来我尝试了别的方法,发现这么的好使
public void deleteEmployee(Employee employee) {
this.getHibernateTemplate().delete(employee);
this.getSession().delete(employee);
this.getSession().beginTransaction().commit();
}
原来是事务的问题。。。。可是,我配过滤器的时候设置了自动提交的啊
<filter> <filter-name>openSessionInviewFilter </filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class> <init-param> <param-name>flushMode</param-name> <param-value>AUTO</param-value> </init-param> </filter>
到现在我也没弄清楚,求解答啊。。。。。
再后来学习了下事务之后,发现这么写也行
@Transactional(isolation=Isolation.READ_COMMITTED,propagation=Propagation.REQUIRED,readOnly=false,timeout=60,rollbackFor=Exception.class)
public void deleteEmployee(Employee employee) {
this.getHibernateTemplate().delete(employee);
}
另外,我想给set排序,一查发现有两种方法
第一种方法,修改配置文件,指定order-by。note:datetime 是表的字段名而不是属性名
<set name="comments" inverse="true" order-by="datetime desc"> <key> <column name="articleId" /> </key> <one-to-many class="com.teamlab.entity.Comment" /> </set>
另一种方法,使用sort对set排序。
<set name="comments" inverse="true" sort="com.teamlab.entity.Comment " >
<key>
<column name="articleId" />
</key>
<one-to-many class="com.teamlab.entity.Comment" />
</set>
在Article实体类中将HashSet改成TreeSet
//private Set comments = new HashSet(0);
private Set comments = new TreeSet(0);
同时Comment类实现Comparator接口。我使用了第一种方法,没用第二种方法。不知道它们哪个效率高。求解。。
另有一事不明,HashSet的不是不保证顺序吗?可是对它排序为什么就好使呢?。。。。。。