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

标签 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)

有人可以指出我如何消除此异常的正确方向吗?

最佳答案

为什么不使用组件映射

<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>

关于java - Hibernate 中多次出现自定义 CompositeUserType 导致 MappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9164338/

相关文章:

hibernate - 如何停止审核 hibernate 环境中的创建操作?

java - JPA 乐观锁定规范是否支持根据客户端提供的版本进行验证

java - 如何映射这个复合主键?

c++ - 区分用户类型和原语

Java Graphics 在没有扩展的情况下不执行 Paint 函数

java - 如何使用Java+Google Data API测量上传码率

java - UTF-8 字符中的无效字符常量

java - 列表数组(Int,Int)

c# - 如何根据用户类型禁用按钮

hibernate - 如何在 Hibernate 中全局定义 UserType 的名称?