0
votes

I have three class with onetomany and manytoone annotated. Below

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>

now when i run this project, the tables are created but it fails to alter and add a forign key and giving this error

[DEBUG] 33:01(SchemaUpdate.java:execute:203) alter table Classes add index FK9619D00659EAC034 (cat_id), add constraint FK9619D00659EAC034 foreign key (cat_id) references Category (id)

[ERROR] 33:01(SchemaUpdate.java:execute:212) Unsuccessful: alter table Classes add index FK9619D00659EAC034 (cat_id), add constraint FK9619D00659EAC034 foreign key (cat_id) references Category (id)

[ERROR] 33:01(SchemaUpdate.java:execute:213) Can't create table 'nebula.#sql-83c_e3' (errno: 150)

[DEBUG] 33:01(SchemaUpdate.java:execute:203) alter table Exam add index FK212C3F59EAC034 (cat_id), add constraint FK212C3F59EAC034 foreign key (cat_id) references Category (id)

[ERROR] 33:01(SchemaUpdate.java:execute:212) Unsuccessful: alter table Exam add index FK212C3F59EAC034 (cat_id), add constraint FK212C3F59EAC034 foreign key (cat_id) references Category (id)

[ERROR] 33:01(SchemaUpdate.java:execute:213) Can't create table 'nebula.#sql-83c_e3' (errno: 150)

Any help is appreciated.

Thanks & Regards

2
please post the hibernate.cfg.xmlWeMakeSoftware
i configured it in spring.xml and added now in question. thanksAadam
you also have a reference to hibernate.cfg.xml. Does it exist?WeMakeSoftware
and what DB are you using?WeMakeSoftware
heh, you are right. The foreign key should be of the same type as the key it's referencingWeMakeSoftware

2 Answers

0
votes

The Java type of the foreign key should be the same as the type you're referencing to.

0
votes

You should always use Long values for generated numerical IDs. Change your Category class as follows:

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

  [...]
}

BTW: It uncommon to use the Java type short (if you do not have to access legacy C interfaces via JNI for instance).