File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes one to many mapping(unidirection) 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 "one to many mapping(unidirection)" Watch "one to many mapping(unidirection)" New topic
Author

one to many mapping(unidirection)

hemantha kumark
Greenhorn

Joined: Jun 05, 2011
Posts: 1

i am not getting where i made mistake, can any one help me out.
i am getting org.hibernate.exception.SQLGrammarException: could not insert:

Parent class




Child class



Test class to run the project



this is what i'm getting output

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
alter table Child drop foreign key FK3E104FCC4758C38
drop table if exists Child
drop table if exists Parent
create table Child (childId integer not null auto_increment, name varchar(255), children_parentId integer, primary key (childId))
create table Parent (parentId integer not null auto_increment, name varchar(255), primary key (parentId))
alter table Child add index FK3E104FCC4758C38 (children_parentId), add constraint FK3E104FCC4758C38 foreign key (children_parentId) references Parent (parentId)
Hibernate: insert into Child (name) values (?)
Hibernate: insert into Child (name) values (?)
Hibernate: insert into Parent (name) values (?)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [com.hibernate.Parent]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2329)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2836)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:705)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:693)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:689)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:344)
at $Proxy8.save(Unknown Source)
at com.hibernate.Test.main(Test.java:52)
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'name' in 'field list'
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1604)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
... 22 more







Waswani Naresh
Ranch Hand

Joined: May 01, 2008
Posts: 66
Hi Hemantha,

I tried executing your code and surprisingly I did not get any error. Could you please furnish what version of hibernate and mysql you are using ?

Regards,
Naresh Waswani


Naresh Waswani
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1


The below line in the StackTrace should be the cause.

Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'name' in 'field list'

Waswani Naresh
Ranch Hand

Joined: May 01, 2008
Posts: 66
@Raj,

The error could be MySQL/Hibernate version specific. I did not get this error on MySQL 5.2 and Hibernate 3.5.1.

Regards,
Naresh Waswani
in Mkumar
Greenhorn

Joined: Sep 08, 2008
Posts: 20
Hey ,

Seems problem with "name" field.

hence the below error you are getting -

Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'name' in 'field list'

Please verify whether field is available in the corresponding table in the database and also ensure the naming.

-Manoj

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: one to many mapping(unidirection)
 
Similar Threads
Problem in <one-to-one> relation mapping
Caused by: java.sql.SQLException: Statement parameter 2 not set.
Hibernate @GeneratedValue problem
java.sql.SQLException: Cannot add or update a child row: a foreign key constraint fails
New to Hibernate.help with org.hibernate.HibernateException: Unable to instantiate default tuplizer