-->
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.  [ 1 post ] 
Author Message
 Post subject: CompositeUserType Question
PostPosted: Wed Jul 07, 2004 12:53 am 
Newbie

Joined: Fri Oct 24, 2003 11:53 am
Posts: 2
Location: China
Hi all,

I hope somebody could help me out!

I have a table using hibernate's CompositeUserType reflection, but it can not work well.

Someone told me how to use CompositUserType class, I don't know how to do it? Has anyone have this kind of problem before? Or do you know the solution of this?

hibernate vesion is 2.1.4

hibernate's entity configure file
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="hibernate.test.CompositeUserTypeEntity" table="CompositeUserType">
<id name="id" column="id" type="string">
   <generator class="uuid.hex"/>
</id>

<property name="usertype" type="hibernate.test.MyCompositeUserType">
    <column name="intValue"/>
    <column name="floatValue"/>
    <column name="datetimeValue"/>
    <column name="stringValue"/>
</property>
</class>
</hibernate-mapping>

Entity Object :CompositeUserTypeEntity
Code:
package hibernate.test;

import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

public class CompositeUserTypeEntity implements Serializable {

    private String id;

    private MyCompositeUserType usertype;

    public CompositeUserTypeEntity() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
   
    public MyCompositeUserType getUsertype() {
        return usertype;
    }

    public void setUsertype(MyCompositeUserType usertype) {
        this.usertype = usertype;
    }
   
    public String toString() {
        return new ToStringBuilder(this).append("id", getId()).toString();
    }

    public boolean equals(Object other) {
        if (!(other instanceof CompositeUserTypeEntity)) return false;
        CompositeUserTypeEntity castOther = (CompositeUserTypeEntity) other;
        return new EqualsBuilder().append(this.getId(), castOther.getId())
                .isEquals();
    }

    public int hashCode() {
        return new HashCodeBuilder().append(getId()).toHashCode();
    }

}


database table sqlscript
Code:
CREATE TABLE [dbo].[CompositeUserType] (
   [id] [char] (32) COLLATE Chinese_PRC_CI_AS NOT NULL ,
   [intValue] [int] NULL ,
   [floatValue] [float] NULL ,
   [stringValue] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
   [datetimeValue] [datetime] NULL
) ON [PRIMARY]


CompositeUserType Object
Code:
package hibernate.test;

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

import net.sf.hibernate.CompositeUserType;
import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.engine.SessionImplementor;
import net.sf.hibernate.type.Type;

import org.apache.commons.lang.builder.EqualsBuilder;

public class MyCompositeUserType implements CompositeUserType {

    public String[] getPropertyNames() {
        return new String[] { "intValue", "floatValue", "stringValue",
                "datetimeValue"};
    }

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

    public Object getPropertyValue(Object component, int property)
            throws HibernateException {
        Component c = (Component) component;
        switch (property) {
        case 0:
            return c.getIntValue();
        case 1:
            return c.getFloatValue();
        case 2:
            return c.getStringValue();
        case 3:
            return c.getDatetimeValue();
        }
        throw new IllegalArgumentException();
    }

    public void setPropertyValue(Object component, int property, Object value)
            throws HibernateException {
        Component c = (Component) component;
        switch (property) {
        case 0:
            c.setIntValue(((Integer) value));
            return;
        case 1:
            c.setFloatValue(((Float) value));
            return;
        case 2:
            c.setStringValue(((String) value));
            return;
        case 3:
            c.setDatetimeValue(((java.sql.Date) value));
            return;
        }
        throw new IllegalArgumentException();
    }

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

    public boolean equals(Object x, Object y) throws HibernateException {
        if (x == y) return true;
        if (x == null || y == null) return false;

        Component c1 = (Component) x;
        Component c2 = (Component) y;

        return new EqualsBuilder().append(c1.getIntValue(), c2.getIntValue())
                .append(c1.getFloatValue(), c2.getFloatValue()).append(
                        c1.getStringValue(), c2.getStringValue()).append(
                        c1.getDatetimeValue(), c2.getDatetimeValue())
                .isEquals();
    }

