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().