如何使用唯一键而不是主键在Hibernate中检索记录 [英] How to retrieve record in Hibernate using Unique key instead of Primary Key
问题描述
使用 session.load()
或 session.get()
或任何其他方法 org.hibernate.session
,是否有可能基于Unique列而不是PK列值获取hibernate中的记录?
我的要求是我需要根据唯一列值而不是主键来获取记录。
<这就像我不想使用Criteria API。我需要使用session.get或加载类型的方法。你提到的答案是为了进行搜索。但我要求根据唯一密钥获取单个记录。为例如说。我的课Fruit有一个PK专栏ID和一个唯一的专栏fruitID,它是唯一的密钥。我想检索基于fruitID而不是ID的唯一记录。例如。水果水果=(Fruit)session.get(Fruit.class,fruitID);这里的fruitID是Fruit类的独特列。
你的意思是这样吗?
Criteria criteria = session.createCriteria(User.class);
criteria.add(Restrictions.eqProperty(uniqueField,value))
List results = criteria.list();
Object myObj = results.get(0);
检查 hibernate manual 以获取更多关于条件的信息。
Using session.load()
or session.get()
or any other method of org.hibernate.session
, is it possible to get a record in hibernate based on the Unique column rather than the PK column value?
My requirement is that I need to get the records based on the unique column value and not the primary key.
It is like I don want to use the Criteria API. I need to use the session.get or load kind of methods. The answer that you have mentioned is for doing a search. But I am asking for getting a single record based on the unique key. Say for eg. My class Fruit has a PK column ID and a unique column fruitID which is unique key. I want to retrieve a unique record based on the fruitID and not ID. eg. Fruit fruit = (Fruit) session.get(Fruit.class,fruitID); here fruitID is the unique column of Fruit class.
You mean something like this?
Criteria criteria = session.createCriteria(User.class);
criteria.add( Restrictions.eqProperty("uniqueField", "value") )
List results = criteria.list();
Object myObj = results.get(0);
Check the hibernate manual for more info about criteria
这篇关于如何使用唯一键而不是主键在Hibernate中检索记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!