-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to map Custom sql types? (create type x as)
PostPosted: Sun Sep 10, 2006 6:51 pm 
Newbie

Joined: Fri Feb 06, 2004 12:16 pm
Posts: 2
I can't find a way to map the next sql example (with postgres 8.1.4 custom types http://www.postgresql.org/docs/8.1/inte ... types.html )

CREATE TYPE PHONETYPE AS (
country VARCHAR,
area VARCHAR,
number VARCHAR
);

CREATE TABLE CLIENTS (
id INT4 NOT NULL,
companyname VARCHAR NOT NULL,
...
mobile PHONETYPE NULL,
phone PHONETYPE NOT NULL,
...etc
);

I tried to create a PhoneType extending CompositeUserType but that didn't seemed to work.

My hibernate mapping is

<hibernate-mapping>
<class name="biz.admin.client.dao.Clients" table="clients" schema="public">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="sequence" />
</id>
...
<property name="mobile" type="biz.admin.client.dao.PhoneType">
<column name="mobile" />
</property>
<property name="phone" type="biz.admin.client.dao.PhoneType">
<column name="phone" />
</property>
<property name="fax" type="biz.admin.client.dao.PhoneType">
<column name="fax" />
</property>
...
</class>
</hibernate-mapping>

The error I have

00:38:53,175 DEBUG Configuration:1130 - resolving reference to class: biz.admin.client.dao.Country
00:38:53,175 DEBUG Configuration:1130 - resolving reference to class: biz.mli%%%% Error Creating SessionFactory %%%%
org.hibernate.MappingException: property mapping has wrong number of columns: biz.admin.client.dao.Clients.mobile type: biz.admin.client.dao.PhoneType
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:396)
at org.hibernate.mapping.RootClass.validate(RootClass.java:192)
at org.hibernate.cfg.Configuration.validate(Configuration.java:984)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1169)
at biz.admin.client.dao.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:60)
at biz.admin.client.dao.HibernateSessionFactory.getSession(HibernateSessionFactory.java:43)
at biz.tests.dao.TestDAO.start(TestDAO.java:41)
at biz.tests.dao.TestDAO.main(TestDAO.java:37)


Using hibernate 3.1.3 and postgres 8.1.4

I am not sure that creating a CompositUserType is the right way to go for mapping custom sql types, is it? As it is not a composition of several columns. But still I need to work with that legacy database.

Should I post my complete code of my PhoneType also?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 11, 2006 3:07 am 
Regular
Regular

Joined: Mon May 08, 2006 6:00 am
Posts: 53
Location: India
It would be good if you post the source code also , i hope you are following the way it has been specified in one of the samples in Hibernate in action, page 206. Over here they have specified how to implement custom user type......please post the source code.

Sudhri


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 11, 2006 4:16 am 
Newbie

Joined: Fri Feb 06, 2004 12:16 pm
Posts: 2
Here is my code for PhoneType, I did follow the DoubleStringType implementation found in the test suite of hibernate.

But; I think the main difference is that the examples I found are compositions of several db columns and in my case it is not multiple fields coded in one column.

/**
* PhoneType.java
* Created on: Sep 10, 2006
* @author xcosyns2
*
* @version $Revision: 1.0$
**/
package biz.admin.client.dao;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Struct;
import java.sql.Types;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.type.Type;
import org.hibernate.usertype.CompositeUserType;

/**
* @author xcosyns2
*
*/
public class PhoneType implements CompositeUserType {

private static final int[] SQL_TYPE = {Types.STRUCT };

private static final String DB_OBJECT_TYPE = "PHONETYPE";

public Object assemble(
Serializable cached,
SessionImplementor session,
Object owner) {

return deepCopy(cached);
}

public String[] getPropertyNames() {
return new String[] { "country", "area", "number" };
}

public Type[] getPropertyTypes() {
return new Type[] { Hibernate.STRING, Hibernate.STRING, Hibernate.STRING };
}

public Object getPropertyValue(Object component, int property) {
Phone phone = (Phone) component;
String res = null;
switch (property) {
case 0:
res = phone.getCountry();
break;
case 1:
res = phone.getArea();
break;
case 2:
res = phone.getNumber();
break;
default:
System.out.println("PhoneType error getting property on index" + property);
break;
}
return res;
}

public void setPropertyValue(
Object component,
int property,
Object value) {
Phone phone = (Phone) component;
switch (property) {
case 0:
phone.setCountry((String) value);
break;
case 1:
phone.setArea((String) value);
break;
case 2:
phone.setNumber((String) value);
break;
default:
System.out.println("PhoneType error setting property on index" + property);
break;
}

( (String[]) component )[property] = (String) value;
}

public Object deepCopy(Object value) throws HibernateException {
if (value == null) {
return null;
}
final Phone phone = (Phone) value;
final Phone clone = new Phone();
clone.setCountry(phone.getCountry());
clone.setArea(phone.getArea());
clone.setNumber(phone.getNumber());
return clone;
}

public Serializable disassemble(Object value, SessionImplementor session) {
return (Serializable) deepCopy(value);
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#equals(java.lang.Object, java.lang.Object)
*/
public boolean equals(Object o1, Object o2) throws HibernateException {
if (o1 == o2){
return true;
}
if (o1 == null || o2 == null) {
return false;
}

final Phone phone1 = (Phone) o1;
final Phone phone2 = (Phone) o2;
return equals(phone1.getNumber(), phone2.getNumber())
&& equals(phone1.getArea(), phone2.getArea())
&& equals(phone1.getCountry(), phone2.getCountry());
}

private boolean equals(final String str1, final String str2) {
//TODO Test for nullpointer?
return str1.trim().equalsIgnoreCase(str2.trim());
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
*/
public int hashCode(Object o) throws HibernateException {
return o.hashCode();
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#isMutable()
*/
public boolean isMutable() {
return true;
}

public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
throws HibernateException, SQLException {
assert names.length == 1;
//final Struct struct = (Struct) rs.getObject(names[0]);
if (rs.wasNull()) {
return null;
}
final Phone phone = new Phone();
phone.setCountry((String) Hibernate.STRING.nullSafeGet(rs, names[0]));
phone.setArea((String) Hibernate.STRING.nullSafeGet(rs, names[1]));
phone.setNumber((String) Hibernate.STRING.nullSafeGet(rs, names[2]));
return phone;
}



public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
throws HibernateException, SQLException {
if (value == null) {
Hibernate.STRING.nullSafeSet(st, "", index);
Hibernate.STRING.nullSafeSet(st, "", index+1);
Hibernate.STRING.nullSafeSet(st, "", index+2);

} else {
Phone phone = (Phone)value;
// statement.setString(index, phone.getCountry());
// statement.setString(index+1, phone.getArea());
// statement.setString(index+2, phone.getNumber());
Hibernate.STRING.nullSafeSet(st, phone.getCountry(), index);
Hibernate.STRING.nullSafeSet(st, phone.getArea(), index+1);
Hibernate.STRING.nullSafeSet(st, phone.getNumber(), index+2);
}
}


public Object replace(Object original, Object target, SessionImplementor session, Object owner)
throws HibernateException {
return deepCopy(original);
}

public Class returnedClass() {
return Phone.class;
}

public int[] sqlTypes() {
return SQL_TYPE;
}

}


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