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.  [ 6 posts ] 
Author Message
 Post subject: Queries on composite-id - help needed a.s.a.p
PostPosted: Wed Aug 31, 2005 5:07 am 
Newbie

Joined: Sat Aug 27, 2005 2:14 pm
Posts: 5
Hi,

I have 3 tables :

1. Documents - pk :document-id
2. Attributesets - pk:attribute-id
and a connecting table:
3. DocumentAttributesets - pk: document-id & attribute-id

I have generated the mapping files & classes by MyEclipse plugin.

The DocumentAttributesets mapping file:

<class name="DocumentAttributesets" table="DOCUMENT_ATTRIBUTESETS">
<composite-id name="id" class="DocumentAttributesetsKey">
<key-many-to-one name="taxonomyAttributesets" column="ATTRIBUTESETID" class="TaxonomyAttributesets"/>
<key-many-to-one name="documents" column="DOCUMENTID" class="Documents"/>
</composite-id>
</class>

DocumentAttributesetsKey class contains the following members:

/** The value of the ATTRIBUTESETID component of this composite id. */
private TaxonomyAttributesets taxonomyAttributesets;

/** The value of the DOCUMENTID component of this composite id. */
private Documents documents;

I don't understand how to write select or delete HQL using the using one of the fields from the composite-id for example:

I have run a query on Documents & got a List of Documents.

1. What is the syntax of HQL query to select all DocumentAttributesets
that there Document is in the List of Documents?

2. What is the syntax of HQL query to delete all DocumentAttributesets
that there Document appears in the List of Documents?

When wrote something like:
session.createQuery("from DocumentAttributesets where DocumentAttributesets.documents in ....)
I get syntax error, hibernate doesn't understand the :DocumentAttributesets.documents



[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 31, 2005 6:12 am 
Beginner
Beginner

Joined: Mon Sep 22, 2003 5:18 am
Posts: 28
Does DocumentAttributesets contains any other columns ?
If this table is a simple link table, used to represent many-to-many relationship, than it is better to use this kind of relationship mapping in xml files for Documnt and Attribute tables.
In this case you will be able to search for document by attributes like
Code:
from Document doc where doc.attributes.elements = :attribute

where :attribute parameter value is the instance of Attribute object loaded before quering.

I think you plugin for Eclipse simlpy wrongly named the class Documnts instead of 'Document', since it is not a collection but an object in composit key.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 31, 2005 6:57 am 
Newbie

Joined: Sat Aug 27, 2005 2:14 pm
Posts: 5
DocumentAttributesets doesn't contain any other records, it is used to represent many-to-many relationship.
The eclipse plugin called the object Documents since this is the table name.

I still don't understand,
how can I delete specific documents' records from the table DocumentAttributesets ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 31, 2005 7:32 am 
Beginner
Beginner

Joined: Mon Sep 22, 2003 5:18 am
Posts: 28
This could be like this:
Code:
public void removeAttributesFromDocument(long docId)
  Documents doc = session.load(Documents.class, new Long(docId);
  doc.attributes = new HashSet();
  session.saveOrUpdate(doc); 


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 31, 2005 9:28 am 
Newbie

Joined: Sat Aug 27, 2005 2:14 pm
Posts: 5
Thanks,

But basically I would like to know how can I refer to the members of the composite key.

For example:
If I would like to do an inner join between DocumentAttributesets & Attributesets tables in order to get only the attributes that have documents containing them , in simple SQL I would do something like:
Selcect attribute_id from DocumentAttributesets docsAttr,Attributesets attr
where attr.attribute_id=docsAttr.attribute_id.

In HQL, I have tried something like:

session.createQuery("select attr from Attributesets as attr,DocumentAttributesets as docAttr where attr.attributesetid=docAttr.attributesetid").list();

and I keep getting the same error:
"could not resolve property: attributesetid of DocumentAttributesets".
This problem happens only in objects that have composite key
(hibernate resolved the attributesetid of Attributesets)

So, if you have any idea what I wrote wrong in the query...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 31, 2005 3:58 pm 
Beginner
Beginner

Joined: Mon Sep 22, 2003 5:18 am
Posts: 28
try to do the following
Code:
select doc from DocumentAttributesets docAttrset
left join docAttrset.documents doc
left join docAttrset.attributes attr
where attr.id = :attrId


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