-->
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.  [ 5 posts ] 
Author Message
 Post subject: Persisting a String property separately in a lookup table
PostPosted: Tue Jul 29, 2008 6:02 pm 
Newbie

Joined: Tue Jul 29, 2008 5:20 pm
Posts: 3
Hi everyone!

I am new to Hibernate and I usually try solving things on my own. I have been searching all of Hibernate's documentation, Google, the forums, etc. I haven't been able to find a solution to my problem, even if I found out things that were close, but not close enough.

I would like to have a nice elegant solution to the following problem. All suggestions are welcome:

I have a class that looks something like this:

Code:
package schema;

public class Event {

    protected String eventType;

    /**
     * Gets the value of the eventType property.
     *
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getEventType() {
        return eventType;
    }

    /**
     * Sets the value of the eventType property.
     *
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setEventType(String value) {
        this.eventType = value;
    }

    // Possibly more properties in this style here...

    // The rest of the properties here such as eventId, etc...

}


This code was generated by xjc (the JAXB framework), so ideally I would like not to modify this code at all. Extending this class may be a possibility, but it's not ideal.

I am not a master of databases in general, so please bare with me if this will be a convoluted description: In the database that I am using I have a table called EVENT. The PK for this is EVENT_ID and there is also a column called EVENT_TYPE that is of type BIGINT (a foreign key). There is also another table called EVENT_TYPE_LKP, which is a lookup table for the codes being stored in the EVENT.EVENT_TYPE (I'm using this notation to refer to the EVENT_TYPE column of the EVENT table). The EVENT_TYPE_LKP table contains a PK named EVENT_TYPE and a column named EVENT_TYPE_DESCRIPTION containing a VARCHAR. Basically in the EVENT_TYPE_LKP.EVENT_TYPE_DESCRIPTION I will have the actual String containing the eventType property from the Event class.

The EVENT.EVENT_TYPE column is just a foreign key referencing the PK of the EVENT_TYPE_LKP table. This way, in the EVENT table I can just store some code (or key) of the eventType, while the actual String associated with that event type should go inside the EVENT_TYPE_LKP table in the EVENT_TYPE_DESCRIPTION column.

One can assume that the EVENT_TYPE_LKP table will not change very often. In other words, the application that I will be writing does not have to write to this table. It only has to read from it. However, codes (PKs) in this table may change and new Descriptions may be added with new PKs associated, separately by a DBA.

What I would like is whenever I save an instance of the Event class using Hibernate to the database, Hibernate should fill in the EVENT.EVENT_TYPE foreign key with the code taken from the EVENT_TYPE_LKP table, based on the eventType property inside the Event instance. In other words it should look inside the EVENT_TYPE_LKP table and find the row with the EVENT_TYPE_DESCRIPTION matching the eventType property and then store that Description's PK inside the EVENT.EVENT_TYPE column.

Also, whenever I retrieve an Event instance from the database using Hibernate, I would like for it to have its eventType property set by getting the EVENT_TYPE_DESCRIPTION from the associated row in the EVENT_TYPE_LKP table.

To me, the obvious relationship to use would be a many-to-one relationship between the EVENT table and the EVENT_TYPE_LKP table. But I can't seem to be able to automate the retrieval of the Description in the eventType property, and also the update of the foreign key in the EVENT table based on the String inside the eventType property.

I know that probably this could be solved easily by either modifying the above mentioned class, or by writing some SQL code. Neither of these options would be elegant since we might change DB providers later on (so SQL code might not be compatible) and the above class is derived from an XML schema using JAXB so any modifications to it would be lost on a subsequent derivation (let's say if the schema changes, and it does every once in a while).

More viable solutions would be (in order of their convenience):
- Configuring Hibernate correctly through an hbm.xml file;
- Writing a UserType for Hibernate;
- A combination of the above;
- Any other solution you might find applicable.

Thank you very much for your understanding and help. I will try to clarify any questions you have as much as possible.

PS: By the way, I looked at the <many-to-one/>, <join/> and <property/> elements and I could not get them configured in a meaningful way. I also tried defining another <class/> element for the EVENT_TYPE_LKP table without specifying a Java class, but that did not seem to work (maybe I'm not doing it right). I also considered creating a separate class for use just with Hibernate (and a new custom property added to the Event class), but I am not quite sure how to do it and combine it seamlessly with the Event class such that it does not require a separate session.save().


Top
 Profile  
 
 Post subject: Anyone?
PostPosted: Wed Jul 30, 2008 4:14 pm 
Newbie

Joined: Tue Jul 29, 2008 5:20 pm
Posts: 3
Ummm, anyone have any suggestions? any slight hints would be appreciated.

Thank you very much.

-Dan


Top
 Profile  
 
 Post subject: I have the same question
PostPosted: Wed Nov 05, 2008 5:33 pm 
Newbie

Joined: Mon Apr 14, 2008 5:38 pm
Posts: 4
I have the same question. Did you figure this out or get a reply on it?
Thanks,
Mike


Top
 Profile  
 
 Post subject: Re: I have the same question
PostPosted: Wed Nov 05, 2008 10:38 pm 
Newbie

Joined: Tue Jul 29, 2008 5:20 pm
Posts: 3
marney@teramedica.com wrote:
I have the same question. Did you figure this out or get a reply on it?
Thanks,
Mike


Hey Mike,

It's been a long time since that post. In a way I did figure it out. However I did have to change the objects that I am using for persistence. Basically they look very similar to the JAXB generated objects, however they have a special EventType instance variable instead of the original String. So given the almost two similar classes, you will have to write a Data Access Object (DAO) to retrieve lookup values from the lookup tables.

When I posted this I thought that Hibernate can relieve me from writing DAOs (I was new to Hibernate). But Hibernate just provides a mapping between Java classes and objects, and tables and rows respectively in a database. It can automate certain saving or updating tasks, but it cannot relieve the programmer from writing a DAO (which is just a bunch of methods to retrieve and persist objects).

So I wrote a DAO method (let's call it save(Event e)) that helps me persist an Event to the database. Whenever I call the method with a valid Event as a parameter, it performs a lookup for the EventType inside this object, retrieves the EventType from the database, sets it to the given Event and finally it saves the Event to the Database.

I also have a generic Object getLookup(String s, Class clazz) method that can retrieve any lookup value from any class/table in the database (as long as it exists). You would specify the String s that you are looking up and the type of object you are looking up (the Class, which maps to a particular table through the Hibernate .hbm.xml files).

I hope this helps. I could give you some more details about what I am doing, but this is the high level overview. I couldn't find anything else more suitable for what I needed to do (i.e. not change the generated JAXB classes and not having to write Hibernate plugins or any other intrusive code).


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Wed Nov 05, 2008 11:13 pm 
Newbie

Joined: Mon Apr 14, 2008 5:38 pm
Posts: 4
This is very helpful -- I just didn't know if I could do this without the DAO. It is very helpful to know that this "lookup/insert row to another table" task is outside Hibernate's scope. Thanks.
Mike


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