    public Object nullSafeGet(ResultSet rs, String[] names,
            SessionImplementor session, Object owner)
            throws HibernateException, SQLException {       
        Integer intValue = (Integer)Hibernate.INTEGER.nullSafeGet(rs, names[0]);
        Float floatValue = (Float)Hibernate.FLOAT.nullSafeGet(rs, names[1]);
        String stringValue = (String)Hibernate.STRING.nullSafeGet(rs, names[2]);
        java.sql.Date datetimeValue = (java.sql.Date)Hibernate.TIMESTAMP.nullSafeGet(rs, names[3]);
       
        Component c = new Component();
        c.setIntValue(intValue);
        c.setFloatValue(floatValue);
        c.setStringValue(stringValue);
        c.setDatetimeValue(datetimeValue);
        return c;
    }

    public void nullSafeSet(PreparedStatement st, Object value, int index,
            SessionImplementor session) throws HibernateException, SQLException {
        Component c;
        if (value == null)
            c = new Component();
        else
            c = (Component) value;

        Hibernate.INTEGER.nullSafeSet(st, c.getIntValue(), index);
        Hibernate.FLOAT.nullSafeSet(st, c.getFloatValue(), index + 1);
        Hibernate.STRING.nullSafeSet(st, c.getStringValue(), index + 2);
        Hibernate.TIMESTAMP.nullSafeSet(st, c.getDatetimeValue(), index + 3);
    }

    public Object deepCopy(Object value) throws HibernateException {
        Component result = null;
        if (value == null) {
            result = null;
        } else {
            Component c = (Component) value;
            result = new Component();
            result.setIntValue(c.getIntValue());
            result.setFloatValue(c.getFloatValue());
            result.setStringValue(c.getStringValue());
            result.setDatetimeValue(c.getDatetimeValue());
        }
        return result;
    }

    public boolean isMutable() {
        return true;
    }

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

    public Object assemble(Serializable cached, SessionImplementor session,
            Object owner) throws HibernateException {
        return deepCopy(cached);
    }

}


Component Object
Code:
package hibernate.test;

import java.io.Serializable;
import java.sql.Date;

public class Component implements Serializable {
    private Integer intValue;
    private Float floatValue;
    private String stringValue;
    private Date datetimeValue;
   
    public Date getDatetimeValue() {
        return datetimeValue;
    }

    public void setDatetimeValue(Date datetimeValue) {
        this.datetimeValue = datetimeValue;
    }

    public Float getFloatValue() {
        return floatValue;
    }

    public void setFloatValue(Float floatValue) {
        this.floatValue = floatValue;
    }

    public Integer getIntValue() {
        return intValue;
    }

    public void setIntValue(Integer intValue) {
        this.intValue = intValue;
    }

    public String getStringValue() {
        return stringValue;
    }

    public void setStringValue(String stringValue) {
        this.stringValue = stringValue;
    }
}


when add new record, the value of compositeUserType object (intvalue, floatvalue, stringvalue, datetimevalue) is null in database table;but not found error messege;

when load from database by id, error messege is:

Quote:
org.springframework.orm.hibernate.HibernateSystemException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of hibernate.test.CompositeUserTypeEntity.setUsertype; nested exception is net.sf.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of hibernate.test.CompositeUserTypeEntity.setUsertype
net.sf.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of hibernate.test.CompositeUserTypeEntity.setUsertype
at net.sf.hibernate.persister.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:212)
at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2206)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:240)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:836)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:856)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:59)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:51)
at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:419)
at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2113)
at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1987)
at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1916)
at org.springframework.orm.hibernate.HibernateTemplate$3.doInHibernate(HibernateTemplate.java:198)
at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:150)
at org.springframework.orm.hibernate.HibernateTemplate.load(HibernateTemplate.java:196)
at hibernate.test.CompositeUserTypeDaoImpl.load(CompositeUserTypeDaoImpl.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.springframework.aop.framework.AopProxyUtils.invokeJoinpointUsingReflection(AopProxyUtils.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:149)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:118)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:191)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:138)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:148)
at $Proxy0.load(Unknown Source)
at hibernate.test.CompositeUserTypeDaoTest.testCompositeUserType(CompositeUserTypeDaoTest.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:410)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:294)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:182)
Caused by: net.sf.cglib.beans.BulkBeanException
at hibernate.test.CompositeUserTypeEntity$$BulkBeanByCGLIB$$e8bb255e.setPropertyValues(<generated>)
at net.sf.hibernate.persister.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:207)
... 42 more
Caused by: java.lang.ClassCastException
... 44 more


Thanks for all your helps.

_________________
己所不欲,勿施于人


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.