-->
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.  [ 8 posts ] 
Author Message
 Post subject: Need help with Queries...
PostPosted: Sat Oct 25, 2003 10:10 am 
Newbie

Joined: Tue Oct 21, 2003 12:06 pm
Posts: 12
I'm trying to get started with Hibernate and I'm following the documentation, but things are not working as expected.

I have a simple JDO class that I've mapped to a database table. The table has four columns, one is the primary key, the other three are strings. My JDO class has setters/getters for each attribute as well as the required empty constructor.

package com.stic.database.jdo;

class TradingPartner
{
public TradingPartner()
{
}

...
}

The mapping is:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<!-- com.stic.database.jdo.TradingPartner root -->
<class name="com.stic.database.jdo.TradingPartner" table="Trading_Partner">
<id name="id" type="long" column="ID">
<generator class="com.stic.database.access.IdGenerator"/>
</id>
<property name="tpDescription" column="TP_DESCRIPTION" type="string"/>
<property name="tpName" column="TP_NAME" type="string"/>
<property name="tpId" column="TP_ID" type="string"/>
</class>
</hibernate-mapping>

I've got four records in the database table.

Now, the query:

from com.stic.database.jdo.TradingPartner

only returns the first record in the database while the query:

from com.stic.database.jdo.TradingPartner as tp where tp.tpName = 'FOO'

where FOO is definitely in the database returns nothing.

What am I doing wrong here?

Thanks in advance,
Chuck


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 25, 2003 10:20 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Please give :
your full request code
your TradingPartner code

Why do you use home maid id generator ?
Is ID column correctly filled in your DB entries ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 25, 2003 11:08 am 
Newbie

Joined: Tue Oct 21, 2003 12:06 pm
Posts: 12
Here's the query...
//------------------------------------------------------
try
{
Session session = sFactory.openSession();
String queryString = "from com.stic.database.jdo.TradingPartner";
log.debug("Preparing query = " + queryString);
results = session.iterate(queryString);

if (results == null)
log.debug("Found no results");
else
log.debug("Found something");

session.close();

}
catch (Exception e)
{
log.error("DatabaseIterator.getAllRecords threw an exception.", e);
}
//------------------------------------------------------------

here's the TradingPartner:

package com.stic.database.jdo;

