您的位置:首页 > 其它

hibernate中update设置

2015-09-17 14:37 274 查看
hibernate三种状态与update之间的关系:

1.transient对象不可以更新,自己设定id对象可以更新。

2.更新detached对象,更新之后转为persistent对象。

3.对于persistent对象只要修改其设定值就会自动发生更新。

此外还有merg和saveOrUpdate方法。

只更新部分字段有三种方法:

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

[html] view
plaincopyprint?

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

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

[java] view
plaincopyprint?

@Column(updatable=false)  

    public int getAge() {  

        return age;  

    }  

2.使用XML中的 dynamic-update="true"

[html] view
plaincopyprint?

<class name="com.sccin.entity.Student"  table="student" dynamic-update="true">  

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

但这样的方法在Annotation中没有

 3.使用HQL语句(灵活,方便)

 使用HQL语句修改数据

[java] view
plaincopyprint?

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();  

    }  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: