-->
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: Component containing constant objects
PostPosted: Thu Aug 19, 2004 12:04 pm 
Beginner
Beginner

Joined: Thu Feb 26, 2004 11:45 am
Posts: 46
I'm trying to solve a problem and beginning to think i'm barking up the wrong tree. Have tried using custom accessors, and ran into issues etc.

I will try to describe what i'm up against. I'll define a few classes. Assume proper getter/setters exist.

Code:
public class VehicleRegistration{
  private Date dateIssued;
  private int grossWeight;
  private Plate plate;
   ... 
}

public class Plate{
  private String plateNumber;
  private InvType classType;
  private InvType classStyle;

}

// objects of this type are generally immutable, there is no
// public constructor
public class InvType{
public static InvType getByCode(String code){
  // i won't bother putting the code here, but it will provide
  // the appropriate constant InvType given a code.
}
public static InvType TYPE_PASS = new (InvType("PAS", "Passenger");
public  static InvType TYPE_COMM = new (Invtype("COMM","Commercial");

public InvType STYLE_REG = new (InvType("REG","Regular");
public InvType STYLE_VANITY = new (InvType("VAN", "Vanity");

  private String code;
  private String description;
}



okay, that is a pretty simplified version of what i've got. Suffice it to say, there are more than just InvType classes that i've got to deal with.

Now what i need to do is to map the VehicleRegistration class. This object links back to Vehicle, that works, not to worry about that portion of the map. The area of interest is in Plate. My plan is to store in this particular table, PlateNumber, plateClassCode, and plateStyleCode. The codes will be written to the table. Upon reading them from the table, the appropriate InvType object (within Plate) will then be reconstructed from the code using the public static call.



Code:
        <class name="VehicleRegistration" table="REGISTRATION">
            <id name="registrationID" column="VehicleID">
              <generator class="foreign">
                  <param name="property">vehicle</param>
            </generator>
            </id>
            <discriminator column="JavaSource" type="string"/>
           <one-to-one name="vehicle"
              class="com.trivin.bo.vehicle.Vehicle"
              constrained="true"
            />
            <property name="grossWeight" />
           <property name="dateIssued" />
            <component name="plate" class="Plate">
              <property name="plateNumber" />
              <property name="plateClass"  /> 
             <property name="plateStyle"  />
            </component>
     </class>



1st thought i could handle this by defining the component section thusly
Code:
            <component name="plate" class="Plate">
              <property name="plateNumber" column = "plateNumber" />
              <property name="plateClass" access="myAccessor" column="plateClassCode" />
             <property name="plateStyle"  access="myAccessor" column="plateStyleCode" />

            </component>




And i thought myAccessor would map between the code and the object. Got it all coded up, but it appeared that Hibernate really didn't like it. I would get:
net.sf.hibernate.MappingException: Could not determine a property type for: plateClass

so then i added a type="InvType" in the property tag, and it complained about.
net.sf.hibernate.MappingException: Could not interpret type: InvType

And then i started looking at defining InvType in the mapping file. And at this point, i was wondering if i was even remotely solving this problem in a reasonable way.

I looked at using a custom user type, but it appeared that that solved more of the problem of getting in and out of the database. I didn't feel that that would solve my problem completely, and also i wanted to use this "pattern" if you will in other classes that contain plate, and didn't want to tie the actual location in the DB to this component. Because Plate can live in a lot of different places, i really just wanted to store codes, and then reconstruct the appropriate InvType.

Anybody have any suggestions on how to approach this problem?

appreciate any help or suggestions.

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 12:17 pm 
Beginner
Beginner

Joined: Mon Aug 09, 2004 3:35 pm
Posts: 22
Location: Minneapolis, MN, USA
Using a custom user type was the right answer. Their not for getting
data from and to the DB, per se, their for mapping the values into
the appropriate type and vice versa. For example, you'd implement
InvTypeUserType.nullSafeGet as

return InvType.getByCode(rs.getString(colnames[0]));

-r


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 1:49 pm 
Beginner
Beginner

Joined: Thu Feb 26, 2004 11:45 am
Posts: 46
Thanks for the quick reply

Quote:
Using a custom user type was the right answer. Their not for getting
data from and to the DB, per se, their for mapping the values into
the appropriate type and vice versa. For example, you'd implement
InvTypeUserType.nullSafeGet as

return InvType.getByCode(rs.getString(colnames[0]));



Well, i suppose, but it sort of does relate to jdbc.

nullSafeGet/Set seem to require usage and knowledge of ResultSet and PreparedStatement and knowledge of the location in the column of the item item question.

I was hoping there was a way that Hibernate would simply pass the code as a string to me, and i'd return the object and the other way around. This approach means that i have to write a custom Usertype that knows about all the resultsets that this particular object might exist in.

If this is the only way to solve this problem, i'll try to make it work.

If anybody else has any other alternatives, please let me know.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 2:48 pm 
Beginner
Beginner

Joined: Mon Aug 09, 2004 3:35 pm
Posts: 22
Location: Minneapolis, MN, USA
pgrillo wrote:
... This approach means that i have to write a custom Usertype that knows about all the resultsets that this particular object might exist in.


It only needs to know the name as it was specified in the mapping.
If you have the same type in a number of tables and they aren't all
named the same, it could get more complicated.

An alternative would be to just map it to a String and return the
constant dynamically:

class x {
String invTypeStr;

public InvType getX() {
return InvType.getByCode(this.invTypeStr);
}
}

-r


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 4:38 pm 
Beginner
Beginner

Joined: Mon Aug 09, 2004 3:35 pm
Posts: 22
Location: Minneapolis, MN, USA
rstreich wrote:
It only needs to know the name as it was specified in the mapping.
If you have the same type in a number of tables and they aren't all
named the same, it could get more complicated.


Scratch all that, must have had my head in some dark place.

You really don't need to know anything about the mapping. The
column name(s, if applicable) is supplied for you. All you need to
know is it's datatype in the DB and call the appropriate getters
on the ResultSet and setters on the PreparedStatement. Dead
easy.

-r


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.