java - 有人可以解释一下这个 hibernate 方法的作用吗?

标签 java hibernate

我是 hibernate 的初学者。我在互联网上看到了一些示例,

org.hibernate.Session session;

//assuming session instance is initialized 

    SampleBean msoft=(SampleBean)session.get(SampleBean.class,id);
    //**id** is of the type Long

文档解释是,

Object get(Class clazz, Serializable id)

Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.

我想知道

  1. 这里的id是否为主键?
  2. 有人可以解释一下这个方法是如何工作的吗?是否只返回SampleBean对象中的一行?
  3. 如果返回多行会发生什么?

PS:使用SampleBean映射的表的主键是INT类型。

最佳答案

I want to know, whether here the id is the primary key?

是的。 ID 应该是唯一的。

Can some body explain me , how this method works,

在数据库中查找指定 ID 并返回 clazz 的实例。

Whether it returns only one row in the SampleBean object?

是的。由于 id 是唯一的,因此应该只有一行。

What will happen if it returns more than one row?

如果 id 是唯一的或主键,则不会发生。

关于java - 有人可以解释一下这个 hibernate 方法的作用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5373982/

相关文章:

java - 迭代器如何绑定(bind)到集合接口(interface)

java - 无法为方法 public Abstract java.util.Optional 创建查询元模型

java - @NotNull 在 JPA/Hibernate 中默认

java - Hibernate:如何使用同一个表(或实体)的两列(字段)创建约束

java - 更改 Naturald 参数名称后出错

java regex 忽略 URL 中的域名并考虑除此之外的所有内容

java - Solrj 与 Solr Suggester

java - 如何从最后到第一个迭代ArrayList?

java - ReplaceChild() 时 org.w3c.dom.DOMException 没有详细消息

mysql - 如何通过传递另一个列值来检索列值