hibernate-criteria

How to group by projections with Hibernate

心不动则不痛 提交于 2019-12-10 09:45:13
问题 I need to find a list of students that are in a specific group and located in a specific address along with their phone numbers in their location. My main issue is I can not retrieve phone numbers of each student as a collection. For example if I have student1,student2. Phone 1111 for student1 in location1 and phone 2222 and phone 3333 for student2 in location 1 and phone 444 for student2 in location2. Lets say I have Student1 Alex group1 1111 Location1 Street1 Student3 Jack group1 93939

select “all columns” with “group by” in hibernate criteria queries

我的梦境 提交于 2019-12-10 02:17:31
问题 I want to write a criteria query using "group by" and want to return all the columns. Plane sql is like this: select * from Tab group by client_name order by creation_time; I understand that it will have count(distinct client_name) number of rows. My current query which doesn't seem to give proper result is as follows: Criteria criteria = getSession(requestType).createCriteria(Tab.class); criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("client_name")));

Hibernate criteria upon associated objects

陌路散爱 提交于 2019-12-09 23:13:18
问题 There is a class"Item" and it has some associated classes called Vehicle,Vmodel,Category,ItemName,Brand,SizeModel.Those each class has properites id and a name(for example Vehicle class, "vid" and "vname").Item class has itemcode. Also I need to get Item objects from a given sample Item object(called " sItem ") which equal to my sample item object's properties and my object's associated objects properties. Here is my code Session session = getSession(); List list = null; try { list = session

How to use key of MAP in Criteria Query?

你说的曾经没有我的故事 提交于 2019-12-09 16:11:27
问题 I have a Bean like this Class TestA { Map<String,TestB> testBMap; } Class TestB { String data; ... } I want to fetch the TestA data along with the map testBMap where key ='test1' . How can i do this using Hibernate. 回答1: The key must be the value of one of the persistent fields of TestB (let's says this field is names "foo"), so this code should work : Criteria criteria = session.createCriteria(TestA.class, "a"); criteria.createAlias("a.testBMap", "b"); criteria.add(Restrictions.eq("b.foo",

Hibernate hql/criteria result contains collection

谁说我不能喝 提交于 2019-12-09 03:44:34
问题 I have two entities: public class Photo { Long id; String url; @ManyToOne @JoinColumn(name ="user_id") User user; // other fields and getters/setters } And second: public class User { Long id; @OneToMany(mappedBy = "user") private Collection<Photo> photos; // other fields and getters/setters } I am trying to get this DTO: public class UserDTO { Long id; List<String> photosUrls; } But I can't find the right solution. I wrote next criteria - find user with photos by login: getCurrentSession()

Hibernate: Query By Example involving one-to-many relationship

对着背影说爱祢 提交于 2019-12-08 18:27:26
I've recently started playing with the query by example component of the Criteria API and have run into a strange issue - an org.hibernate.QueryException is thrown when trying to perform a search. My scenarios is as follows: I have a class A, which as one of its properties has a set of instances of class B (Set< B> listOfBs). This is mapped as a one-to-many relationship in A. I was hoping to set a criteria query on an example instance of B, for example specifying all B's with a property value of "somevalue", and then apply that criteria to find all A's that have such a B in their set. This is

Criteria.DISTINCT_ROOT_ENTITY doesn't prevent duplicated objects

让人想犯罪 __ 提交于 2019-12-08 16:41:56
问题 I have following dao method: @Override public List<AdminRole> findAll() { Session session = sessionFactory.getCurrentSession(); Criteria criteria = session.createCriteria(AdminRole.class); criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); return criteria.list(); } Actually I want to retrieve all entries from database. Sometimes I see duplicates. This happens when I add user with AdminRole. I have read that it is possible when I use EAGER fetch type and this should be fix adding

Hibernate: Query By Example involving one-to-many relationship

心不动则不痛 提交于 2019-12-08 04:32:44
问题 I've recently started playing with the query by example component of the Criteria API and have run into a strange issue - an org.hibernate.QueryException is thrown when trying to perform a search. My scenarios is as follows: I have a class A, which as one of its properties has a set of instances of class B (Set< B> listOfBs). This is mapped as a one-to-many relationship in A. I was hoping to set a criteria query on an example instance of B, for example specifying all B's with a property value

Equivalent criteria query for named query

僤鯓⒐⒋嵵緔 提交于 2019-12-08 00:47:46
问题 My named query looks like this, thanks to here. @NamedQuery( name="Cat.favourites", query="select c from Usercat as uc inner join uc.cat as c where uc.isFavourtie = true and uc.user = :user") And the call to implement looks like this : Session session = sessionFactory.getCurrentSession(); Query query = session.getNamedQuery("Cat.favourites"); query.setEntity("user", myCurrentUser); return query.list(); What would be the equivalent criteria query that returns a list of cats ? 回答1: With JPA 2.0

hibernate.jpa.criteria.BasicPathUsageException: Cannot join to attribute of basic type

孤街醉人 提交于 2019-12-07 19:21:30
问题 I have two tables: Tax and TaxRule . There is one column same in both table i.e TAX_RULE_ID . But don't have any Hibernate mapping like OneToOne or OneToMany . Both table looks like- TAX @Id @Column(name = "TAX_RATE_ID") private Long taxRateId; @Column(name = "TAX_RULE_ID") private Long taxRuleId; @Column(name = "TAX_TYPE") private String taxType; TAX_RULE @Id @Column(name = "TAX_RULE_ID") private Long taxRuleId; @Column(name = "TAX_CD") private String TaxCode; @Column(name = "STATE") private