/**
* @author <a href="http://boss.bekk.no/boss/middlegen/">Middlegen</a>
*
* @jdo.bean
* identity-type="datastore"
* @jdo.rdbmapping table="TRADING_PARTNER"
*
* @jdo.finder todo...
*/
public class TradingPartner {

// fields
/**
* The id field
* @jdo.field
* modifier="persistent"
* null-value="exception"
* primary-key="true"
* @jdo.rdbmapping column="ID"
*/
private long id;

/**
* The tpId field
* @jdo.field
* modifier="persistent"
* null-value="exception"
* @jdo.rdbmapping column="TP_ID"
*/
private java.lang.String tpId;

/**
* The tpName field
* @jdo.field
* modifier="persistent"
* null-value="exception"
* @jdo.rdbmapping column="TP_NAME"
*/
private java.lang.String tpName;

/**
* The tpDescription field
* @jdo.field
* modifier="persistent"
* null-value="none"
* @jdo.rdbmapping column="TP_DESCRIPTION"
*/
private java.lang.String tpDescription;


// relations
/**
* The collection of related ConversationQueueBean
*
* @jdo.field collection-type="collection" element-type="airline.jdo.ConversationQueueBean"
*/
private java.util.Collection conversationQueuesByToTradingPartner = new java.util.HashSet();

/**
* The collection of related ConversationQueueBean
*
* @jdo.field collection-type="collection" element-type="airline.jdo.ConversationQueueBean"
*/
private java.util.Collection conversationQueuesByFromTradingPartner = new java.util.HashSet();

/**
* The collection of related ConversationRelationshipBean
*
* @jdo.field collection-type="collection" element-type="airline.jdo.ConversationRelationshipBean"
*/
private java.util.Collection conversationRelationshipsByTradingPartner2 = new java.util.HashSet();

/**
* The collection of related ConversationRelationshipBean
*
* @jdo.field collection-type="collection" element-type="airline.jdo.ConversationRelationshipBean"
*/
private java.util.Collection conversationRelationshipsByTradingPartner1 = new java.util.HashSet();


/**
* Constructs a new TradingPartnerBean with only mandatory (non-nullable) parameters
* @param id the id value
* @param tpId the tpId value
* @param tpName the tpName value
*/
public TradingPartner(long id, java.lang.String tpId, java.lang.String tpName) {
setId(id);
setTpId(tpId);
setTpName(tpName);
}

/**
* Constructs a new TradingPartnerBean with both nullable and not nullable parameters
* @param id the id value
* @param tpId the tpId value
* @param tpName the tpName value
* @param tpDescription the tpDescription value
*/
public TradingPartner(long id, java.lang.String tpId, java.lang.String tpName, java.lang.String tpDescription) {
setId(id);
setTpId(tpId);
setTpName(tpName);
setTpDescription(tpDescription);
}

public TradingPartner()
{
}

/**
* Returns the id
*
* @return the id
*/
public long getId() {
return id;
}

/**
* Sets the id
*
* @param long the new id
*/
public void setId(long newId) {
id = newId;
}

/**
* Returns the tpId
*
* @return the tpId
*/
public java.lang.String getTpId() {
return tpId;
}

/**
* Sets the tpId
*
* @param java.lang.String the new tpId
*/
public void setTpId(java.lang.String newTpId) {
tpId = newTpId;
}

/**
* Returns the tpName
*
* @return the tpName
*/
public java.lang.String getTpName() {
return tpName;
}

/**
* Sets the tpName
*
* @param java.lang.String the new tpName
*/
public void setTpName(java.lang.String newTpName) {
tpName = newTpName;
}

/**
* Returns the tpDescription
*
* @return the tpDescription
*/
public java.lang.String getTpDescription() {
return tpDescription;
}

/**
* Sets the tpDescription
*
* @param java.lang.String the new tpDescription
*/
public void setTpDescription(java.lang.String newTpDescription) {
tpDescription = newTpDescription;
}


/**
* Returns a collection of related ConversationQueueBean
*
* @return a collection of related ConversationQueueBean
*/
public java.util.Collection getConversationQueuesByToTradingPartner() {
return conversationQueuesByToTradingPartner;
}

/**
* Sets a collection of related ConversationQueueBean
*
* @param a collection of related ConversationQueueBean
*/
public void setConversationQueuesByToTradingPartner(java.util.Collection conversationQueuesByToTradingPartner) {
this.conversationQueuesByToTradingPartner = conversationQueuesByToTradingPartner;
}


/**
* Returns a collection of related ConversationQueueBean
*
* @return a collection of related ConversationQueueBean
*/
public java.util.Collection getConversationQueuesByFromTradingPartner() {
return conversationQueuesByFromTradingPartner;
}

/**
* Sets a collection of related ConversationQueueBean
*
* @param a collection of related ConversationQueueBean
*/
public void setConversationQueuesByFromTradingPartner(java.util.Collection conversationQueuesByFromTradingPartner) {
this.conversationQueuesByFromTradingPartner = conversationQueuesByFromTradingPartner;
}


/**
* Returns a collection of related ConversationRelationshipBean
*
* @return a collection of related ConversationRelationshipBean
*/
public java.util.Collection getConversationRelationshipsByTradingPartner2() {
return conversationRelationshipsByTradingPartner2;
}

/**
* Sets a collection of related ConversationRelationshipBean
*
* @param a collection of related ConversationRelationshipBean
*/
public void setConversationRelationshipsByTradingPartner2(java.util.Collection conversationRelationshipsByTradingPartner2) {
this.conversationRelationshipsByTradingPartner2 = conversationRelationshipsByTradingPartner2;
}


/**
* Returns a collection of related ConversationRelationshipBean
*
* @return a collection of related ConversationRelationshipBean
*/
public java.util.Collection getConversationRelationshipsByTradingPartner1() {
return conversationRelationshipsByTradingPartner1;
}

/**
* Sets a collection of related ConversationRelationshipBean
*
* @param a collection of related ConversationRelationshipBean
*/
public void setConversationRelationshipsByTradingPartner1(java.util.Collection conversationRelationshipsByTradingPartner1) {
this.conversationRelationshipsByTradingPartner1 = conversationRelationshipsByTradingPartner1;
}

}
//-----------------------------------------------

