-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: i'm almost done but this elt column just appeared out of no
PostPosted: Sun Jan 27, 2008 11:55 pm 
Newbie

Joined: Sun Jan 27, 2008 11:45 pm
Posts: 3
Hi

I am trying to implement a many-to-many association.
I have a simple scenario where I have two entities three tables.
one is Teacher.java with teacher table containg id and name.
the other is Student.java with student table containing id and name as well.

let me show you my java files, my tables and the hibernate mapping files.


Code:
package com.oxman.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;


public class Teacher implements Serializable{
private int id;
private String name;
private Set<Student> students;

public Teacher(){

}

public Teacher(String name){
setName(name);
}

public Teacher(String name, Student student){
this(name);
addStudent(student);
}

public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addStudent(Student student) {
if (students == null) {
students = new HashSet();
}
students.add(student);
}
}


Code:
package com.oxman.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class Student implements Serializable{
private int id;
private String name;
private Set<Teacher> teachers;

public Student(){

}

public Student(String name){
setName(name);
}

public Student(String name, Teacher teacher){
this(name);
addTeacher(teacher);
}

public Set<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(Set<Teacher> teachers) {
this.teachers = teachers;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public void addTeacher(Teacher teacher) {
if (teachers == null) {
teachers = new HashSet();
}
teachers.add(teacher);
}
}


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.oxman.entity.Teacher" table="teachers">
<id name="id" column="uid" type="int" unsaved-value="null">
<generator class="native"/>
</id>
<property name="name" type="string" length="255"/>
<set name="students" table="teachers_students" cascade="all">
<key column="teacher_id"/>
<many-to-many class="com.oxman.entity.Student"/>
</set>
</class>
</hibernate-mapping>


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.oxman.entity.Student" table="students">
<id name="id" column="uid" type="int" unsaved-value="null">
<generator class="native"/>
</id>
<property name="name" type="string" length="255"/>
<set name="teachers" table="teachers_students" cascade="all">
<key column="student_id"/>
<many-to-many class="com.oxman.entity.Teacher"/>
</set>
</class>
</hibernate-mapping>


DROP TABLE IF EXISTS `teachers`;
CREATE TABLE `teachers` (
`uid` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `students`;
CREATE TABLE `students` (
`uid` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `teachers_students`;
CREATE TABLE `teachers_students` (
`teacher_id` int(11) NOT NULL default '0',
`student_id` int(11) NOT NULL default '0',
PRIMARY KEY (`teacher_id`,`student_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

I have tried to run this code

Code:
package com.oxman.test;

public class StudentTeacherTest {   
   public static void main(String[] args){            SessionFactory factory = HibernateFactory.buildSessionFactory();   Session session = HibernateFactory.openSession();      Transaction tx= session.beginTransaction();            try{                         Student student = new Student("Sanetti");         session.save(student);                  Teacher teacher = new Teacher("Preecha");                    session.save(teacher);                     teacher.addStudent(student);                    session.saveOrUpdate(teacher);                     tx.commit();           session.close();      }
catch(StaleStateException SSExc){                     SSExc.printStackTrace();      }
catch(Exception Exc){         
                           Exc.printStackTrace();      }
finally{          if(session != null){             try{                HibernateFactory.close(session);                HibernateFactory.closeFactory();             }catch(HibernateException ignore){                //igonre                             }          }       }   }}


the record teacher with id = 6 and the record student with id = 1 do already existed in the database.

here is the stack trace
Hibernate: insert into students (name) values (?)
Hibernate: insert into teachers (name) values (?)
Hibernate: insert into teachers_students (teacher_id, elt) values (?, ?)
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:145)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.oxman.test.StudentTeacherTest.main(StudentTeacherTest.java:34)
Caused by: java.sql.BatchUpdateException: Unknown column 'elt' in 'field list'
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1669)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1085)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 8 more


How did elt field come up, I have no idea.

so if anybody knows what went wrong, please share it with me
thank you so much

Sura


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.