Hibernate 2.1: How do I get a partially initialized object?

Discussions

General J2EE: Hibernate 2.1: How do I get a partially initialized object?

  1. Hi all,

    I'm still fairly new to Hibernate. I have an Event class that has tons of properties. In the main screen of my application, I want to simply list the name and ID of each event. Is there any way to restrict which properties Hibernate looks up and sets? I tried this query:

    "select event.number, event.name from Event as event"

    but it didn't work. It wasn't even giving me back an Event object.

    Basically what I want to get back is Event objects, but with only the name and id fields having values.

    Another way to look at it is - I need the equivalence of this query:

    SELECT number, name from Event

    but apparently the 'select' has a different meaning in Hibernate? Please help if you can offer any assistance... thanks!

    Joe

    Threaded Messages (3)

  2. Use constructors[ Go to top ]

    Hi Joe,

    You can use the following HQL for obtaining partially initializaed objects.

    "select new Event(event.number, event.name) from Event as event"

    Define constructor Event(int number, String name) in the Event object.

    Senthil
  3. Use constructors[ Go to top ]

    Great! Thanks for the help, Senthil.

    The HQL documentation wasn't so clear, but I get it now.

    Thanks again!
  4. I seem to run into almost the same, but still different kind of problem.
    Is there a way to partially initialize the object by excluding some of the properties?

    My object also has tons of properties, but when I load the object I'd like only some of them excluded from resulting object.
    I don't want to create a special constructor(s) which would include ALL the nessecary properties (excluding one or two of them), since it's the ordering is error prone and not obvious to see what's being done (especially when there really are tons of properties).

    What i want is something like
    session.exclude(Event.class).addProperty("name");
    session.createQuery("select e from Event e where ... ");

    Andres