我在 MyEclipse IDE 中编写了这个 hibernate 程序。我想从学生表中检索数据并显示在控制台中,我想检索主键值为 2 的行(因为我在数据库中的表中输入) 我收到以下异常: 线程“main”中的异常 org.hibernate.MappingException:未知实体: java.lang.整数
程序:
package info.inetsolv;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibJavaPrgm {
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sf = cfg.buildSessionFactory();
Session hsession = sf.openSession();
Transaction ts = hsession.beginTransaction();
Student stu=new Student();
hsession.load(2,stu);
ts.commit();
hsession.close();
sf.close();
}
}
最佳答案
有两个Session.load()您可以使用的方法:
load(Class theClass, Serializable id)
Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists.
和
load(Object object, Serializable id)
Read the persistent state associated with the given identifier into the given transient instance.
这些方法都不将 ID 作为第一个参数。
你想要
Student student = (Student) session.load(Student.class, 2);
或
Student student = new Student();
session.load(student, 2);
关于java - 关于 hibernate 中的数据检索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14029895/