java - 关于 hibernate 中的数据检索

标签 java hibernate

我在 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/

相关文章:

java - org.hibernate.AnnotationException : Use of @OneToMany or @ManyToMany targeting an unmapped class: core. user.User.userRole[core.user.UserRole]

hibernate 查询 - grails

java - jpa oneToMany 和 jackson 序列化问题(新手)

java - Hibernate 条件返回空结果

java - JaNeLA 的 JNLP 错误

java - 我可以在同一台服务器上运行kafka生产者和消费者吗

java - 影响 Linux 上 Java 6 中 NetworkInterface.getNetworkInterfaces 枚举的顺序

java - Hibernate Envers 为外资 OneToMany 关系创建修改字段

java - 根据先前选择的在 RecyclerView 中设置复选框

java - docker-compose selenium-grid、maven、java。用于设置功能的空 VM 池