这是配置<?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());News类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;
}
}
异常
Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [entity.News#1]
at org.hibernate.impl.SessionFactoryImpl$2.handleEntityNotFound(SessionFactoryImpl.java:433)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:233)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:285)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1090)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:974)
at Test.main(Test.java:14)
<!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());News类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;
}
}
异常
Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [entity.News#1]
at org.hibernate.impl.SessionFactoryImpl$2.handleEntityNotFound(SessionFactoryImpl.java:433)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:233)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:285)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1090)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:974)
at Test.main(Test.java:14)
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对象ne
最后又打印ne.getNewContent(),但是newContent是没有值的吧。
起码也要给News类弄个构造方法,给它的三个属性初始化一下。
News new=(News)se.load(ne.getClass(),new Integer(1));
public News(){
}
public News(int newId,String newTitle,String newContent){
this.newId=newId;
this.newTitle=newTitle;
this.newContent=newContent;}