问题描述
我想通过主键创建查询。
假设我有类主键,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按主键查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!