Hibernate中多次出现自定义CompositeUserType导致MappingException

时间:2012-02-06 17:27:04

标签: java hibernate usertype

您好我有以下代码:

public class Trail {
    private int trailID;
    private Location startLocation;
    private Location destination;

    // Getters and setters
}

哪个位置是自定义数据库类型,它不是表

public class LocationType implements org.hibernate.usertype.CompositeUserType {
    @Override
    public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
        if ( value == null ) {
            DoubleType.INSTANCE.set(st, null, index, session);
            DoubleType.INSTANCE.set(st, null, index+1, session);
            DoubleType.INSTANCE.set(st, null, index+2, session);
            StringType.INSTANCE.set(st, null, index+3, session);
        } else {
            final Location loc = (Location) value;
            DoubleType.INSTANCE.set(st, loc.getLatitude(), index, session);
            DoubleType.INSTANCE.set(st, loc.getLongitude(), index+1, session);
            DoubleType.INSTANCE.set(st, loc.getAltitude(), index+2, session);
            StringType.INSTANCE.set(st, loc.getDescription(), index+3, session);
        }
    }

    // Other functions
}

这是我的位置等级

public class Location implements Serializable {
    ...
}

Trail.hbm.xml如下所示:

<hibernate-mapping package="com.mytest.walking">
    <class name="Trail" table="TB_Trail" dynamic-insert="true">
        <id name="TrailID" column="TrailID" >
            <generator class="native"/>
        </id>
        <property name="StartLocation" type="LocationType" >
            <column name="latitude" />
            <column name="longitude" />
            <column name="altitude" />
            <column name="description" />
        </property>
        <property name="Destination" type="LocationType" >
            <column name="latitude" />
            <column name="longitude" />
            <column name="altitude" />
            <column name="description" />
        </property>
    </class>
</hibernate-mapping>

然后我得到以下异常

Caused by: org.hibernate.MappingException: property mapping has wrong number of columns: com.mytest.walking.Trail.Destination type: com.mytest.walking.LocationType
        at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:468)
        at org.hibernate.mapping.RootClass.validate(RootClass.java:268)
        at org.hibernate.cfg.Configuration.validate(Configuration.java:1287)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1729)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1775)
        at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:184)
        at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:314)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)

有人能指出我如何摆脱这种例外的正确方向吗?

2 个答案:

答案 0 :(得分:0)

为什么不使用组件映射

<component name="StartLocation" >
    <property name="..." column="latitude" />
    <property name="..." column="longitude" />
    <property name="..." column="altitude" />
    <property name="..." column="description" />
</property>
<component name="Destination" >
    <property name="..." column="latitude" />
    <property name="..." column="longitude" />
    <property name="..." column="altitude" />
    <property name="..." column="description" />
</property>

答案 1 :(得分:0)

我找到了答案。只需以不同方式命名列。它们不需要与自定义类型名称相同。

<hibernate-mapping package="com.mytest.walking">
    <class name="Trail" table="TB_Trail" dynamic-insert="true">
        <id name="TrailID" column="TrailID" >
            <generator class="native"/>
        </id>
        <property name="StartLocation" type="LocationType" >
            <column name="StartLocationLatitude" />
            <column name="StartLocationLongitude" />
            <column name="StartLocationAltitude" />
            <column name="StartLocationDescription" />
        </property>
        <property name="Destination" type="LocationType" >
            <column name="DestinationLatitude" />
            <column name="DestinationLongitude" />
            <column name="DestinationAltitude" />
            <column name="DestinationDescription" />
        </property>
    </class>
</hibernate-mapping>