论坛首页 Java企业应用论坛

保存父表同时保存多个子表时出现的问题

浏览 3796 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2004-04-02  
在一对多关系中,我想保存父表时同时保存多个子表,可是用HashSet,一次只能保存一条子表纪录,我改用List(ArrayList),但是出现如下异常

java.lang.ClassCastException

at net.sf.hibernate.type.SetType.wrap(SetType.java:27)

at net.sf.hibernate.impl.SessionImpl.wrap(SessionImpl.java:2519)

at net.sf.hibernate.impl.SessionImpl.wrap(SessionImpl.java:2459)

at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:708)

at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:605)

at com.tctc.ts.common.ExecuteSqlResult.execSave(ExecuteSqlResult.java:58)

at com.tctc.ts.common.Test_ExecuteSqlResult.testExecSave(Test_ExecuteSqlResult.java:78)

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 com.borland.jbuilder.unittest.JBTestRunner.run(JBTestRunner.java:210)

at com.borland.jbuilder.unittest.JBTestRunner.initiateTest(JBTestRunner.java:252)

at com.borland.jbuilder.unittest.JBTestRunner.main(JBTestRunner.java:558)

我从其他帖中,看到有人用bag,我试过用bag也出现异常
   发表时间:2004-04-02  
Could u post your hibernate mapping and po class,it seems the class in the collection not match your mapping configuration.
0 请登录后投票
   发表时间:2004-04-02  
po类如下:
package com.tctc.ts.common.persistence;

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

/** @author Hibernate CodeGenerator */
public class TsAssignmentBatch implements Serializable,POFactory {

    /** identifier field */
    private int assignmentId;

    /** nullable persistent field */
    private Integer assignedBy;

    /** nullable persistent field */
    private String name;

    /** nullable persistent field */
    private int testId;

    /** nullable persistent field */
    private Short isTimes;

    /** nullable persistent field */
    private Short duration;

    /** nullable persistent field */
    private Short isSingleTry;

    /** nullable persistent field */
    private Date startDate;

    /** nullable persistent field */
    private Date endDate;

    /** nullable persistent field */
    private Short scoreVisibility;

    /** nullable persistent field */
    private String message;

    /** nullable persistent field */
    private Date created;

    /** nullable persistent field */
    private Date lastUpdate;

    /** nullable persistent field */
    private int assignmentType;

    /** nullable persistent field */
    private Short status;


    private List tsTestSession  = new ArrayList();

    /** full constructor */
    public TsAssignmentBatch(Integer assignedBy, String name, int testId, Short isTimes, Short duration, Short isSingleTry, Date startDate, Date endDate, Short scoreVisibility, String message, Date created, Date lastUpdate, int assignmentType, Short status) {
        this.assignedBy = assignedBy;
        this.name = name;
        this.testId = testId;
        this.isTimes = isTimes;
        this.duration = duration;
        this.isSingleTry = isSingleTry;
        this.startDate = startDate;
        this.endDate = endDate;
        this.scoreVisibility = scoreVisibility;
        this.message = message;
        this.created = created;
        this.lastUpdate = lastUpdate;
        this.assignmentType = assignmentType;
        this.status = status;
    }

    /** default constructor */
    public TsAssignmentBatch() {
    }

    public int getAssignmentId() {
        return this.assignmentId;
    }

    public void setAssignmentId(int assignmentId) {
        this.assignmentId = assignmentId;
    }

    public Integer getAssignedBy() {
        return this.assignedBy;
    }

    public void setAssignedBy(Integer assignedBy) {
        this.assignedBy = assignedBy;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getTestId() {
        return this.testId;
    }

    public void setTestId(int testId) {
        this.testId = testId;
    }

    public Short getIsTimes() {
        return this.isTimes;
    }

    public void setIsTimes(Short isTimes) {
        this.isTimes = isTimes;
    }

    public Short getDuration() {
        return this.duration;
    }

    public void setDuration(Short duration) {
        this.duration = duration;
    }

    public Short getIsSingleTry() {
        return this.isSingleTry;
    }

    public void setIsSingleTry(Short isSingleTry) {
        this.isSingleTry = isSingleTry;
    }

    public Date getStartDate() {
        return this.startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return this.endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Short getScoreVisibility() {
        return this.scoreVisibility;
    }

    public void setScoreVisibility(Short scoreVisibility) {
        this.scoreVisibility = scoreVisibility;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Date getCreated() {
        return this.created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getLastUpdate() {
        return this.lastUpdate;
    }

    public void setLastUpdate(Date lastUpdate) {
        this.lastUpdate = lastUpdate;
    }

    public int getAssignmentType() {
        return this.assignmentType;
    }

    public void setAssignmentType(int assignmentType) {
        this.assignmentType = assignmentType;
    }

    public Short getStatus() {
        return this.status;
    }

    public void setStatus(Short status) {
        this.status = status;
    }

    public List getTsTestSession()
    {
    return this.tsTestSession;
    }

    public void setTsTestSession(List tsTestSessions)
    {
    this.tsTestSession= tsTestSessions;
    }

    public String toString() {
        return new ToStringBuilder(this)
            .append("assignmentId", getAssignmentId())
            .toString();
    }

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

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

}

映射文件如下:
<?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="com.tctc.ts.common.persistence.TsAssignmentBatch" table="TS_ASSIGNMENT_BATCH" dynamic-update="true" >
<id column="ASSIGNMENT_ID" name="assignmentId" type="java.lang.Integer" unsaved-value = "null">
<generator class="native"/>
</id>
<property column="ASSIGNED_BY" name="assignedBy" type="java.lang.Integer"/>
<property column="NAME" name="name" type="java.lang.String"/>
<property column="TEST_ID" name="testId" type="java.lang.Integer"/>
<property column="IS_TIMES" name="isTimes" type="java.lang.Short"/>
<property column="DURATION" name="duration" type="java.lang.Short"/>
<property column="IS_SINGLE_TRY" name="isSingleTry" type="java.lang.Short"/>
<property column="START_DATE" name="startDate" type="java.util.Date"/>
<property column="END_DATE" name="endDate" type="java.util.Date"/>
<property column="SCORE_VISIBILITY" name="scoreVisibility" type="java.lang.Short"/>
<property column="MESSAGE" name="message" type="java.lang.String"/>
<property column="CREATED" name="created" type="java.util.Date"/>
<property column="LAST_UPDATE" name="lastUpdate" type="java.util.Date"/>
<property column="ASSIGNMENT_TYPE" name="assignmentType" type="java.lang.Integer"/>
<property column="STATUS" name="status" type="java.lang.Short"/>
<set name="tsTestSession" lazy="true" inverse="true" cascade="all" sort="unsorted">
    <key column="ASSIGNMENT_ID"/>
    <one-to-many class="com.tctc.ts.common.persistence.TsTestSession"/>
</set>
</class>
0 请登录后投票
   发表时间:2004-04-06  
是不是我问的问题,太愚蠢,怎么没人理我,还是看到这么多代码,都不愿意看了?求求你们,帮帮忙呀
0 请登录后投票
   发表时间:2004-04-06  
把你做存取操作的代码贴出来吧
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics