?
Solved

hibernate one-many example issue

Posted on 2010-01-10
10
Medium Priority
?
801 Views
Last Modified: 2013-11-24
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.

0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 7

Author Comment

by:gudii9
ID: 26278640
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
 
LVL 40

Assisted Solution

by:Gurvinder Pal Singh
Gurvinder Pal Singh earned 800 total points
ID: 26280848
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
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 26280874
<<<<<<<<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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 7

Author Comment

by:gudii9
ID: 26285174
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
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 26285387
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
 
LVL 7

Author Comment

by:gudii9
ID: 26285693
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
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 26285782
<<<<<<<<<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
 
LVL 7

Author Comment

by:gudii9
ID: 26286767
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
 
LVL 20

Accepted Solution

by:
Sathish David  Kumar N earned 1200 total points
ID: 26290455
>>>>>>>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
 
LVL 7

Author Closing Comment

by:gudii9
ID: 31675228
thank you very much. I appreciate it
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question