This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Bi-directional mapping problems while creating tables on runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Bi-directional mapping problems while creating tables on runtime " Watch "Bi-directional mapping problems while creating tables on runtime " New topic
Author

Bi-directional mapping problems while creating tables on runtime

nagaraj kris
Greenhorn

Joined: Jan 12, 2007
Posts: 5
Hi,

I have tables with many-many relationship mapping and they are bidirectional.

I am writing junits using spring mock,DBUnit and HSQL. All the tables are created on the fly using hibernate's HBM2DDL option.

when table 'student' is getting created, i am getting exception saying table 'student_course' doesnt exist. I tried to move around the order of tables creation , but nothing is working.

Here are the mapping files

Table student:
===============
<hibernate-mapping package="test">
<class name="Student" table="student" lazy="true" batch-size="20" >
<id name="studentId" column="student_id" type="long" unsaved-value="0">
<generator class="identity"/>
</id>

<property name="name" column="name" />
<set name="courses" table="student_course" cascade="save-update">
<key column="student_id" />
<many-to-many column="course_id" class="Course" />
</set>
</class>
</hibernate-mapping>

Table course:
===============
<hibernate-mapping package="test">
<class name="Course" table="course" lazy="true" batch-size="20" >
<id name="courseId" column="course_id" type="long" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="name" column="name" />

<property name="countOfStudents" lazy="true"
formula="(select count(*) from student_course sc
where sc.course_id = course_id) " />
</class>
</hibernate-mapping>

Table student_course:
==================
<hibernate-mapping package="test">
<class name="StudentCourse" table="student_course" lazy="true" batch-size="20">
<id name="studentCourseId" column="student_course_id" type="long" unsaved-value="0">
<generator class="identity" />
</id>

<many-to-one name="student" column="student_id"/>
<many-to-one name="course" column="course_id" />
</class>
</hibernate-mapping>


If i remove the set in student mapping, then everything is working fine, but that requires changes in my code, can anybody suggest other options.

Thanks,
Nag
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Nag, next time please use the CODE button below the Add Reply button when posting code or XML so that the indentation remains and it is readable.

But I don't see a ManyToMany mapping in your student class. You would have one if you mean to make it bi-directional.

Also, your studentcourse table does not need a mapping. It is a join table, so if you just have the two many to many mappings in just student and course you will generate a join table automatically.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Bi-directional mapping problems while creating tables on runtime
 
Similar Threads
Hibernate - How to do associations with join tables?
Bad cascade
Why a select statement, not an insert ?
Bi-directional mapping problems while creating tables on runtime
java.lang.IllegalArgumentException argument type mismatch