Hibernate:ArrayList作为复合键的一部分

时间:2014-09-05 20:00:27

标签: hibernate key composite

我有一个描述GPX轨道的实体:

package my.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;

public class Track implements Serializable {
    private static final long serialVersionUID = -9138958070798036048L;
    private List<TrackSegment> track;
    private Id id;
    private long trackId;

    @Autowired
    private Account account;

    public Track() {
        this.track = new ArrayList<>();
        this.id = new Id();
    }

    public Id getId() {
        return id;
    }

    public void setId(Id id) {
        this.id = id;
    }

    public void addSegment( TrackSegment trkseg ) {
        track.add(trkseg);
    }

    public List<TrackSegment> getTrack() {
        return track;
    }

    public void setTrack( List<TrackSegment> track ) {
        this.track = track;
    }

    public long getTrackId() {
        return trackId;
    }

    public void setTrackId(long trackId) {
        this.trackId = trackId;
    }

    @PostConstruct
    public void init() {
        this.id.setCustomerId(account.getParentAccountId());
    }

    public void setCreateDate(Date createDate) {
        this.id.setCreateDate(createDate);
    }
}

Id.java:

package my.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Id implements Serializable {
    private static final long serialVersionUID = -9118607746459983559L;
    private long customerId;
    private List<Date> createDate;
    private long trackId;

    public Id() {
        this.createDate = new ArrayList<>();
    }

    public long getCustomerId() {
        return customerId;
    }

    public void setCustomerId(long customerId) {
        this.customerId = customerId;
    }

    public List<Date> getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate.add(createDate);
    }

    public long getTrackId() {
        return trackId;
    }

    public void setTrackId(long trackId) {
        this.trackId = trackId;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((createDate == null) ? 0 : createDate.hashCode());
        result = prime * result + (int) (customerId ^ (customerId >>> 32));
        result = prime * result + (int) (trackId ^ (trackId >>> 32));
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Id other = (Id) obj;
        if (createDate == null) {
            if (other.createDate != null)
                return false;
        } else if (!createDate.equals(other.createDate))
            return false;
        if (customerId != other.customerId)
            return false;
        if (trackId != other.trackId)
            return false;
        return true;
    }
}

如您所见,Id是具有一对客户ID和创建日期的密钥(每个客户都是唯一的。)

Tracks.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="my.domain.domain.Track" table="TRACKS" schema="DBO" dynamic-update="true" dynamic-insert="true" batch-size="30">
        <composite-id name="id" class="my.domain.domain.Id" unsaved-value="undefined">
            <key-property name="customerId" type="long">
                <column name="CUSTOMERID" not-null="true"/>
            </key-property>
            <key-property name="createDate" type="date">
                <column name="CREATEDATE" not-null="true"/>
            </key-property>
        </composite-id>
        <component name="track" class="my.domain.domain.TrackSegment">
            <property name="longitude" type="double">
                <column name="LONGITUDE"  not-null="true"/>
            </property>
            <property name="latitude" type="double">
                <column name="LATITUDE" not-null="true" />
            </property>
            <property name="altitude" type="double">
                <column name="ALTITUDE" not-null="true" />
            </property>
            <property name="pointTime" type="timestamp">
                <column name="POINTTIME" not-null="true" length="7" />
            </property>
        </component>
        <property name="trackId" type="long">
            <column name="TRACKID"  not-null="true"/>
        </property>
    </class>
</hibernate-mapping>

在解析输入XML之后,Track类的实例是一个完整的容器,可以保存GPX数据。但是当我试图坚持实体时,Hibernate会抛出异常:

  

线程“main”中的异常java.lang.ClassCastException:   java.util.ArrayList无法强制转换为java.util.Date       在org.hibernate.type.descriptor.java.JdbcDateTypeDescriptor.extractHashCode(JdbcDateTypeDescriptor.java:40)       at org.hibernate.type.AbstractStandardBasicType.getHashCode(AbstractStandardBasicType.java:214)       at org.hibernate.type.AbstractStandardBasicType.getHashCode(AbstractStandardBasicType.java:218)       在org.hibernate.type.ComponentType.getHashCode(ComponentType.java:257)       在org.hibernate.engine.spi.EntityKey.generateHashCode(EntityKey.java:76)       在org.hibernate.engine.spi.EntityKey。(EntityKey.java:71)       在org.hibernate.internal.AbstractSessionImpl.generateEntityKey(AbstractSessionImpl.java:248)       在org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:172)       at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:135)       at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)       at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)       在org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)       在org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)       在org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)       在org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:683)       在org.hibernate.internal.SessionImpl.save(SessionImpl.java:675)       at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)       at my.domain.domain.dao.impl.TrackDaoImpl.saveTrack(TrackDaoImpl.java:41)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)       at java.lang.reflect.Method.invoke(Unknown Source)       在org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)       在org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)       在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)       at org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:96)       at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)       在org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)       在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)       在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)       在com.sun.proxy。$ Proxy17.saveTrack(未知来源)       at my.domain.utilities.NMEAData.saveGPX(NMEAData.java:97)       在my.domain.Main.main(Main.java:49)

我哪里错了? Hibernate是否支持复合键中的列表?

谢谢。

0 个答案:

没有答案