-->
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.  [ 4 posts ] 
Author Message
 Post subject: custom mapping for inner class
PostPosted: Mon Feb 02, 2004 3:29 pm 
Newbie

Joined: Fri Jan 23, 2004 6:33 pm
Posts: 4
I have an existing object model with many classes that contain inner classes used to store a String from an enumerated set of possibilities they maintain. The inner class simply maintains the integrity of this data by restricting the possible value for the String and itself stores this value.

As non-static inner classes are not supported via hibernate, I need a way to persist the enclosing classes, retrieving the String from their non-persistable inner classes and storing this within the enclosing class's table, upon retrieveal using this String to set the appropriate value in the class's inner class.

This seems simple enough to me but I am unsure as to implement this (writing a UserType?) using hibernate. Any help or references to code of similar functionality would be much appreciated.

thanks,

C>T>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2004 3:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You should be able to work around this pretty easily using custom types and PropertyAccessors. Somehow there must be a way to access the property from the outside, I suppose. If you post a sample I would be able to give you some more details.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2004 3:47 pm 
Newbie

Joined: Fri Jan 23, 2004 6:33 pm
Posts: 4
here is a sample class, Measurement with just the relevant code, including the inner class Type:

public
class Measurement
{
/**
* Inner class for the enumeration values that the attribute type
* can assume.
*/
public
class
Type
{
int value = -1;
String name = null;
private HashMap nameToValue = new HashMap(2);

private HashMap valueToName = new HashMap(2);

final public int absolute = 0;
final public int change = 1;

Type(){
nameToValue.put("absolute",new Integer(0));
valueToName.put(new Integer(0),"absolute");
nameToValue.put("change",new Integer(1));
valueToName.put(new Integer(1),"change");
}

public int setValueByName(String name) {
value = ((Integer)nameToValue.get(name)).intValue();
return value;
}
public int getValueByName(String name) {
return ((Integer)nameToValue.get(name)).intValue();
}
public String setNameByValue(int val) {
name = ((String)valueToName.get(new Integer(val)));
return name;
}
public String getNameByValue(int val) {
return ((String)valueToName.get(new Integer(val)));
}
public int getValue() {
return value;
}
public String getName() {
return name;
}
public String toString() {
return getNameByValue(getValue());
}
}

/**
* The type of measurement
*/
Type type = new Type();

/* setter/getter below for type */
public
Type
getType()
{
return type;
}

public
void
setType(
Type type
)
{
this.type = type;
}



// Accessors/Mutators to Inner Class Type fields

/**
* Return the current name of the Enumeration type of Type.
*/ public
String
getNameType()
{
return getType().getName();
}

/**
* Return the currrent value of the Enumeration type of Type.
*/ public
int
getValueType()
{
return getType().getValue();
}

public
String
setNameByValueType (int val)
{
return getType().setNameByValue(val);
}

/**
* For Type set the Value of the Enumeration type by passing a Name to it.
* @param String name The name to be mapped to a value */
public
int
setValueByNameType (String name)
{
return getType().setValueByName(name);
}

public
String
getNameByValueType (int val)
{
return getType().getNameByValue(val);
}

public
int
getValueByNameType (String name)
{
return getType().getValueByName(name);
}

}

thanks,

C>T>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2004 3:58 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I think the easiest way would be to map something like "type" as a string property and set a custom persister for it which uses getNameType() to get an setValueByNameType() for setting (can do just the same with int, to).

I'd really say this is pretty strange however, so if you still can change your design, I would advise you to do so, using something like the enum pattern.


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