-->
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: [Retrieving Objects] Question
PostPosted: Tue Sep 23, 2008 3:26 pm 
Newbie

Joined: Tue Sep 23, 2008 2:22 pm
Posts: 4
Hello everyone. Yes I'm new to Hibernate. Yes I've searched through the references and followed the tutorials and searched the forums. I'm very sorry if this is a lame question or a repeat. If it's a repeat please just point me in the right direction.

I am designing an application which will have several different POJO's with Map attributes. I would love to use Criteria (preferably Example) queries to retrieve objects, as this seems like an awesome way to grab my persisted objects.

However, it wasn't clear to me from the documentation exactly how these queries worked, especially with respect to List and Map attributes. I would like to be able to use an Example query, but I want it to match when the object's Map is a superset of my Example's Map. Same with Lists.

Is this possible or will I have to do something more complex with HQL or SQL?

Thanks in advance for all your help. And I apologize again if this is a lame question. You guys sure have plenty of warnings for n00bs... ;)

-Steve


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2008 6:36 pm 
Newbie

Joined: Tue Sep 23, 2008 2:22 pm
Posts: 4
From the documentation, it seems like Restrictions are designed to work with strings and numbers and that's it. If I want to work with lists and Maps (other than straight "equal" or "notEqual"), then it seems like I'll have to build it myself.

Is my perception correct here?

-Steve


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2008 1:36 am 
Beginner
Beginner

Joined: Wed Sep 24, 2008 5:44 pm
Posts: 34
Are you asking how to retrieve data from mapped classes using a session?

Have you initialized configured your sessionFactory? Created some sort of HibernateUtil class to retrieve your hibernate session? Then call

Code:
HibernateUtil.getSessionFactory().getSession().createCriteria(Example.class).list;


This returns a List of Examples from your database.

I'm not exactly sure what you're trying to do. You can .add criterion to the Criteria. The options provided are quite robust.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2008 3:08 pm 
Newbie

Joined: Tue Sep 23, 2008 2:22 pm
Posts: 4
I apologize for not including any code. Obviously my question is unclear without an example. So here goes:

I pulled an example from here and modified it a bit. I'm ignoring the part about lazy loading and changing the classes around.

So here's the deal. Suppose we're storing City objects and each City has a Name, and a HashMap with keys of Streets and Values of PostalCodes.

In this case, my goal would be to pull all the Cities with a street called "Embarcadero".

Code:
<hibernate-mapping package="com.xebia.map" default-access="field">
  <class name="City">
    <id name="id">
      <generator class="native"/>
    </id>
<property name="name"/>
<map name="postalCodes">
      <key column="id"/>
      <index column="streetName" type="string">
      <element column="zipCode" type="string">
    </map>

  </class>
</hibernate-mapping>

City city1 = new City("Oakland");
city.add("Hegenberger", "94606");
city.add("Outlook", "94605");
city.add("Embarcadero", "94603");

City city2 = new City("San Francisco");
city.add("Embarcadero", "94111");
city.add("Market", "94113");
city.add("Castro", "94102");

Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
session.save(city1);
session.save(city2);
transaction.commit();
session.close();

session = factory.openSession();

//now will this pull back both cities?
City exampleCity = new City("Example");
exampleCity.add("Embarcadero", null);
Example example = Example.create(exampleCity).enableLike();
List cities1 = session.createCriteria(Person.class)
                 .add(example).list();

//or will this pull back both cities?
List cities2 = session.createCriteria(City.class)
                .add(Restrictions.like("postalCodes.streetName", "Embarcadero"))
                .list();

//OR will I need to just break down and use HQL?
//and is this HQL even right?
List cities3 = session.createQuery("select c from City c " +
                           "join fetch c.postalCodes " +
                           "where c.postalCodes.streetName = :streetName")
                 .setParameter("streetName", "Embarcadero");


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2008 3:59 pm 
Beginner
Beginner

Joined: Wed Sep 24, 2008 5:44 pm
Posts: 34
I think I get where you're going with this and I think it would be possible to do using Criteria or HQL.

I realize that what you provided was just an example to illustrate your point but I think there are a few issues with it.
The postalCodes map should be referencing some sort of postalCode table or street table. I'm assuming these values don't live in the city table.

As for how to get the data out, I don't know what that first one means or what Example is, but this looks kinda crazy.
I think you could pull off using Criteria with a join alias in this case, see
http://www.javalobby.org/articles/hibernatequery102/
As for the HQL that looks pretty ok, I honestly haven't written anything very complex in HQL so I can't promise you anything.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 29, 2008 7:09 pm 
Newbie

Joined: Tue Sep 23, 2008 2:22 pm
Posts: 4
Thanks for your responses phen0m. It's going to be awhile before I can starting trying this out, as I have a lot of groundwork to lay first. But I like to know that my plan will work before I start out... so that I don't waste a lot of time.

Anyway thanks for the link too. I don't think that a Criteria join alias will work, but I'll try it and get back to you... :)

Thanks again.

If anyone else has any tidbits for me, I'd greatly appreciate them.


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.