2013-05-29 3 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 
} 

весной 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: выполнить: 203) изменять табличные классы добавить индекс FK9619D00659EAC034 (cat_id), добавить ограничение FK9619D00659EAC034 внешний ключ (cat_id) ссылки Категория (ID)

[ERROR] 33:01 (SchemaUpdate.java:execute:212) Неудачно: изменить таблица Классы добавить индекс FK9619D00659EAC034 (cat_id), добавить ограничение FK9619D00659EAC034 внешний ключ (cat_id) ссылки Категория (ID)

[ERROR] 33:01 (SchemaUpdate.java:execute:213) Не удается создать таблицу 'туманность # SQL-83c_e3' (ошибка: 150)

[DEBUG] 33:01 (SchemaUpdate.java:execute:203) изменить таблицу экзамен добавить индекс 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) Не удается создать таблицу 'туманность # sql-83c_e3' (errno: 150)

Любая помощь приветствуется.

Благодаря & уважение

+0

, пожалуйста, напишите hibernate.cfg.xml – WeMakeSoftware

+0

. Я настроил его в spring.xml и добавил к этому вопросу. спасибо – Aadam

+0

у вас также есть ссылка на hibernate.cfg.xml. Он существует? – WeMakeSoftware

ответ

0

Тип Java внешнего ключа должен быть таким же, как тип вы ссылаетесь к.

0

Вы должны всегда Длительные значения для сгенерированных числовых идентификаторов. Измените класс Категории следующим образом:

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

    [...] 
} 

BTW: Это редкость, чтобы использовать тип Java короткого (если у вас нет доступа к интерфейсам устаревшихов С помощью JNI, например).

Смежные вопросы