Hibernate update无法更新问题
代码如下:
public class StudentOperate {
private Session session=null;
public StudentOperate(){
Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
this.session = factory.openSession();
}
public void update(Student o,Serializable id){
Transaction t = this.session.beginTransaction();
Student c =(Student)session.load(o.getClass(),id);
c.getId().setAge(new Integer(30));//要更新的值
System.out.println(c.getId().getName());
this.session.update(c);
this.session.flush();
t.commit();
this.session.close();
}
}
测试代码:
public class Testdom {
public static void main(String[] args){
Integer id1 = new Integer(1);
Integer age1 = new Integer(20);
Student student = new Student();
StudentId s1 = new StudentId(id1,"李建刚",age1);
StudentOperate studentoperate = new StudentOperate();
studentoperate.update(student,s1);
}
}
配置文件:
<hibernate-mapping>
<class name="com.Student" table="student" catalog="school" dynamic-update="true">
<composite-id name="id" class="com.StudentId" >
<key-property name="id" type="java.lang.Integer">
<column name="id" />
</key-property>
<key-property name="name" type="java.lang.String">
<column name="name" length="50" />
</key-property>
<key-property name="age" type="java.lang.Integer">
<column name="age" />
</key-property>
</composite-id>
</class>
</hibernate-mapping>
程序执行后无法更新数据库,请问各位这是什么原因?
------解决方案--------------------c.getId().setAge(new Integer(30));//要更新的值
这一行怎么解释,难道c.getId()返回的是一个Student对象?
------解决方案--------------------