-->
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.  [ 21 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Population of non-mapped entity
PostPosted: Mon Oct 20, 2003 8:48 am 
Regular
Regular

Joined: Wed Oct 15, 2003 4:40 pm
Posts: 67
Hello,

I have a SelectOption class with the String attributes "value" and "description" and appropriate setters/getters which I intend to use for selectbox parameters in a Collection.

The question is, what would be the best way to populate this from tables mapped in Hibernate. I can, of course select out the interesting records and manually iterate over them and generate the SelectOption items but is there a way to automate this (either in HQL or with reflection) so that I would get a collection of SelectOption objects?

Thanks in advance,
Nik


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2003 9:14 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
Nik, I've got a lot of these. I created tables in my DB (as it's easier to modify that than change my code, recompile, re-deploy, etc.) which all follow the same structure:

ID, Description

Some are:

ID, Description, Full Description (If they need a full length description.) But let's leave that alone for now. :P

What I did, was create an abstract Java Bean that has two properties called ID (long) and Description (String). I extend this for each of the tables that I have. Like this:

Code:
public abstract class TableReference {

    private long id;
    private String description;

    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public TableReference() {
        super();
    }
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
}

And here's a table that extends it...
Code:
public class PropertyType extends TableReference {
}

Then, for a nice and simple Hibernate mapping:
Code:
        <class name="PropertyType" table="PropertyType">
                <id name="id" column="ID" unsaved-value="0">
                    <generator class="identity"/>
                </id>
                <property name="description" column="Description" not-null="true"/>
        </class>


What you can do, is have some object, that has a Collection of these types. And hey presto, you have something that you can maintain in the database, and easily read into your system via a Hibernate query, like this:
Code:
Query q = session.createQuery("select c from " +
                yourObject.class.getName() +  " as c");

    List listResult = q.list();
    for (int i=0; i < listResult.size();i++) {
        YourObject yourObject = (YourObject) listResult.get(i);
        // Process as needed.
    }
...

I hope that's what you're asking...otherwise I just cut 'n pasted and typed a whole lot of nothing. :P

-G


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2003 11:42 am 
Regular
Regular

Joined: Wed Oct 15, 2003 4:40 pm
Posts: 67
[quote="Brannor McThife"
I hope that's what you're asking...otherwise I just cut 'n pasted and typed a whole lot of nothing. :P
[/quote]

Thanks for the effort but I don't think it's quite applicable in my case. The problem is that the description is not unique for a row, sometimes I want to list column1, and sometimes I want to list column2 and so on. Let's say I have a address table mapped with the columns

ID
Street
City

I might want to generate a list of SelectOption items containing all cities which has a street 'FooStreet' in them and another list of items containing all streets in 'FooCity'...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2003 11:42 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
Heh, I think I misread the question. :P

Correct me if I'm wrong.

You already have the data stored in various fields? You want to extract these from those tables, and insert them into a single table? Or simply extract them and have them as objects?

Also, what are these fields called and do they differ in each table?

As you said, you could do a simple query to extact this information, and you could do it in HQL.

Like this?

Code:
    public static ArrayList getValues (Class _class, ArrayList list) {

        try {
            //get a session
            Query q = session.createQuery("select c.value, c.description from " + _class.getName() + " as c");

            List listResult = q.list();
            for (int i=0; i < listResult.size(); i++) {
                SelectObject selectObject = (SelectObject) listResult.get(i);
                list.add(selectObject);
            }
        }   
        catch (HibernateException he) {}
    }

This way, you can have it go to each of the tables by simply passing it the class type of the table you want to query, this makes for good reuse of code.

Is this any help?

-G


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2003 11:50 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
Oops. Posted at the same time. :)

Ok. That shouldn't be too hard. Take my last post, and in that, simply add in new parameters, two Strings, one for...ah well...like this...

