Hibernate无法创建外键

时间:2013-05-29 09:15:14

标签: mysql hibernate spring-mvc hibernate-mapping

我有三个课程,有onetomany和manytoone注释。

下面

Category.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}

Classes.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
}

Exam.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
}

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

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

  

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

     

[ERROR] 33:01(SchemaUpdate.java:execute:212)不成功:改变   table Classes添加索引FK9619D00659EAC034(cat_id),添加约束   FK9619D00659EAC034外键(cat_id)引用类别(id)

     

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

     

[DEBUG] 33:01(SchemaUpdate.java:execute:203)alter table Exam add   索引FK212C3F59EAC034(cat_id),添加约束FK212C3F59EAC034   外键(cat_id)引用类别(id)

     

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

     

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

感谢任何帮助。

谢谢&amp;此致

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您应该始终对生成的数字ID使用Long值。 按如下方式更改Category类:

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

  [...]
}

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