The ID field is filled in in all four records.

The ID Generator is homemade but was not used to populate the table. The table was populated at the time the table was created. Viewing the table via any JDBC viewer shows all four records so I know they are all there.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 25, 2003 11:22 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This code doesn't tell you there is only one record returned
Why don't you use a session.find(), it is more appropriate in most cases. (section 7.3)

What about the second one with the where clause) ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 25, 2003 12:12 pm 
Newbie

Joined: Tue Oct 21, 2003 12:06 pm
Posts: 12
Ok session.find got me the desired results (all four records)!

Thanks for that help!


Now for the query with the where clause, here's the code. I've built a generic getRecords method that uses the Parameter class (type, name & value) to pass the parameters needed for the query. The type attribute is used so the correct Query class function is used to add the parameter.

public void getPartnerByName(String tpName)
{
String query = "from com.stic.database.jdo.TradingPartner as tp where tp.tpName = :tpname";
Vector parameters = new Vector();

Parameter param = new Parameter(Parameter.STRING_TYPE, "tpname", tpName);
parameters.add(param);

getRecords(query, parameters);
}

and the getRecords code...

public void getRecords(String queryString, Vector parameters)
{
try
{
Session session = sFactory.openSession();

log.debug("Preparing query = " + queryString);
Query query = session.createQuery(queryString);

if (parameters != null)
{
for (Iterator paramIterator = parameters.iterator(); paramIterator.hasNext();)
{
Parameter param = (Parameter) paramIterator.next();
log.debug(
"Adding parameter = "
+ param.getName()
+ " with value = "
+ param.getValue()
+ " with column type = "
+ param.getColumnType());

switch (param.getColumnType())
{
case Parameter.INTEGER_TYPE :
query.setInteger(param.getName(), Integer.parseInt(param.getValue()));
case Parameter.LONG_TYPE :
query.setLong(param.getName(), Long.parseLong(param.getValue()));
case Parameter.STRING_TYPE :
query.setString(param.getName(), param.getValue());
}
}
}
log.debug("About to execute query...");
results = query.iterate();

if (results == null)
log.debug("Found no results");
else
log.debug("Found something");

session.close();

}
catch (Exception e)
{
log.error("DatabaseIterator.getRecords threw an exception.", e);
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 25, 2003 12:32 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
srvacbwilliams wrote:
results = query.iterate();

if (results == null)
log.debug("Found no results");
else
log.debug("Found something");

session.close();

Once again, this code doen't tell you there is no result to your request !

When using query.iterate(), an iterator is returned.
Code:
int nbr = 0;
while (results.hasNext()) {
  results.next();
  nbr++;
}
log.debug("Nb of result=" + nbr);


will do the work.

Be careful, iterate() doesn't load object until they are really used. If you manipulate results after session.close() it will fails.

And Once again, query.list() (same as session.find) is generally more appropriate.

Have a look at javadoc of session and query.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 25, 2003 2:01 pm 
Newbie

Joined: Tue Oct 21, 2003 12:06 pm
Posts: 12
I've changed the call to query.iterate() to query.list() and I am still not getting any results back.

going to keep looking into it.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 25, 2003 2:32 pm 
Newbie

Joined: Tue Oct 21, 2003 12:06 pm
Posts: 12
Ok, I've finally got this working the way I need it to be working!

Thanks for all the help!


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