-->
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: Mapping file to insert into another table a constant value
PostPosted: Thu Sep 17, 2009 11:04 am 
Newbie

Joined: Thu Sep 17, 2009 9:31 am
Posts: 10
Hello. I was hoping to get some advice on my situation. I'm new to the forum and hibernate, and i've been racking my brain for days for a solution, so any help would be much appreciated.

It's a little difficult to summarize, but it involves a Code Table, locale based descriptions. Just wondering if I can set up a hibernate mapping file to work with my current database design.

A little info on our database. We have two tables we're using at the moment: ChangeReason and StandardText. Here's what they look like:

CHANGE_REASON
CHANGE_REASON_ID
CHANGE_REASON_CODE

STANDARD_TEXT
STANDARD_TEXT_ID
STANDARD_TEXT_CODE
STANDARD_TEXT_DESCRIPTION
LANGUAGE_ID
STANDARD_TEXT_ID

The java class is as follows:
Code:
public class ChangeReason {
   private Integer id;
   private String code;
   private Map<Locale, String> descriptions;

   // standard constructors, getters, setters



Notice that there is no java class for StandardText, as it's only used to store locale based text/descriptions in the DB. From a business POV, the descriptions are stored in the ChangeReason java object.

Here's my hibernate mapping file:

ChangeReason.hbm.xml
Code:
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
   
<hibernate-mapping>
   <class name="canada.borderservices.tphs.models.codes.ChangeReason"
      table="&quot;changeReason&quot;" schema="&quot;TPHS&quot;">
      
      <id name="id" column="&quot;changeReasonID&quot;">
         <generator class="sequence">
            <param name="sequence">"TPHS"."changeReason_changeReasonID_seq"</param>
         </generator>
      </id>
      
      <property
         name="code" column="&quot;changeReasonCode&quot;" />
      
      <!-- This code uses the changeReasonId, and inserts it in the StandardTextCode field in StandardText table-->
      <map name="descriptions" table="&quot;standardText&quot;" lazy="false">
         <key column="&quot;standardTextCode&quot;" />
         <index column="&quot;languageID&quot;" type="locale" />
         <element column="&quot;standardTextDescription&quot;" type="string" />
         
      </map>
      
      
   </class>
</hibernate-mapping>


So far, when creating a new ChangeReason, it inserts a row in the StandardText table for each (key, value) pair in the ChangeReason.descriptions attribute in the java class. In addition to what it does currently, each time a new StandardText entry is inserted, we need to set the value of the STANDARD_TEXT.STANDARD_TEXT_TYPE column to "changeReason" or something like that so that the row is marked as being part of the ChangeReason group.

Is there code I can put in the mapping file that will do what I need? i.e. for each row inserted, fill the value of the "STANDARD_TEXT_TYPE" to ChangeReason?

Or do I need to write specific HQL statements for such a thing? Or possible an interceptor? I've read about both of those things, but don't know exactly how it will help solve my problem.

Thanks in advance
Chris


Top
 Profile  
 
 Post subject: Re: Mapping file to insert into another table a constant value
PostPosted: Fri Sep 18, 2009 8:54 am 
Newbie

Joined: Thu Sep 17, 2009 9:31 am
Posts: 10
I have figured out what I need to do:

I ended up writing a custom <sql-insert> block in my ChangeReason.hbm.xml. This seems to be working.

Code:
         <sql-insert>
            insert into "appName"."standardText"
            ("entityID", "languageID","standardTextDescription" ,"standardTextType")
            VALUES
            (?,?,?,'changeReason')
         </sql-insert>


Top
 Profile  
 
 Post subject: Re: Mapping file to insert into another table a constant value
PostPosted: Fri Sep 18, 2009 1:04 pm 
Newbie

Joined: Thu Sep 17, 2009 9:31 am
Posts: 10
Does anyone know if this is possible without the use of the above code? I'd feel better if I didn't write custom HQL inserts.


Top
 Profile  
 
 Post subject: Re: Mapping file to insert into another table a constant value
PostPosted: Fri Sep 18, 2009 7:23 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Writing too much HQL sucks, but it does look like a reasonable solution to this problem. :(

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: Mapping file to insert into another table a constant value
PostPosted: Tue Sep 22, 2009 5:01 pm 
Newbie

Joined: Thu Sep 17, 2009 9:31 am
Posts: 10
I am trying to find a solution to the problem without writing custom HQL still. I have changed my database design as well. If interested, see post below.

viewtopic.php?f=1&t=999813


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.