English 中文(简体)
会议。 除此以外,还更新藏匿国的数据。
原标题:Session.save is updating the data in hibernate
  • 时间:2012-05-07 04:36:19
  •  标签:
  • hibernate

Iam试图在藏书会上抽样收集学生信息。 就此,学生名称、阶级、教师。

表:统计

SNO SNAME             SCLASS         TNO
----------- ----------------------------------------
1 J D Alex            3                1
2 Goaty               2                2
3 J D Paul            7                1

法典:

Transaction tx1=session1.beginTransaction();
Object o2=session1.get(Student.class,new Integer(3));
((Student)o2).setSclass("8");
session1.save(o2);
log.info("loadStdYearlyInfo:class "+((Student)o2).getSclass());
tx1.commit();
session1.close();

After saving the data and seen the output the class value is updated as 8 for student id is 3

 SNO SNAME             SCLASS         TNO
    ----------- ----------------------------------------
    1 J D Alex            3                1
    2 Goaty               2                2
    3 J D Paul            8                1

[07/May/2012:10:03:06] info ( 3500): CORE3282: stdout: Hibernate: /* load com.aims.beans.Student */ select student0_.sno as sno0_, student0_.sname as sname1_0_, student0_.sclass as sclass1_0_, student0_.tno as tno1_0_ from student student0_ where student0_.sno=?
[07/May/2012:10:03:06] info ( 3500): CORE3282: stdout: loadStdYearlyInfo:class 8
[07/May/2012:10:03:06] info ( 3500): CORE3282: stdout: Hibernate: /* update com.aims.beans.Student */ update student set sname=?, sclass=?, tno=? where sno=?
[07/May/2012:10:03:06] info ( 3500): CORE3282: stdout: loadStdYearlyInfo2

如何在数据库中更新学生班级价值? 但这里的价值得到了更新。 让我知道。 是否有任何问题?

最佳回答

这是解放党的预期行为。

当记录由一次记名会装时,其实例将载于persistent 。 由本届会议管理。 如果惯犯的价值观发生变化,则被视为ir脏。 在冲洗过程中(e.Session.flush()),解放者将发现所有 d脏病例。 (我们称之为“aut dir eck>,生成并印发必要的文件,以便更新相应的非行记录,确保非行的记录与在经贸总协定中的相应记录相同。)

冲积物场会议的行为由<代码>决定。 页: 1 页: 1 页: 1 因此,在您的法典中,尽管你没有打上<代码>session.flush(),但抽取过程仍然发生,无法发布这些声明。

关于守则的一些评论:

Transaction tx1=session1.beginTransaction();   

/**
 * o2 is the in the persistent state and managed by session1
 */
Object o2=session1.get(Student.class,new Integer(3));

/**
 *As the value of o2 is changed , it becomes dirty and hibernate will issue an UPDATE SQL 
 *for it during flushing.
 */
 ((Student)o2).setSclass("8");

/**
 * save() only has effect on the transient instance. Nothing will 
 * be done when calling it on the persistent instance . So removing this line of code 
 * still produces the same result.
 */
session1.save(o2);
log.info("loadStdYearlyInfo:class "+((Student)o2).getSclass());

/**
 *I believe default FlushMode (FlushMode.AUTO) is used in here ,so session.flush()  will be invoked implicitly before tx1.commit().
 *Automatic dirty checking occurs and UPDATE SQL is generated and issued to the DB 
 *to update the dirty o2 instance
 */ 
tx1.commit();
session1.close();
问题回答

It s an expected behaviour.

persistent<>。 持久性物体的<代码>save基本上被忽视。 但是,Hibernate自动跟踪持久性物体的变化,并相应更新数据库,从而更新<编码>日说明。

I think it is because you are working with transactions. When you commit a transaction it automatically flushes any changes to an already loaded entity, in this case your Student object.
It does not save the object because it already has an unique identifier attached to the session.
I would have thought Hibernate would have thrown an exception because you tried to save an already attached object.

I hope this helps you out.





相关问题
Multiple Hibernate instances using C3P0

I am facing a weird problem and it seems to be c3p0 related. I am starting two instances of an app in the same java vm which interact with each other. After some operations "APPARENT DEADLOCK" ...

Hibernate vs Ibatis caching

We can speed up a hibernate app easyly with 2nd level cache using infinispan or ehcache/terracotta,... but ibatis only have a simple interface to implement for caching. And hibernate knows more ...

Using annotations to implement a static join in hibernate

I m relatively new to hibernate and was wondering if someone could help me out. While I have no issues implementing a normal join on multiple columns in hibernate using the @JoinColumns tag, I m ...

Hibernate query with fetch question

I have a 2 entities in a One-To-Many relationship: OfficeView.java: public class OfficeView implements java.io.Serializable { private Integer officeId; private String addr1; private ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

Hibernate/GORM: collection was not processed by flush()

I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate....

Hibernate Criteria API equivalent for "elements()"

Is it possible to implement the following query using Criteria API? select order from ORDER as order,ITEM as item where item.itemID like ITM_01 and item in elements(order.items)

热门标签