简体   繁体   English

休眠条件-从两个表中获取结果而没有任何关联

[英]Hibernate Criteria - Fetching results from two tables without any association

I have been trying hard to implement a functionality of Search using Hibernate criteria. 我一直在努力使用Hibernate标准来实现搜索功能。 The requirement is bit vague. 要求有点含糊。 User could enter any text/word in the search field, the search has to be made through multiple tables which doesn't have any association between each other. 用户可以在搜索字段中输入任何文本/单词,搜索必须通过多个表进行,而这些表之间没有任何关联。

following are the two entity classes. 以下是两个实体类。

Table A (EntityClassA.java)
Table B (EntityClassB.java)

if a user enter "anycharecter" that should be searched in those two tables and should return fetched list(s) matching the entered character(s). 如果用户输入“ anycharecter”,则应在这两个表中进行搜索,并且应返回与输入字符匹配的提取列表。 Also I want to store results from table A to List and result from Table B to List . 我也想将结果从表A存储到列表,将结果从表B存储到列表

I am not sure how this can achieved using hibernate criteria. 我不确定如何使用休眠条件来实现。

I could only manage to write this for one table. 我只能设法为一张桌子写这个。

public List<EntityClassAVo> search((string keywords){

List<EntityClassAVo> outputList = new ArrayList<EntityClassVo>();

Criteria criteria = etSessionFactory().getCurrentSession().createCriteria(EntityClassA.class);
//applying restriction - as I need to search entered string in all columns. 
criteria.add(Restrictions.ilike("id", keywords, MatchMode.ANYWHERE));
criteria.add(Restrictions.ilike("name", keywords, MatchMode.ANYWHERE));
criteria.add(Restrictions.ilike("age", keywords, MatchMode.ANYWHERE));
outputList = criteria.list();
return outputList;
}

Question

1) if its possible, How to write criteria for EntityClassB.java in the same "search" method? 1)如果可能,如何以相同的“搜索”方法编写EntityClassB.java的条件?

2) if question 1 is possible, how to store results fetched from EntityClassA in one object and EntityClassB in another object? 2)如果问题1是可能的,那么如何将从EntityClassA获取的结果存储在一个对象中,而将EntityClassB获取的存储在另一个对象中?

3) If the above is not simple possible, i am probably going to ask to change the design ( only if, If i have a strong reason). 3)如果上述方法不简单,我可能会要求更改设计(仅在我有充分理由的情况下)。 - I am thinking of putting a intermediate table (Table X) between (Table A) and (Table B). -我正在考虑在(表A)和(表B)之间放置一个中间表(表X)。

[EDITED] 4) There is a scenario, where user wants to search results from either of 1 table or both. [编辑] 4)在一种情况下,用户希望从1个表中的一个或两个中搜索结果。 How we are going to check this condition? 我们将如何检查这种情况?

Please help me to crack this thing. 请帮我破解这个东西。

Why can't you simply write the same criteria for EntityClassB? 为什么不能简单地为EntityClassB编写相同的条件?

Criteria criteriaA = etSessionFactory().getCurrentSession().
            createCriteria(EntityClassA.class);
criteriaA.add(Restrictions.ilike("id", keywords, MatchMode.ANYWHERE));
criteriaA.add(Restrictions.ilike("name", keywords, MatchMode.ANYWHERE));
criteriaA.add(Restrictions.ilike("age", keywords, MatchMode.ANYWHERE));

Criteria criteriaB = etSessionFactory().getCurrentSession().
            createCriteria(EntityClassB.class);
criteriaB.add(Restrictions.ilike("smth", keywords, MatchMode.ANYWHERE));
criteriaB.add(Restrictions.ilike("rows", keywords, MatchMode.ANYWHERE));
criteriaB.add(Restrictions.ilike("here", keywords, MatchMode.ANYWHERE));

and then use, for example, Map: Entity -> List to return results 然后使用例如Map:Entity-> List返回结果

map.put(EntityClassA.class, criteriaA.list());
map.put(EntityClassB.class, criteriaB.list());
return map;

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

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