-->
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.  [ 10 posts ] 
Author Message
 Post subject: Doing join using hibernate...
PostPosted: Mon Feb 19, 2007 3:39 pm 
Newbie

Joined: Mon Feb 19, 2007 3:36 pm
Posts: 6
How can I do a "join" query in hibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 3:57 pm 
Beginner
Beginner

Joined: Tue Oct 26, 2004 8:14 am
Posts: 20
Location: Mainz, Germany
You should read the hibernate docs:
Query: http://www.hibernate.org/hib_docs/v3/reference/en/html/objectstate.html#objectstate-querying-executing

Criteria: http://www.hibernate.org/hib_docs/v3/reference/en/html/querycriteria.html


br,
Matthias


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 4:56 pm 
Newbie

Joined: Mon Feb 19, 2007 3:36 pm
Posts: 6
Why this doesn't work...
from ERMEvent e join e.status.statusType.id where e.status_statusType.code like '%Pending%'


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 5:55 pm 
Beginner
Beginner

Joined: Tue Oct 26, 2004 8:14 am
Posts: 20
Location: Mainz, Germany
Try the following:

from ERMEvent as e join e.status as s join s.statusType as t where t.code like '%Pending%'


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 6:51 pm 
Newbie

Joined: Mon Feb 19, 2007 3:36 pm
Posts: 6
I am getting this error now....
unexpected token: s near line 1, column 60 [from com.nyl.ie2.model.ERMEvent as e join e.status as join s.statusType as t where t.statusCode like '%Pending%']


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 6:56 pm 
Newbie

Joined: Mon Feb 19, 2007 3:36 pm
Posts: 6
Getting this now...
r on getERMPendingEvents null


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 10:32 pm 
Newbie

Joined: Mon Feb 19, 2007 3:36 pm
Posts: 6
Now getting this...
from ERMEvent as e join e.status.statusType.id where e.status.statusType.statusCode like '%Pending%'
java.lang.NullPointerException
at org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:312)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3275)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3067)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:58)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:60)
at java.lang.reflect.Method.invoke(Method.java:391)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy0.createQuery(Unknown Source)
at com.nyl.ie2.dao.hibernate.ERMEventDAO.getERMPendingEvents(ERMEventDAO.java:82)
at com.nyl.ie2.services.DAOHibernateService.getERMPendingEvents(DAOHibernateService.java:132)
at com.nyl.ie2.testDataAccess.EventManager.main(EventManager.java:150)
com.nyl.ie2.dao.ie2DAOException.IE2DAOException: Error on getERMPendingEvents null
at com.nyl.ie2.dao.hibernate.ERMEventDAO.getERMPendingEvents(ERMEventDAO.java:101)
at com.nyl.ie2.services.DAOHibernateService.getERMPendingEvents(DAOHibernateService.java:132)
at com.nyl.ie2.testDataAccess.EventManager.main(EventManager.java:150)
Error on getERMPendingEvents null


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 4:30 am 
Beginner
Beginner

Joined: Tue Oct 26, 2004 8:14 am
Posts: 20
Location: Mainz, Germany
Quote:
unexpected token: s near line 1, column 60 [from com.nyl.ie2.model.ERMEvent as e join e.status as join s.statusType as t where t.statusCode like '%Pending%']


You forgot a 's' after 'e.status as':
from ERMEvent as e join e.status as s join s.statusType as t where t.statusCode like '%Pending%'

Please post the classes ERMEvent, Status and StatusType and their mapping files. Maybe there is a problem.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 9:28 am 
Newbie

Joined: Mon Feb 19, 2007 3:36 pm
Posts: 6
package com.nyl.ie2.model;

import java.util.Date;
import java.util.List;

/**
* @author Marcin Czauz
*/
public class ERMEvent extends Event {

private List statusHistoryCollection;

//private Status status;

private DmsMetadata dmsMetadata;

private FilenetMetadata filenetMetadata;

private CenteraMetadata centeraMetadata;

private Date newRetentionDate;

private long requestId;

private long messageId;

/**
* @return Returns the messageId.
*/
public long getMessageId() {
return messageId;
}
/**
* @param messageId The messageId to set.
*/
public void setMessageId(long messageId) {
this.messageId = messageId;
}
/**
* @return Returns the centeraMetadata.
*/
public CenteraMetadata getCenteraMetadata() {
return centeraMetadata;
}

/**
* @param centeraMetadata The centeraMetadata to set.
*/
public void setCenteraMetadata(CenteraMetadata centeraMetadata) {
this.centeraMetadata = centeraMetadata;
}

/**
* @return Returns the dmsMetadata.
*/
public DmsMetadata getDmsMetadata() {
return dmsMetadata;
}

/**
* @param dmsMetadata The dmsMetadata to set.
*/
public void setDmsMetadata(DmsMetadata dmsMetadata) {
this.dmsMetadata = dmsMetadata;
}

/**
* @return Returns the newRetentionDate.
*/
public Date getNewRetentionDate() {
return newRetentionDate;
}

/**
* @param newRetentionDate The newRetentionDate to set.
*/
public void setNewRetentionDate(Date newRetentionDate) {
this.newRetentionDate = newRetentionDate;
}

/**
* @return Returns the requestType.
*/
public long getRequestId() {
return requestId;
}

/**
* @param requestType The requestType to set.
*/
public void setRequestId(long requestType) {
this.requestId = requestType;
}

/**
* @return Returns the statusHistoryCollection.
*/
public List getStatusHistoryCollection() {
return statusHistoryCollection;
}

/**
* @param statusHistoryCollection The statusHistoryCollection to set.
*/
public void setStatusHistoryCollection(List statusHistoryCollection) {
this.statusHistoryCollection = statusHistoryCollection;
}

/**
* @return Returns the filenetMetadata.
*/
public FilenetMetadata getFilenetMetadata() {
return filenetMetadata;
}

/**
* @param filenetMetadata The filenetMetadata to set.
*/
public void setFilenetMetadata(FilenetMetadata filenetMetadata) {
this.filenetMetadata = filenetMetadata;
}

/**
* @return Returns the status.
*/
/*public Status getStatus() {
return status;
}*/

/**
* @param status The status to set.
*/
/*public void setStatus(Status status) {
this.status = status;
}*/
}

