Hibernate中update方法的注意点

Hibernate 中如果直接使用

Session.update(Object o);

会把这个表中的所有字段更新一遍。


public class TeacherTest {
@Test
public void update(){
Session session = HibernateUitl.getSessionFactory().getCurrentSession();
session.beginTransaction();
Teacher t = (Teacher) session.get(Teacher.class, 3);
t.setName(“yangtb2″);
session.update(t);

session.getTransaction().commit();
}
}

Hibernate 执行的SQL语句:


Hibernate:
update
Teacher
set
age=?,
birthday=?,
name=?,
title=?
where
id=?


我们只更改了Name属性,而Hibernate 的sql语句 把所有字段都更改了一次。

这样要是我们有字段是文本类型,这个类型存储的内容是几千,几万字,这样效率会很低。

那么怎么只更改我们更新的字段呢?

有三中方法:

1.XML中设置property 标签 update = “false” ,如下:我们设置 age 这个属性在更改中不做更改


<property name=”age” update=”false”></property>
<property name=”age” update=”false”></property>

在Annotation中 在属性GET方法上加上@Column(updatable=false)


@Column(updatable=false)
public int getAge() {
return age;
}
@Column(updatable=false)
public int getAge() {
return age;
}

我们在执行 Update方法会发现,age 属性 不会被更改


Hibernate:
update
Teacher
set
birthday=?,
name=?,
title=?
where
id=?
Hibernate:
update
Teacher
set
birthday=?,
name=?,
title=?
where
id=?

缺点:不灵活····

2.第2种方法··使用XML中的 dynamic-update=”true”

view plaincopy to clipboardprint?
<class name=”com.sccin.entity.Student” table=”student” dynamic-update=”true”>
<class name=”com.sccin.entity.Student” table=”student” dynamic-update=”true”>

OK,这样就不需要在字段上设置了。

但这样的方法在Annotation中没有

3.第三种方式:使用HQL语句(灵活,方便)

使用HQL语句修改数据

view plaincopy to clipboardprint?
public void update(){
Session session = HibernateUitl.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query query = session.createQuery(“update Teacher t set t.name = ‘yangtianb’ where id = 3″);
query.executeUpdate();
session.getTransaction().commit();
}
public void update(){
Session session = HibernateUitl.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query query = session.createQuery(“update Teacher t set t.name = ‘yangtianb’ where id = 3″);
query.executeUpdate();
session.getTransaction().commit();
}

Hibernate 执行的SQL语句:


Hibernate:
update
Teacher
set
name=’yangtianb’
where
id=3
Hibernate:
update
Teacher
set
name=’yangtianb’
where
id=3


这样就只更新了我们更新的字段······
转载请标明出处:http://blog.csdn.net/Baykermr/archive/2010/03/09/5359913.aspx


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:oscache="http://www.springmodules.org/schema/oscache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springmodules.org/schema/oscache http://www.springmodules.org/schema/cache/springmodules-oscache.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!-- 对web包的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 mvc:annotation-driven --> <mvc:annotation-driven/> <!-- 扫描包 --> <context:annotation-config/> <context:component-scan base-package="com.org.*" /> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 配置jdbc --> <bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="locations"> <value>classpath:properties/jdbc.properties</value> </property> </bean> <!-- 配置數據源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="1"/> <property name="maxActive" value="500"/> <property name="maxIdle" value="2"/> <property name="minIdle" value="1"/> </bean> <!-- 配置sessionFactory 注解配置 org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean 配置形式: org.springframework.orm.hibernate3.LocalSessionFactoryBean --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>com.org.entity</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <!-- 配置hibernateTemplate --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- Spring AOP config配置切 --> <aop:config> <aop:pointcut expression="execution(public * com.org.service.*.*(..))" id="bussinessService" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService" /> </aop:config> <!-- 配置那个类那个方法用到事务处理 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 这个映射配置主要是用来进行静态资源的访问 --> <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/> <mvc:resources mapping="/resource/**" location="/resource/" /> <mvc:resources mapping="/jsp/**" location="/jsp/" /> </beans>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值