0

我有三个类,带有 onetomany 和 manytoone 注释。以下

类别.java

@Entity
public class Category implements Serializable {
    @Id
    @GeneratedValue
    int id;
    String cat;
    @OneToMany(cascade= CascadeType.ALL)
    @JoinColumn(name="cat_id")
    private Collection<Subject> subjects = new ArrayList<Subject>();

    @OneToMany(cascade= CascadeType.ALL)
    @JoinColumn(name="cat_id")
    private Collection<Classes> classes = new ArrayList<Classes>();

    @OneToMany(cascade= CascadeType.ALL)
    @JoinColumn(name="cat_id")
    private Collection<Exam> exam = new ArrayList<Exam>();

    public Collection<Subject> getSubjects() {
        return subjects;
    }

    public void setSubjects(Collection<Subject> subjects) {
        this.subjects = subjects;
    }

    public Collection<Classes> getClasses() {
        return classes;
    }

    public void setClasses(Collection<Classes> classes) {
        this.classes = classes;
    }

    public Collection<Exam> getExam() {
        return exam;
    }

    public void setExam(Collection<Exam> exam) {
        this.exam = exam;
    }

    public Category() {
    }

    public Category(String cat) {
        this.cat = cat;
    }
     //getters/setters}

类.java

@Entity
public class Classes implements Serializable {
    @Id
    @GeneratedValue
    int id;   
    String name;
    @Column(name="cat_id")
    short cat_id;
    short yr;
    @ManyToOne
    @JoinColumn(name="cat_id", updatable=false,insertable=false)
    private Category cat;

    public Category getCat() {
        return cat;
    }

    public void setCat(Category cat) {
        this.cat = cat;
    }

    public Classes() {
    }

    public Classes(String name, short cat_id, short yr) {
        this.name = name;
        this.cat_id = cat_id;
        this.yr = yr;
    }
    //setters&getters
}

考试.java

@Entity
public class Exam {
    @Id
    @GeneratedValue
    int id;
    String name;
    short yr;
    @Column(name="cat_id")
    short cat_id;
    short total;
    @Temporal(TemporalType.TIMESTAMP)
    Date date_time;

    @ManyToOne
    @JoinColumn(name="cat_id", updatable=false,insertable=false)
    private Category cat;

    public Category getCat() {
        return cat;
    }

    public void setCat(Category cat) {
        this.cat = cat;
    }

    public Exam() {
    }

    public Exam(String name, short yr, short cat_id, short total, Date date_time) {
        this.name = name;
        this.yr = yr;
        this.cat_id = cat_id;
        this.total = total;
        this.date_time = date_time;
    }
getter setter
}

春天的xml

<?xml  version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd>
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <mvc:annotation-driven />
    <context:annotation-config />
    <context:component-scan base-package="org.app.nebula." />

    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:resources/messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="java:comp/env/jndiName"/>
        </bean>


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:resources/hibernate.cfg.xml</value>
        </property>
        <property name="packagesToScan" value="org.app.nebula.domain" />
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />  
</beans>

现在当我运行这个项目时,表已创建但它无法更改并添加外键并给出此错误

[DEBUG] 33:01(SchemaUpdate.java:execute:203) 更改表类添加索引 FK9619D00659EAC034 (cat_id),添加约束 FK9619D00659EAC034 外键 (cat_id) 引用类别 (id)

[错误] 33:01(SchemaUpdate.java:execute:212)不成功:更改表类添加索引 FK9619D00659EAC034(cat_id),添加约束 FK9619D00659EAC034 外键(cat_id)引用类别(id)

[错误] 33:01(SchemaUpdate.java:execute:213) 无法创建表 'nebula.#sql-83c_e3' (errno: 150)

[DEBUG] 33:01(SchemaUpdate.java:execute:203) 更改表检查添加索引 FK212C3F59EAC034 (cat_id),添加约束 FK212C3F59EAC034 外键 (cat_id) 引用类别 (id)

[错误] 33:01(SchemaUpdate.java:execute:212)不成功:更改表检查添加索引 FK212C3F59EAC034(cat_id),添加约束 FK212C3F59EAC034 外键(cat_id)引用类别(id)

[错误] 33:01(SchemaUpdate.java:execute:213) 无法创建表 'nebula.#sql-83c_e3' (errno: 150)

任何帮助表示赞赏。

感谢和问候

4

2 回答 2

0

外键的 Java 类型应该与您引用的类型相同。

于 2013-05-29T10:53:19.560 回答
0

对于生成的数字 ID,您应该始终使用 Long 值。更改您的 Category 类,如下所示:

@Entity public class Category implements Serializable {
  @Id
  @GeneratedValue
  Long id;

  [...]
}

顺便说一句:使用 Java 类型short并不常见(例如,如果您不必通过 JNI 访问旧版 C 接口)。

于 2013-05-29T11:33:28.183 回答