-->
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: Need an example for CompositeUserType
PostPosted: Fri Sep 14, 2007 6:04 pm 
Newbie

Joined: Fri Sep 14, 2007 5:53 pm
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

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="HibExample" table="HibExample">
<id name="id" column="id">
<generator class="assigned"/>
</id>
<property name="uname" type="string" column="uname"/>
<property name="message" type="DoubleStringType">
<column name="msg1"/>
<column name="msg1"/>
</property>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
import org.hibernate.Session;
import java.util.Date;

public class HibExampleManager {
public static void main(String[] args) throws Exception{
HibExampleManager mgr = new HibExampleManager();
mgr.createAndStoreEvent();
HibernateUtil.getSessionFactory().close();
}
private void createAndStoreEvent() throws Exception{

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
HibExample example = new HibExample();
example.setId(1);
example.setMsg1("Hello");
example.setMsg2("World");
session.load(example, true);
}
}


Full stack trace of any exception that occurs:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Initial SessionFactory creation failed.org.hibernate.MappingException: property mapping has wrong number of columns: HibExample.message type: DoubleStringType
Exception in thread "main" java.lang.ExceptionInInitializerError
at HibernateUtil.<clinit>(HibernateUtil.java:12)
at HibExampleManager.createAndStoreEvent(HibExampleManager.java:12)
at HibExampleManager.main(HibExampleManager.java:7)
Caused by: org.hibernate.MappingException: property mapping has wrong number of columns: HibExample.message type: DoubleStringType
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:441)
at org.hibernate.mapping.RootClass.validate(RootClass.java:192)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1102)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1287)
at HibernateUtil.<clinit>(HibernateUtil.java:8)
... 2 more



Name and version of the database you are using:
Sybase


The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

Hi :
I have been trying to test an example of the CompositeUserType interface.
I went with the DoubleStringType example that i found in the documentation.

However i am unable to set it up.

Here is what I am trying to do
I have two string from the DB - and i would like to combine them and display - this is just an example that i am trying to get a better understanding of how to use the CompositeUserType.

Can you either tell me if i have not implemented this correctly or point me to the right documentation.

//$Id: DoubleStringType.java,v 1.4 2004/06/04 01:27:35 steveebersole Exp $
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.usertype.*;
import org.hibernate.*;
import org.hibernate.engine.*;
import org.hibernate.type.*;

public class DoubleStringType implements CompositeUserType {

private static final int[] TYPES = { Types.VARCHAR, Types.VARCHAR };

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

public Class returnedClass() {
return String[].class;
}

public boolean equals(Object x, Object y) {
if (x==y) return true;
if (x==null || y==null) return false;
return ( (String[]) x )[0].equals( ( (String[]) y )[0] ) && ( (String[]) x )[1].equals( ( (String[]) y )[1] );
}

public Object deepCopy(Object x) {
if (x==null) return null;
String[] result = new String[2];
String[] input = (String[]) x;
result[0] = input[0];
result[1] = input[1];
return result;
}

public boolean isMutable() { return true; }

public int hashCode(Object arg) throws org.hibernate.HibernateException{ return 0; }

public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
throws HibernateException, SQLException {

String first = (String) Hibernate.STRING.nullSafeGet(rs, names[0]);
String second = (String) Hibernate.STRING.nullSafeGet(rs, names[1]);

return ( first==null && second==null ) ? null : new String[] { first, second };
}

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

String[] strings = (value==null) ? new String[2] : (String[]) value;

Hibernate.STRING.nullSafeSet(st, strings[0], index);
Hibernate.STRING.nullSafeSet(st, strings[1], index+1);
}

public String[] getPropertyNames() {
return new String[] { "s1", "s2" };
}

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

public Object getPropertyValue(Object component, int property) {
return ( (String[]) component )[property];
}

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

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

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

return deepCopy(cached);
}

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

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


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 15, 2007 4:46 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
There should be an example in the test directory of the Hibernate source download.

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 27, 2007 9:15 am 
Beginner
Beginner

Joined: Wed Aug 24, 2005 5:32 am
Posts: 23
You can see a sample of the important methods of CompositeUserType in my blog:
http://www.jroller.com/eyallupu/entry/hibernate_compositeusertype_and_annotations


Eyal


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.