简体   繁体   English

有人可以解释一下这种休眠方法的作用吗?

[英]Can somebody explain what this hibernate method does,

I'm a beginner in hibernate.I saw some samples in the internet, 我是休眠的初学者。我在互联网上看到了一些样本,

org.hibernate.Session session;

//assuming session instance is initialized 

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

The documentation explanation is, 文档说明是,

Object get(Class clazz, Serializable id) 对象get(Class clazz,可序列化ID)

Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance. 返回具有给定标识符的给定实体类的持久实例;如果不存在这样的持久实例,则返回null。

I want to know, 我想知道,

  1. whether here the id is the primary key? id是否是主键?
  2. Can some body explain me how this method works? 有人可以解释一下这种方法的原理吗? Whether it returns only one row in the SampleBean object? 它是否仅返回SampleBean对象中的一行?
  3. What will happen if it returns more than one row? 如果返回不止一行,会发生什么?

PS:the primary key for the table mapped using SampleBean is of the INT type. PS:使用SampleBean映射的表的主键为INT类型。

I want to know, whether here the id is the primary key? 我想知道,这里的ID是否为主键?

Yes. 是。 The id should be unique. 该ID应该是唯一的。

Can some body explain me , how this method works, 可以让我解释一下这种方法的原理吗,

Looks up the DB for the specified ID and returns a instance of clazz . 在数据库中查找指定的ID,并返回clazz的实例。

Whether it returns only one row in the SampleBean object? 它是否仅返回SampleBean对象中的一行?

Yes. 是。 Since id is unique and there should be only one row. 由于id是唯一的,因此只能有一行。

What will happen if it returns more than one row? 如果返回不止一行,会发生什么?

Can't happen if id is unique or primary key. 如果id是唯一键或主键,则不会发生。

Yes, the id here is the primary key. 是的,这里的ID是主键。 It will be an instance of whatever type the specified Entity uses are its primary key (so typically Integer, Long, or String, though other types are entirely possible). 这将是指定实体使用的任何类型作为其主键的实例(因此通常是Integer,Long或String,尽管其他类型也是完全可能的)。

The method works by going to the table in the database that corresponds to the given Entity type ( SampleBean in this case) and executing a fetch based upon primary key. 该方法通过转到数据库中与给定Entity类型(在这种情况下为SampleBean )相对应的表并基于主键执行访SampleBean来工作。 In essence, it runs an SQL query that is roughly like SELECT * FROM sampleBeanTable t WHERE t.primaryKey = [id]; 本质上,它运行的SQL查询大致类似于SELECT * FROM sampleBeanTable t WHERE t.primaryKey = [id]; .

At most 1 row (or more accurately, 1 instance of the Entity) will be returned (or your database instance is very, very broken because if there are multiple rows that would mean that two or more objects have the same key). 最多将返回1行(或更准确地说,是Entity的1个实例)(或者您的数据库实例非常非常破损,因为如果存在多行,则意味着两个或更多对象具有相同的键)。 If no object is found with the given key then the method returns null . 如果未找到具有给定键的对象,则该方法返回null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM