-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to query composite key
PostPosted: Wed Feb 01, 2006 9:06 pm 
Beginner
Beginner

Joined: Thu Jun 30, 2005 4:14 pm
Posts: 25
Hi Folks,

I have a help table, a primary key consists of contextid field and appid field.

But I don't know how to query.

the id is passed to query

Help.HelpId id = new Help.HelpId(new Integer(1), new Integer(99)) ;

q = sess.createQuery("from Help where hp.id = :id" )
.setParameter("id", searchKeyObj);

But I got this error.

Hibernate: select help0_.ContextID as ContextID, help0_.AppID as AppID, help0_.Path as Path1_, help0_.Title as Title1_ from tblHelp help0_ where (help0_.ContextID, help0_.AppID)=?
17:58:24,960 WARN JDBCExceptionReporter: SQL Error: 170, SQLState: 37000
17:58:24,960 ERROR JDBCExceptionReporter: Line 1: Incorrect syntax near ','.
------------- ---------------- ---------------
------------- Standard Error -----------------
org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:59)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:1564)
at org.hibernate.loader.Loader.list(Loader.java:1544)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:271)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:830)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)

----------------------------------------------------

Please help. Thank you very much!

Chwang

please also take look Help.class and Help.hbm.xml files.



<hibernate-mapping package="com.server.persistence.datamodel">

<class name="Help" table="tblHelp" lazy="true">
<composite-id name="id" class="com.server.persistence.datamodel.Help$HelpId"
access="field"
unsaved-value="none">

<key-property name="contextId"
access="field"
column="ContextID"
length="16"/>

<key-property name="appId"
access="field"
column="AppID"
length="16"/>
</composite-id>


<property name="path" type="string" column="Path" length="55" not-null="false"/>
<property name="title" type="string" column="Title" length="55" not-null="false"/>

</class>
</hibernate-mapping>


----------------------


public class Help implements Serializable,Comparable {


// ******************* Begin Inner composite Id class ******************* //
public static class HelpId implements Serializable {
public Integer contextId;
public Integer appId;

public HelpId() {}

public HelpId(Integer contextId, Integer appId) {
this.contextId = contextId;
this.appId = appId;
}

public boolean equals(Object o) {
if (o instanceof HelpId) {
HelpId that = (HelpId)o;
return this.contextId.equals(that.contextId) &&
this.appId.equals(that.appId);
} else {
return false;
}
}

public int hashCode() {
return contextId.hashCode() + appId.hashCode();
}
}
// ******************* End Inner composite Id class ******************* //

// ===============

public Help(){}

private HelpId id = new HelpId();
private String path;
private String title;

public HelpId getId()
{
return id;
}
/**
public void setHelpId(HelpId i)
{
helpId = i;
}
**/

public String getPath()
{
return path;
}

public void setPath(String s)
{
path = s;
}

public String getTitle()
{
return title;
}

public void setTitle(String s)
{
title = s;
}

public int compareTo(Object o) {

if (o instanceof Help)
return getPath().compareTo(((Help)o).getPath());
return 0;
}


}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 9:27 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Have you read section 5.1.5 of the ref docs?
Quote:
Unfortunately, this approach to composite identifiers means that a persistent object is its own identifier. There is no convenient "handle" other than the object itself. You must instantiate an instance of the persistent class itself and populate its identifier properties before you can load() the persistent state associated with a composite key. We call this approach an embedded composite identifier, and discourage it for serious applications.

You can use mapped ids (same section) to work around this. It involves defining a POJO for the key object. You already have the POJO, Help.HelpId. So this is the solution you want. Read that section for the gotchas involved in this solution.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 02, 2006 12:40 pm 
Beginner
Beginner

Joined: Thu Jun 30, 2005 4:14 pm
Posts: 25
Thank you,

So I tried to use in this way.

Help.HelpId hid = new Help.HelpId(new Integer(1), new Integer(99)) ;


Help ob = (Help)sess.load(Help.class, hid);

But I got the java.lang.NullPointerException at load().
I check the Help table. Actually the number are avaiable in Help table.

Do you know reason why?

Thank you very ncuh

Chwang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 02, 2006 5:06 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
According to section 5.1.5, you need the fields in the composite id to be in both the composite id class (Help.HelpId) and the mapped class (Help). Sorry for not making that clearer yesterday.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 03, 2006 12:09 pm 
Beginner
Beginner

Joined: Thu Jun 30, 2005 4:14 pm
Posts: 25
Thank you very much,
With your help , I have fixed this issue.

Best Regards,

Chwang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.