-->
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 do I get SQLQuery to Return the following Entity
PostPosted: Wed Jul 01, 2009 12:40 pm 
Beginner
Beginner

Joined: Thu May 28, 2009 10:25 am
Posts: 21
For various reasons beyond the scope of this question, we persist certain POJOs (DocumentAttribute) in multiple tables . To get hibernate to play nice with this arrangement, I use a SQL query to retrieve data.

What I'd like to do is retrieve the Document POJO using the following method , but Hibernate/SQL is only happy retrieving the DocumentAttribute object. If I try to specify just the docattr.DOCUMENT_ID , and add the Document.class entity , .. Hibernate attempts to load the document.id, document.name etc .. and fails, obviously . I'm guessing I need some sort of left join to get this work, but am not sure of the specifics.

Thanks

Code:
   @Override
   public List<Document> getDocs(String tableName, long attrId) throws SystemFailureException {

      HibernateSessionHelper sessionHelper = null;
      Session session = null;
      List<DocumentAttribute> docList = null;
      try {
         sessionHelper = HibernateSessionHelperSingletonFactory.GetInstance();
         session = sessionHelper.beginSession(entityClasses);

//========> I WOULD LIKE TO RETRIEVE JUST THE DOCUMENT OBJECT HERE, AND NOT THE DOCUMENTATTRIBUTE OBJECT
         SQLQuery sqlQuery = session.createSQLQuery("select docattr.ID, docattr.ATTRIBUTE_ID, docattr.DOCUMENT_ID from " + tableName + " docattr where docattr.ATTRIBUTE_ID=:pAttrId").addEntity(DocumentAttribute.class);

         sqlQuery.setLong("pAttrId", attrId);


         docList = (List<DocumentAttribute>) sqlQuery.list();
      } catch (HibernateException e) {
         throw new SystemFailureException(e);
      } finally {
         if (session != null)
            sessionHelper.closeSession(session, false);
      }

//========> HACK TO EXTRACT DOCUMENT OBJECT FROM DOCUMENTATTRIBUTE
      return docAttrListTodocList(docList);
   }


The relevant POJOs follow below:

The document attribute datastructure works somewhat like a join datastructure, although it is explicit, as opposed to an implicit join table created by hibernate.
Code:
@Entity
@Table(name = "DOCUMENTATTRIBUTE", uniqueConstraints = { @UniqueConstraint(columnNames = { "ATTRIBUTE_ID", "DOCUMENT_ID" }) })
public class DocumentAttribute implements Serializable {

   @Id
   @PrimaryKeyJoinColumn
   @Column(name = "ID")
   @GeneratedValue(strategy = GenerationType.AUTO)
   private long id;

   @ManyToOne(cascade = { CascadeType.ALL } , optional=false)
   @PrimaryKeyJoinColumn(name = "ATTRIBUTE_ID")
   private Attribute attribute;

   @ManyToOne(cascade = { CascadeType.ALL } , optional=false)
   @PrimaryKeyJoinColumn(name = "DOCUMENT_ID")
   private Document document;
}


where
Code:
@Entity
@Table(name = "DOCUMENT")
public class Document implements Serializable , IFieldsAccessibleAsString{

   @Id
   @Column(name = "ID")
   @GeneratedValue(strategy = GenerationType.AUTO)
   private long id;
   
   @Column(name = "NAME")
   private String name;
}



Thanks


Top
 Profile  
 
 Post subject: Re: How do I get SQLQuery to Return the following Entity
PostPosted: Fri Jul 03, 2009 4:43 am 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
If you use more tables, can't you use the SecondaryTable annotation and then use Hibernate normally?

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=13mappingoneclasstotwodatabasetablesjpahibernate
http://www.coderanch.com/t/218948/Object-Relational-Mapping/java/Hibernate-Hibernate-Mapping-Multiple-Table

I don't know, but maybe you just need a normal join to do:

Code:
SQLQuery sqlQuery = session.createSQLQuery("select doc.* from DocumentAttribute docattr join Document doc on docattr.document_id = doc.document_id and docattr.ATTRIBUTE_ID=:pAttrId").addEntity(Document.class);

sqlQuery.setLong("pAttrId", attrId);

docList = (List<Document>) sqlQuery.list()


Top
 Profile  
 
 Post subject: Re: How do I get SQLQuery to Return the following Entity
PostPosted: Mon Jul 06, 2009 10:41 am 
Beginner
Beginner

Joined: Thu May 28, 2009 10:25 am
Posts: 21
FWIW, .. I ended up using a normal join.
Code:
SQLQuery sqlQuery2 = session.createSQLQuery("select document.ID , document.NAME from document inner join " + tableName + " docattr on  document.id = docattr.DOCUMENT_ID where   docattr.ATTRIBUTE_ID=:pAttrId ").addEntity(Document.class);
sqlQuery2.setLong("pAttrId", attrId);
resultList2 = sqlQuery2.list();


I'l llook at the secondary table annotation


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.