hibernate按主键查询 [英] hibernate Query by primary key

查看:185
本文介绍了hibernate按主键查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过主键创建查询。
假设我有类主键,PersonKey,属性是name和id。

I wanna create query by primary key. Supposed I have class primary key, PersonKey, the properties is name and id.

我有Person类,属性是PersonKey,地址,DOB。

I have Person class, the property is PersonKey, address, DOB.

现在,我想通过主键搜索人。

Now, I wanna search person by primary key.

首先,我创建PersonKey的实例,然后设置名称变成:joe,并且id变为:007

First, I create instance of PersonKey, and set the name become: joe, and id become:007

我可以通过ID传递密钥变量???

can I get the person by ID, by pass the key variable ???

person.findByKey(someKey); ,但逻辑没有标准

person.findByKey(someKey); , but the logic do not criteria

推荐答案

是的,你可以。假设 PersonKey Serializable ,只需将其传递给 get 方法:

Yes you can. Assuming the PersonKey is Serializable, simply pass it to the get method:

PersonKey pk = new PersonKey(007l, "joe");
Person person = (Person) session.get(Person.class, pk);

这篇关于hibernate按主键查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