Code:
   public static ArrayList getValues (Class _class, ArrayList list, String value, String description) {

        try {
            //get a session
            StringBuffer sql = new StringBuffer();
            sql.append("select c.");
            sql.append(value);
            sql.append(", c.");
            sql.append(description);
            sql.append(" from ");
            sql.append(_class.getName());
            sql.append(" as c");
            Query q = session.createQuery(sql.toString());

            List listResult = q.list();
            for (int i=0; i < listResult.size(); i++) {
                SelectObject selectObject = (SelectObject) listResult.get(i);
                list.add(selectObject);
            }
        }   
        catch (HibernateException he) {}
        return list;
    }

How's that one? I passed in the List, as you may want to get it from multiple tables...not sure on that...but the HQL should work like that.

-G


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2003 11:55 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Whats wrong with:

Code:
select new SelectObject(c.name, c.description) from MyClass c where....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2003 12:02 pm 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
gavin wrote:
Whats wrong with:

Code:
select new SelectObject(c.name, c.description) from MyClass c where....

Because he's looking for c.value, not c.name.

:D

And that's what I wrote for him in my final answer. Sorry for the incorrect first post...I misread the question.

<hangs head in shame>

-G


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2003 3:35 pm 
Regular
Regular

Joined: Wed Oct 15, 2003 4:40 pm
Posts: 67
gavin wrote:
Whats wrong with:

Code:
select new SelectObject(c.name, c.description) from MyClass c where....


Aah, that could be it! And there it was at the end of section 9.4 in the reference guide :-)

Should it be possible to select into a normal class that has no hbm-mapping? I get a classnotfound (probably from the newInstance) although the class seem to be in my classpath :-/


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2003 12:42 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
I'm guessing that's what's wrong. Maybe Gavin can explain. I always thought that you can't use non-mapped classes (Your own) in HQL, or can you do type casts like this?

I still think the last method I wrote should work, and it's kinda generic, so you can use it with all your various tables/mappings.

-G


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2003 1:02 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Use the fully qualified classname.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2003 1:46 am 
Regular
Regular

Joined: Wed Oct 15, 2003 4:40 pm
Posts: 67
gavin wrote:
Use the fully qualified classname.


Aah yes, Class.forName probably requires it. Got it working, thanks to both of you for the quick response, I think a variation of Brannors advice will come in handy in another situation.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2003 3:37 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Brannor McThife wrote:
I always thought that you can't use non-mapped classes (Your own) in HQL, or can you do type casts like this?


This particular syntax does not need to use mapped classes.
It only used the appropriate constructor of the class you specified.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2003 6:55 am 
Regular
Regular

Joined: Wed Oct 15, 2003 4:40 pm
Posts: 67
In relation to this, I have another simple question:

If I have a parametrized query with 3 String variables a=:foo etc. which I use the query setString on, is there a simple way to indicate * for one of the variables if I don't want to use it as a criteria? In the same way one could do a like '%' but with a =


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2003 7:07 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
epbernard wrote:
Brannor McThife wrote:
I always thought that you can't use non-mapped classes (Your own) in HQL, or can you do type casts like this?


This particular syntax does not need to use mapped classes.
It only used the appropriate constructor of the class you specified.

Ah. Nice. Thanks. :)

Nik, perhaps build your query as a StringBuffer, adding/leaving out the parts you want or don't want, then use the setString, etc on those that you know you added.

Like,
Code:
if (string1!=null) {
    sql.append("and test1 =:testValue1 ");
}
//etc.

    query q = session.createQuery(sql);
if (string1!=null) {
    q.setString("testValue1", string1);
}

This is actually nicer than the good old preparedstatement where you had to set the position as well. (if memory serves me correct).

Oh, and feel free to tell me to shush...I love trying to come up with various code. :P

-G


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2003 7:44 am 
Regular
Regular

Joined: Wed Oct 15, 2003 4:40 pm
Posts: 67
Brannor McThife wrote:
This is actually nicer than the good old preparedstatement where you had to set the position as well. (if memory serves me correct).


I think the recommended way is still the named version. Yes, your example works but I am still kind of attached to the pureness of the preparedstatement solution :-) (if there is such a way, otherwise I take 'working' before 'pretty')


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 21 posts ]  Go to page 1, 2  Next

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.