
I am facing following problem, I have created 2 tables (student and address) with foreign key in address table. I am using hibernate annotations to insert records into these tables. But it is trying to insert records into 'student_tbl_address_tbl' which is not created in database.
Following ar pojo classes
- Student.java
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="student_tbl")
public class Student implements Serializable {
/**
*
*/
private static final long serialVersionUID = 8876327743522818732L;
@Id
private long sid;
@Column(name="sname")
private String sname;
@Column(name="age")
private double age;
@OneToMany(cascade=CascadeType.ALL)
private Set<Address> addresses=new HashSet<Address>(0);
@OneToMany(mappedBy="student",cascade=CascadeType.ALL)
public Set<Address> getAddresses() {
return addresses;
}
public void setAddresses(Set<Address> addresses) {
this.addresses = addresses;
}
public long getSid() {
return sid;
}
public void setSid(long sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public double getAge() {
return age;
}
public void setAge(double age) {
this.age = age;
}
}
- Address.java
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="address_tbl")
public class Address implements Serializable{
/**
*
*/
private static final long serialVersionUID = 6583374367093492573L;
@Id
long adno;
@Column(name="street")
String street;
@Column(name="city")
String city;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="sid")
Student student;
public long getadno() {
return adno;
}
public void setadno(long adno) {
this.adno = adno;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
I have created following class to store the records inside the database
- Write.java
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Write {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Session session=HibernateSessionFactory.getSession();
Transaction tx=session.beginTransaction();
Student s=new Student();
s.setAge(25d);
s.setSid(1l);
s.setSname("suresh");
Set<Address> addressSet=new HashSet<Address>();
Address a=new Address();
a.setadno(1l);
a.setCity("hyd");
a.setStreet("kukatpally");
a.setStudent(s);
addressSet.add(a);
a=new Address();
a.setadno(2l);
a.setCity("sec");
a.setStreet("ameerpet");
a.setStudent(s);
addressSet.add(a);
s.setAddresses(addressSet);
session.save(s);
tx.commit();
session.close();
}
}
following is the exception i got,
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select address_.adno, address_.city as city1_, address_.street as street1_, address_.sid as sid1_ from address_tbl address_ where address_.adno=?
Hibernate: select address_.adno, address_.city as city1_, address_.street as street1_, address_.sid as sid1_ from address_tbl address_ where address_.adno=?
Hibernate: insert into student_tbl (age, sname, sid) values (?, ?, ?)
Hibernate: insert into address_tbl (city, street, sid, adno) values (?, ?, ?, ?)
Hibernate: insert into address_tbl (city, street, sid, adno) values (?, ?, ?, ?)
Hibernate: insert into student_tbl_address_tbl (student_tbl_sid, addresses_adno) values (?, ?)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert collection: [com.tbss.Student.addresses#1]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1205)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at com.tbss.Write.main(Write.java:41)
Caused by: java.sql.SQLException: Invalid object name 'student_tbl_address_tbl'.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:368)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2820)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2258)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:632)
at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:584)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:546)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:504)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:46)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1168)
... 10 more
Please send the answer to "Anugandula.Suresh@Tata-bss.com"
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.