package com.nyl.ie2.model;

import java.util.Date;

/**
* @author Marcin Czauz
*/
public abstract class Event {
private Date dateCreated;

private Status status;

private long id;

/**
* @return Returns the dateCreated.
*/
public Date getDateCreated() {
return dateCreated;
}

/**
* @param dateCreated The dateCreated to set.
*/
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}

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

/**
* @param id The id to set.
*/
public void setId(long id) {
this.id = id;
}

/**
* @return Returns the staus.
*/
public Status getStatus() {
return status;
}

/**
* @param staus The staus to set.
*/
public void setStatus(Status staus) {
this.status = staus;
}
}
package com.nyl.ie2.model;

import java.util.Date;

/**
* @author Marcin Czauz
*/
public class Status {

private Date date;

private String description;

StatusType statusType;

/**
* @return Returns the date.
*/
public Date getDate() {
return date;
}

/**
* @param date The date to set.
*/
public void setDate(Date date) {
this.date = date;
}

/**
* @return Returns the description.
*/
public String getDescription() {
return description;
}

/**
* @param description The description to set.
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @return Returns the statusType.
*/
public StatusType getStatusType() {
return statusType;
}
/**
* @param statusType The statusType to set.
*/
public void setStatusType(StatusType statusType) {
this.statusType = statusType;
}
}

package com.nyl.ie2.model;

/**
* @author Rolando Garcia
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class StatusType {
private long id;
private String statusCode;
/**
*
*/
public StatusType() {
super();
// TODO Auto-generated constructor stub
}

/**
* @return Returns the id.
*/
public long getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(long id) {
this.id = id;
}
/**
* @return Returns the statusCode.
*/
public String getStatusCode() {
return statusCode;
}
/**
* @param statusCode The statusCode to set.
*/
public void setStatusCode(String statusCode) {
this.statusCode = statusCode;
}
}

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.nyl.ie2.model.ERMEvent" table="PENDING_TRANSACTIONS">
<id name="id" type="long" column="TRANSACTION_ID">
<generator class="identity"/>
</id>
<!-- Common properties -->
<property name="dateCreated" column="REQUEST_TM"/>
<property name="messageId" column="MESSAGE_ID"/>
<property name="requestId" column="REQUEST_ID"/>
<component name="status" class="com.nyl.ie2.model.Status">
<property name="date" column="STATUS_TM"/>
<property name="description" column="STATUS_DN"/>
<component name="statusType" class="com.nyl.ie2.model.StatusType">
<property name="id" column="STATUS_ID"/>
</component>
</component>
<!-- Object specific -->
<property name="newRetentionDate" column="RETENTION_REVISED_DT"/>

<!-- DMS Metadata object -->
<component name="dmsMetadata" class="com.nyl.ie2.model.DmsMetadata">
<property name="folderNumber" column="FOLDER_NO"/>
<property name="businessType" column="DOCUMENT_TYPE_CD"/>
</component>

<!-- Filenet Metadata object -->
<component name="filenetMetadata" class="com.nyl.ie2.model.FilenetMetadata">
<property name="documentId" column="DOCUMENT_ID"/>
</component>

<!-- Centera Metadata object -->
<component name="centeraMetadata" class="com.nyl.ie2.model.CenteraMetadata">
<property name="clipId" column="CLIP_ID"/>
<property name="retentionDate" column="RETENTION_INITIAL_DT"/>
</component>
</class>
</hibernate-mapping>



Here is the method...
public static ERMEvents getERMPendingEvents() throws IE2DAOException
{
try {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
ArrayList oRecords = new ArrayList();
session.beginTransaction();
String queryStr = "'%Pending%'";
// String test="from ERMEvent as e join e.status as s join s.statusType as t where t.statusCode like " + queryStr;
String test="from ERMEvent as e join e.status.statusType.id where e.status.statusType.statusCode like " + queryStr;
System.out.println(test);
Query query = session.createQuery(test);
System.out.println("Bad item");
//Query query = session.createQuery("from ERMEvent e join e.status.statusType.id WHERE e.status.statusType.statusCode LIKE " + queryStr);
if (query==null) {
System.out.println("Bad item");
}
System.out.println("Size " + query.getQueryString());
ArrayList list = (ArrayList) query.list();

for (int index=0; index < list.size(); index++){
ERMEvent obj = (ERMEvent)list.get(index);
System.out.println("Name " + obj.getId() + " Desc " + obj.getStatus().getDescription());
oRecords.add(obj);
}

session.getTransaction().commit();
return (new ERMEvents(oRecords));
} catch (Exception e) {
e.printStackTrace();
throw new IE2DAOException("Error on getERMPendingEvents " + e.getMessage());
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 9:50 am 
Beginner
Beginner

Joined: Tue Oct 26, 2004 8:14 am
Posts: 20
Location: Mainz, Germany
Oh, I never used component mappings.

You can try:

Code:
<many-to-one name="status" class="com.nyl.ie2.model.Status"        column="<column_name_status_reference_in_Table_ERMEvent>"/>


instead of

Code:
<component name="status" class="com.nyl.ie2.model.Status">
<property name="date" column="STATUS_TM"/>
<property name="description" column="STATUS_DN"/>
</component>


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