-->
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.  [ 6 posts ] 
Author Message
 Post subject: Question about UserType implementation
PostPosted: Mon Dec 10, 2007 4:12 am 
Beginner
Beginner

Joined: Thu May 26, 2005 10:22 am
Posts: 20
Hi everybody.
I have the following question using User Types.
I have defined an hibernate mapping with a property called meta that maps to a specific user type.
I have implemented the Meta class that has some attributes I want to be mapped, and the Meta Type.
I have created Java source files with hbm tools from the mapping file and then the Theme class has an attribute called meta of type MetaType.
I supposed it should have a meta attribute of type Meta so I could load it with the data I wanted.

Theme theme = new Theme();
Meta meta = new Meta();
meta.set(...);

theme.set(meta);

But the type of meta is MetaType not Meta, so I don't know how to load the Meta attributes in the Theme class, because the MetaType has not attributes for the data.

Could anyone explain me how to do this?
Thank you in advance.

Hibernate version:
3.0

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.Theme"
table="Theme">
<id name="id" type="java.lang.Long">
<column name="ID">
<comment>Surrogated Key</comment>
</column>
<generator class="sequence">
<param name="sequence">THEME_SEQ</param>
</generator>
</id>
<property name="theme_id" type="java.lang.String" unique="true" unique-key="THEME_ID">
<column name="theme_id">
</column>
</property>
<property name="meta" type="com.MetaType">
<column name="META_NAME" sql-type="VARCHAR(24)">
<comment>Name label representing product</comment>
</column>
<column name="META_DESCRIPTION" sql-type="VARCHAR(24)">
<comment>Description with produc details</comment>
</column>
...
Here comes more attributes
...
</property>
</class>
</hibernate-mapping>


public class Meta implements Serializable, Cloneable {

/**
* Name label representing product/media object
*/
public String name;

/**
* Description with produc/media object details
*/
public String description;

More attributes and getters and setters
}

public class MetaType implements UserType {

User Type implementation
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 10, 2007 4:57 am 
Newbie

Joined: Fri Jun 29, 2007 9:03 am
Posts: 12
Hi
Quote:
But the type of meta is MetaType not Meta, so I don't know how to load the Meta attributes in the Theme class, because the MetaType has not attributes for the data.

u have to override the nulllSafeSet() and nullSafeGet() method of UserType in MetaType class .
you can look the following eg ..
http://www.hibernate.org/172.html


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 10, 2007 5:11 am 
Beginner
Beginner

Joined: Thu May 26, 2005 10:22 am
Posts: 20
Yes, I've done that.
But do I have to call nullSafeGet and nullSafeSet explicitly?
Could you write an example of how to load the Theme class with the Meta name, description, etc... atributtes?

public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
Meta meta = new Meta();
meta.setName((String) Hibernate.STRING.nullSafeGet(rs, names[0]));
meta.setDescription((String) Hibernate.STRING.nullSafeGet(rs, names[1]));
meta.setTextId((String) Hibernate.STRING.nullSafeGet(rs, names[2]));

...

return meta;
}

public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {

Meta meta = (Meta) value;
Hibernate.STRING.nullSafeSet(st, meta.getName(), index);
Hibernate.STRING.nullSafeSet(st, meta.getDescription(), index+1);
...

}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 10, 2007 6:28 am 
Newbie

Joined: Fri Jun 29, 2007 9:03 am
Posts: 12
i guess u have to use CompositeUserType instead of UserType.

The nullSafeSet() and nullSafeGet() looks fine . also u have to implement some other methods for CompositeUserType.
getPropertyNames() , getPropertyValue(....),getPropertyTypes(),setPropertyValue(...)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 11, 2007 4:24 am 
Beginner
Beginner

Joined: Thu May 26, 2005 10:22 am
Posts: 20
Hi!
Thank you for you help.
I've tried with the CompositeUserType but I'm having the same problem.
I don't know if I'm misunderstanding anything with UserTypes.

I have a Theme Class with some attributes and what I want is to define a type that holds a Meta Class attributes inside the Theme. The meta attributes will map to certain columns in Theme table in the database.

I've defined the MetaType both as a UserType and CompositeType.

The problem is I don't know how to instantiate a Meta class and associate it to a Theme Class because the meta attribute inside the Theme class is a MetaType not a Meta.
So if I write this code:

Theme theme = new Theme();
theme.setXXX(....)
MetaType metaType = new MetaType();
theme.setMeta(metaType);

How do I set the attributes I want to metaType? It does not have any setters or getters to associate the attributes of the Meta class it represents.
The some thing happens with CompositeType. It has two methods to access properties get and set Property.

public void setPropertyValue(Object component, int property,
Object value) throws HibernateException {}

But what this method does is to assign it's value to a component property that I pass as a parameter. But how do I associate the component to the MetaType so the mapping could be effectuated?

It would be very appreciated if anyone could explain me this.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 13, 2007 7:35 am 
Beginner
Beginner

Joined: Thu May 26, 2005 10:22 am
Posts: 20
I think I have resolved it myself.
Hibernate tools were not creating the POJO Java files from hbm correctly and were setting the attribute meta as MetaType, not Meta as it shoud.
So that was the problem.


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