日期:2014-05-19 浏览次数:20681 次
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="entity.News" table="NEWS"> <id name="newId" > <generator class="sequence"> <param name="sequence">NewsSequ</param> </generator> </id> <properties name="newTitle"/> <property name="newContent"/> </class> </hibernate-mapping
News ne=new News(); Configuration conf=new Configuration().configure(); SessionFactory sf=conf.buildSessionFactory(); Session se=sf.openSession(); se.load(ne,new Integer(1)); System.out.println(ne.getNewContent());
package entity; public class News { private int newId; private String newTitle; private String newContent; public int getNewId() { return newId; } public void setNewId(int newId) { this.newId = newId; } public String getNewTitle() { return newTitle; } public void setNewTitle(String newTitle) { this.newTitle = newTitle; } public String getNewContent() { return newContent; } public void setNewContent(String newContent) { this.newContent = newContent; } }
News ne=new News(); Configuration conf=new Configuration().configure(); SessionFactory sf=conf.buildSessionFactory(); Session se=sf.openSession(); se.load(ne,new Integer(1)); System.out.println(ne.getNewContent());
------解决方案--------------------
你这写法怎么觉的异样呢,应该是News new=(News)se.load(News.class,new Integer(1));或是
News new=(News)se.load(ne.getClass(),new Integer(1));
------解决方案--------------------
给实体类News加2个构造方法,一个有参数的和一个无参数的
public News(){
}
public News(int newId,String newTitle,String newContent){
this.newId=newId;
this.newTitle=newTitle;
this.newContent=newContent;
}
------解决方案--------------------