These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: How to query the object by the composite primary?
PostPosted: Wed Sep 14, 2005 1:23 am 
Newbie

Joined: Mon Aug 29, 2005 12:53 am
Posts: 13
I have the class which primary key is composed of two attribute. How to query it by the user id and the group id? The following is the source code.

public class GroupMembersVO implements Serializable{
private GroupMembersVOPK groupMembersVOPK;
private int revision;

private UserAccountVO userAccountVO;
private UserGroupVO userGroupVO;

public GroupMembersVO() {
groupMembersVOPK = new GroupMembersVOPK();
}

public void setGroupMembersVOPK(GroupMembersVOPK groupMembersVOPK) {
this.groupMembersVOPK = groupMembersVOPK;
}

/**
* @hibernate.id generator-class="assigned" type="mo.umac.eticketing.common.valueobject.user.GroupMemberVOPK"
*/
public GroupMembersVOPK getGroupMembersVOPK() {
return groupMembersVOPK;
}

public void setGroupId(Long groupId) {
groupMembersVOPK.setGroupId(groupId);
}

public Long getGroupId() {
return groupMembersVOPK.getGroupId();
}

public void setUserId(Long userId) {
groupMembersVOPK.setUserId(userId);
}

public Long getUserId() {
return groupMembersVOPK.getUserId();
}


public void setRevision(int revision) {
this.revision = revision;
}

/**
* @hibernate.property length="10" not-null="true"
*/
public int getRevision() {
return revision;
}

/**
* @hibernate.many-to-one column="userId" constrained="true" insert="false" update="false"
*/
public UserAccountVO getUserAccountVO() { return userAccountVO; }
public void setUserAccountVO(UserAccountVO userAccountVO) { this.userAccountVO = userAccountVO; }

/**
* @hibernate.many-to-one column="groupId" constrained="true" insert="false" update="false"
*/
public UserGroupVO getUserGroupVO() { return userGroupVO; }
public void setUserGroupVO(UserGroupVO userGroupVO) { this.userGroupVO = userGroupVO; }

public void copyFrom(GroupMembersVO vo) {
setGroupId(vo.getGroupId());
revision = vo.getRevision();
setUserId(vo.getUserId());
}
}

public class GroupMembersVOPK implements Serializable {
private Long userId;
private Long groupId;

public GroupMembersVOPK() {
}

/**
* @hibernate.property length="10"
*/
public Long getUserId(){ return userId; }
public void setUserId(Long pUserId){ userId = pUserId; }

/**
* @hibernate.property length="8"
*/
public Long getGroupId(){ return groupId; }
public void setGroupId(Long pGroupId){ groupId = pGroupId; }

public boolean equals(Object o) {
if(o == null)
return false;

if(!(o instanceof GroupMembersVOPK))
return false;

GroupMembersVOPK gm = (GroupMembersVOPK)o;
return new EqualsBuilder().append(this.userId, gm.userId)
.append(this.getGroupId(), gm.getGroupId()).isEquals();
}

public int hashCode() {
HashCodeBuilder hashCodeBuilder = new HashCodeBuilder();
return hashCodeBuilder.append(this.userId)
.append(this.groupId).toHashCode();
}
}

public class GroupMembersVOFinder {
private GroupMembersVOFinder() {
}

public static List findByGroupIdUserId(Long groupId, Long userId)throws AppException {
Query query = HibernateManager.currentSession().createQuery("from GroupMembersVO as gm where gm.groupId=:groupId and gm.userId=:userId");

query.setParameter("groupId", groupId);
query.setParameter("userId", userId);

return query.list();
}
}

When I call the findByGroupIdUserId, it throws an exception as the below:
org.hibernate.QueryException: could not resolve property: groupId of: GroupMembersVO [from GroupMembersVO as gm where gm.groupId=:groupId and gm.userId=:userId]

at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)

at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)

at org.hibernate.persister.entity.BasicEntityPersister.toType(BasicEntityPersister.java:1094)

at org.hibernate.hql.ast.FromElementType.getPropertyType(FromElementType.java:273)

at org.hibernate.hql.ast.FromElement.getPropertyType(FromElement.java:349)

at org.hibernate.hql.ast.DotNode.getDataType(DotNode.java:474)

at org.hibernate.hql.ast.DotNode.prepareLhs(DotNode.java:207)

at org.hibernate.hql.ast.DotNode.resolve(DotNode.java:166)

at org.hibernate.hql.ast.FromReferenceNode.resolve(FromReferenceNode.java:88)

at org.hibernate.hql.ast.FromReferenceNode.resolve(FromReferenceNode.java:84)

at org.hibernate.hql.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:463)

at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:863)

at org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:3713)

at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3190)

at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1405)

at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1330)

at org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:599)

at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:404)

at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:201)

at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:151)

at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189)

at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130)

at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:84)

at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:427)

at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:885)

at org.hibernate.impl.SessionImpl.list(SessionImpl.java:834)

at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)

How to query the composite primary object?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 2:11 am 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
Instead of
Code:
Query query = HibernateManager.currentSession().createQuery("from GroupMembersVO as gm where gm.groupId=:groupId and gm.userId=:userId");

query.setParameter("groupId", groupId);
query.setParameter("userId", userId);
return query.list();


I'd try something like
Code:
GroupMembersVOPK pk = new GroupMembersVOPK();
pk.setUserId(userId);
pk.setGroupId(groupId);
return (GroupMembersVO) HibernateManager.currentSession().get(GroupMembersVO.class, pk);

which also directly returns the object you expect instead of a List (hopefully) containing only one element. With the appropriate constructor in GroupMembersVOPK you could even make it shorter...

Erik


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 7:50 am 
Newbie

Joined: Mon Aug 29, 2005 12:53 am
Posts: 13
ErikFK wrote:
Instead of
Code:
Query query = HibernateManager.currentSession().createQuery("from GroupMembersVO as gm where gm.groupId=:groupId and gm.userId=:userId");

query.setParameter("groupId", groupId);
query.setParameter("userId", userId);
return query.list();


I'd try something like
Code:
GroupMembersVOPK pk = new GroupMembersVOPK();
pk.setUserId(userId);
pk.setGroupId(groupId);
return (GroupMembersVO) HibernateManager.currentSession().get(GroupMembersVO.class, pk);

which also directly returns the object you expect instead of a List (hopefully) containing only one element. With the appropriate constructor in GroupMembersVOPK you could even make it shorter...

Erik


Thank you for your help!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.