2013-05-29 146 views
0

我有三个类与onetomany和manytoone注释。下面Hibenate无法创建外键

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:执行: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)无法创建表 '星云#SQL-83c_e3'(错误: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)

任何帮助表示赞赏。

感谢&问候

+0

请发布hibernate.cfg.xml – WeMakeSoftware

+0

我在spring.xml中配置了它,现在添加了问题。谢谢 – Aadam

+0

你还有一个对hibernate.cfg.xml的引用。它存在吗? – WeMakeSoftware

回答

0

外键的Java类型应该是一样的,你要引用的类型。

0

您应该总是使用Long值生成的数字ID。 更改您的范畴类,如下所示:

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

    [...] 
} 

BTW:这罕见的使用了Java类型(如果你没有通过JNI来访问传统的C接口的实例)。