hibernate one-many example issue

Hi,

I am trying hibernate one-many mapping example on mysql database server from link
http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-one-to-many-1.html

when i execute the main.java class getting following error.




log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into STUDENT (STUDENT_NAME) values (?)
Hibernate: insert into PHONE (PHONE_TYPE, PHONE_NUMBER) values (?, ?)
org.hibernate.exception.GenericJDBCException: could not insert: [com.vaannila.student.Phone]
      at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
      at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
      at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
      at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1747)
      at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2149)
      at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:34)
      at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
      at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:238)
      at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:158)
      at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:96)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
      at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:416)
      at org.hibernate.engine.Cascades$5.cascade(Cascades.java:153)
      at org.hibernate.engine.Cascades.cascade(Cascades.java:721)
      at org.hibernate.engine.Cascades.cascadeCollection(Cascades.java:860)
      at org.hibernate.engine.Cascades.cascade(Cascades.java:739)
      at org.hibernate.engine.Cascades.cascade(Cascades.java:817)
      at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:361)
      at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:263)
      at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:158)
      at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
      at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
      at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
      at org.hibernate.impl.SessionImpl.save(SessionImpl.java:429)
      at org.hibernate.impl.SessionImpl.save(SessionImpl.java:424)
      at com.vaannila.student.Main.main(Main.java:25)
Caused by: java.sql.SQLException: General error message from server: "Field 'phone_id' doesn't have a default value"
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
      at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1167)
      at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1278)
      at com.mysql.jdbc.Connection.execSQL(Connection.java:2251)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1772)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1619)
      at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1729)
      ... 28 more



Please advise on how to resolve it. Any ideas, suggestions, resources, sample code highly appreciated. Thanks in advance.

LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gudii9Author Commented:
Hi,

I made phone_id column as auto-increment in phone table and again executed main.java. this time the error message changed to




log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into STUDENT (STUDENT_NAME) values (?)
Hibernate: insert into PHONE (PHONE_TYPE, PHONE_NUMBER) values (?, ?)
Hibernate: insert into PHONE (PHONE_TYPE, PHONE_NUMBER) values (?, ?)
Hibernate: insert into STUDENT_PHONE (STUDENT_ID, PHONE_ID) values (?, ?)
Hibernate: insert into STUDENT_PHONE (STUDENT_ID, PHONE_ID) values (?, ?)
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
      at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
      at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
      at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:161)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
      at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
      at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
      at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669)
      at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:293)
      at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
      at com.vaannila.student.Main.main(Main.java:27)
Caused by: java.sql.BatchUpdateException: Duplicate key or integrity constraint violation message from server: "Duplicate entry '4' for key 'PRIMARY'"
      at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1492)
      at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
      at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:154)
      ... 8 more

Please advise
0
Gurvinder Pal SinghCommented:
Reason for error in original question:
phone_id key is not updated in original VOs added in the Set

Could you please put some more debug logs in the main class as well as post the table schemas

Please see this link for reason of Error in post #26278640
http://markmail.org/message/yqbqs5vnwiautowp

It seems you have put the auto-increment on phone table and not in student-phone and Student table

thanks
0
Sathish David Kumar NArchitectCommented:
<<<<<<<<I made phone_id column as auto-increment in phone table and again executed main.java. this time the error message changed to

ur not changing  phone_id column in other tables as increment

eg:  phone_id column also persent in student table !!

in ur table u have mention may-to-one relationship !!
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

gudii9Author Commented:
Hi,

It worked fine after making phone_id also as primary key within the student_phone table. I wonder why we need this combined table called student_phone and why we need to mention the both the ID columns as primary key columns. Please advise
0
Sathish David Kumar NArchitectCommented:
if we want phone tables deatils with for the user means !!
that time we need any one commone flied  to access  both the table so that only we are mention that permiray key (composite permey key )

for eg :
student and,phone table common flied is ph_no

depend upon the ph_no we can get common values !!  
0
gudii9Author Commented:
hi,
>>>>student and,phone table common flied is ph_no
I do not see phone number in student table.


student table has only 2 columns namely

STUDENT_ID(primary key column)
STUDENT_NAME

where as phone table has 3 columns such as
PHONE_ID(primary key column)
PHONE_TYP
PHONE_NUMBER

Where as student_phone table has 2 columns

student_id(primary key column)
phone_id(primary key column)


Please advise on how can we get common values based on phone_no.
0
Sathish David Kumar NArchitectCommented:
<<<<<<<<<Please advise on how can we get common values based on phone_no

i gave just example as per ur table


Here common filed are student_id,phone_id

here u can put a join and get the values !!

select a.STUDENT_NAME,b.PHONE_NUMBER  from student  a,phone  b,student_phone c
where

aSTUDENT_ID=c.STUDENT_ID and c.phone_id=b.phone_id

this query result will student name and his ph no !!
0
gudii9Author Commented:
Hi,

>>>>that time we need any one commone flied  to access  both the table so that only we are mention that permiray key (composite permey key )

Here common table is student_phone which has the common composite primary key( consisting of student_id, phone_id) which maps and joins the tables right. Please advise
0
Sathish David Kumar NArchitectCommented:
>>>>>>>Here common table is student_phone which has the common composite primary key( consisting of student_id, phone_id) which maps and joins the tables righ

yes ur correct !! as per ur table student_phone is the common for student  ,phone   tables !!
if u want to get some data of phone   tables means u can write the join of this 3 tables and get the value !!
as i mention before !!



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
thank you very much. I appreciate it
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.