-->
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: Fetching data from ternary associations
PostPosted: Thu Jun 26, 2008 6:27 pm 
Newbie

Joined: Sat Oct 07, 2006 4:44 pm
Posts: 4
Hibernate version:3.2.5.ga
Mysql 5.0:
Code:
Class Theme {
    @CollectionOfElements(targetElement = TernaryAssociation.class, fetch = FetchType.LAZY)
   @JoinTable(name = "themes_locations_types", joinColumns = @JoinColumn(name = "theme_id"))
   public Set<TernaryAssociation> getAssociations() {
   }
}

Class Location {}
Class Type {}

@Embeddable
Class TernaryAssociation {
@Parent
public Theme getTheme(){
}

@ManyToOne
@JoinColumn(name = "location_id", nullable = false, updatable = false)
public Location getLocation() {
}

@ManyToOne
@JoinColumn(name = "type_id", nullable = false, updatable = false)
public Type getType() {
}

}


Class A owns the relation.
First, is this the right way to have a ternary association?
Second, I am able to say A.getAssociations to get the list of associations but what I am looking for is give me C where a.id = 1 and b.id=1.

Theme
------------------------
| 1 | description 1
| 2 | description 2
| 3 | description 3

Location
------------------------
| 1 | description 1
| 2 | description 2
| 3 | description 3

Type
------------------------
| 1 | description 1
| 2 | description 2
| 3 | description 3

Theme_Location_Type
------------------------
| 1 | 1 | 1 |
| 2 | 1 | 1 |
| 3 | 1 | 1 |
| 1 | 2 | 1 |
| 2 | 2 | 1 |
| 3 | 2 | 1 |

How do I get, say 2 2 1 from Theme_Location_Type? I have a handle of Theme object. One way of doing it is get the list through theme object and loop until I find the right record.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 29, 2008 2:43 pm 
Newbie

Joined: Sat Oct 07, 2006 4:44 pm
Posts: 4
Is this very difficult to achieve or am I doing something wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 10:35 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Quote:
One way of doing it is get the list through theme object and loop until I find the right record.


This is certainly a valid way! If there aren't too many objects, it could actually be fairly efficient.

You could also use custom HQL, or even my favorite, use a Criteria Query. The Hibernate3 Criteria API greatly simplifies your queries and makes data access much easier:



Code:
public static void main(String args[]) {
  Session session = HibernateUtil.beginTransaction();
  Criterion c1 = Restrictions.gt("id", (long)2);
  Criterion c2 = Restrictions.lt("id", (long)8);
  Criterion c3 = Restrictions.isNotNull("emailAddress");
  User user = new User();
  user.setEmailAddress(".com");
  Example c4 = Example.create(user);
  c4.enableLike(MatchMode.END);
  c4.ignoreCase();
  Criteria criteria = session.createCriteria(User.class);
  criteria.add(c1);
  criteria.add(c2);
  criteria.add(c3);
  criteria.add(c4);
  List results = criteria.list();
  HibernateUtil.commitTransaction();
  for (int i = 0; i<results.size(); i++) {
    System.out.println(results.get(i).toString());
  }
}

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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.