hibernate中Session.update()方法详解

2025-10-17 23:41:44

1、Session的update方法:使一个游离对象转化为持久化对象,并且计划执行一条update语句。

若希望Session仅当修改了对象的属性时才执行update()语句,可以把映射文件中<class>元素的select-before-update设为true,该属性的默认值为false。

hibernate中Session.update()方法详解

2、若更新一个持久化对象,不需要显示的调用update方法,因为在调用Transaction的commit()方法是,会先执行session的flush方法。

         @Test

        public void testUpdate() {

                News news = (News) session.get(News.class, 1);

                news.setAuthor("oracle");

                

                session.update(news);

        }

hibernate中Session.update()方法详解

3、我们接下来测试一段代码,看会不会调用update方法。

        @Test

        public void testUpdate() {

                News news = (News) session.get(News.class, 1);

                

                transaction.commit();

                session.close();

                

                session = sessionFactory.openSession();

                transaction = session.beginTransaction();

                

                news.setAuthor("oracle185");

        }

hibernate中Session.update()方法详解

4、在上述代码代码中,由于对象已经处于游离状态,若要更新一个游离对象,需要显示的调用session的update方法。可以把一个游离对象转化成持久化对象。

@Test

        public void testUpdate() {

                News news = (News) session.get(News.class, 1);

                

                transaction.commit();

                session.close();

                

                session = sessionFactory.openSession();

                transaction = session.beginTransaction();

                

                news.setAuthor("oracle185");

                session.update(news);

        }

hibernate中Session.update()方法详解

5、需要注意的是:无论要更新的游离对象和数据表的记录是否一致,都会发送update语句。

       @Test

        public void testUpdate() {

                News news = (News) session.get(News.class, 1);

                

                transaction.commit();

                session.close();

                

                session = sessionFactory.openSession();

                transaction = session.beginTransaction();

                

                //news.setAuthor("oracle185");

                session.update(news);

        }

hibernate中Session.update()方法详解

6、如何能让update方法不再盲目的触发update语句?在.hbm.xml文件的class节点设置select-before-update=true;

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="com.gwolf.domain.News" table="NEWS" select-before-update="true">

        <id name="id" type="java.lang.Integer">

            <column name="ID" />

            <generator class="native" />

        </id>

        <property name="title" type="java.lang.String">

            <column name="TITLE" />

        </property>

        <property name="author" type="java.lang.String">

            <column name="AUTHOR" />

        </property>

        <property name="date" type="java.util.Date">

            <column name="DATE" />

        </property>

    </class>

</hibernate-mapping>

hibernate中Session.update()方法详解

hibernate中Session.update()方法详解

7、可以看到触发了两条select语句。若数据表中没有对象的记录,但是还是调用了update方法,会抛出异常。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