gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:44:31 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com