-->
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.  [ 7 posts ] 
Author Message
 Post subject: Help needed on converting legacy code to Hibernate
PostPosted: Tue Oct 28, 2008 5:26 pm 
Beginner
Beginner

Joined: Mon Mar 17, 2008 2:50 pm
Posts: 24
I am converting an application that was written in good old JDK 1.3 days to use Hibernate and I need a helping hand on two issues:
1. My object contains a HashMap of various properties. These properties are stored in the separate table with the following structure:
Code:
create table DOC_PROPS (
   DOC_ID               integer                        not null,
   PROP_NAME            varchar(32)                    not null,
   PROP_TYPE            char(1)                        default 'A' not null
         constraint CKC_PROP_TYPE check (PROP_TYPE in ('A','I','D','F')),
   PROP_VALUE           varchar(60)                    null,
   constraint PK_DOC_PROPS primary key (DOC_ID, PROP_NAME)
)

Where PROP_TYPE value defines the type of the property:
    A - alphanumeric (String);
    I - Integer;
    D - Date;
    F - Float;
Now in my hibernate mapping I am writing:
Code:
<class name="Document" ... >
    <property .../>
    ...
    <map name="documentProps" table="DOC_PROPS">
        <key column="DOC_ID"/>
        <map-key column="PROP_NAME" type="string"/>
        <element ??????? - here I stuck />
    </map>
</class>

Can someone suggest how to tell hibernate to convert varchar into proper object type based on PROP_TYPE value?

2. The second problem is that legacy Java code for the Document class does not have numeric surrogate key that exists in the database.
Do I have to specify <id> construct in the mapping file? I did put there <composit-id> for the natural key corresponding to the unique DB constraint. All references in the database, however are done via surrogate key.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2008 1:17 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Create class containing prop_name, prop_type and prop_value, and map it as a component, using the composite-element tag.

There is an example in the documentation, 8.2. Collections of dependent objects

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2008 1:56 pm 
Beginner
Beginner

Joined: Mon Mar 17, 2008 2:50 pm
Posts: 24
gonzao_diaz wrote:
Create class containing prop_name, prop_type and prop_value, and map it as a component, using the composite-element tag.

There is an example in the documentation, 8.2. Collections of dependent objects

Unfortunately, it is not what I need, unless I want to rewrite the business logic, which I do not.
I don't need a composite-element with a special class for it.
My element must be an object of java.lang.String, java.lang.Integer, java.lang.Float or java.util.Date and nothing else.

Somehow I have to make Hibernate dynamically change the 'type' of the element based on prop_type value. I need a special case of mapping a database row to the Map.Entry instance.
I was thinking in the lines of utilizing the <meta> but not sure how to do it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2008 6:54 pm 
Newbie

Joined: Fri Jul 14, 2006 12:05 pm
Posts: 19
I'd create a UserType. The sqlTypes() method would return an array containing 2 copies of Types.VARCHAR. The returnedClass() method would just return Object.class. The nullSafeSet() and nullSafeGet() methods would need to deal with the prop_type and prop_value columns and handle them appropriately.

Hope this helps,
Oscar


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 30, 2008 1:10 pm 
Beginner
Beginner

Joined: Mon Mar 17, 2008 2:50 pm
Posts: 24
opearce wrote:
I'd create a UserType. The sqlTypes() method would return an array containing 2 copies of Types.VARCHAR. The returnedClass() method would just return Object.class. The nullSafeSet() and nullSafeGet() methods would need to deal with the prop_type and prop_value columns and handle them appropriately.

Hope this helps,
Oscar

I'll try that approach, although, it does look quite complicated - 11 methods to implement for the UserType class. Never done it before. Methods you mentioned are easy. I am more concerned with assemble/disassemble methods.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 30, 2008 1:34 pm 
Newbie

Joined: Fri Jul 14, 2006 12:05 pm
Posts: 19
Actually, they can just delegate to deepCopy, something like this:

Code:
public Object deepCopy(Object value) {
  if (value instanceof Date) {
    // Date is mutable, so return a new Date
    return new Date(((Date) value).getTime());
  } else {
    // the other types are not mutable, so just return them
    return value;
  }
}

public Serializable disassemble(Object value) throws HibernateException {
  return (Serializable) deepCopy(value);
}

public Object assemble(Serializable cached, Object owner) throws HibernateException {
  return deepCopy(cached);
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 31, 2008 12:07 am 
Beginner
Beginner

Joined: Mon Mar 17, 2008 2:50 pm
Posts: 24
Thank yo.


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