日期:2014-05-19  浏览次数:20681 次

谁帮我解决Hibernate问题?
这是配置
XML code

<?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


测试类
Java code

        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类
Java code

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)


------解决方案--------------------
数据库里没有 newId=1的记录啊,如果不想报异常,可以用get方法,获取不到返回null
------解决方案--------------------
Java code
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;

}

------解决方案--------------------