7月27号学习日志

     前面提到做项目使用了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的不是不保证顺序吗?可是对它排序为什么就好使呢?。。。。。。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值