This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Hibernate not inserting rows

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I able to generate the schema but when I try to insert, I am getting the error com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'id' cannot be null. My Phone table primary key is a composite key consisting of phoneNumber and foreign key id.

I have my classes as below

Student.java



Phone.java



PhonePK.java



hibernate.cfg.xml



Main.java



Console output

 
Rav Nomula
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way I have the schema, I have id column in Phone table as both primary key and foreign key to Student table and its failing while trying to perform an insert. How can I get this to work? Below is from logs.





 
Rav Nomula
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anybody?? Is my schema wrong? Please advice!!
 
Ranch Hand
Posts: 439
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings Rav,

I just took a glance at all that code, I didnt go in the details but according to your hibernate logs ,

Hibernate: create table Phone (phoneNumber varchar(255) not null, color varchar(255), id integer not null, primary key (phoneNumber, id))



The table Phone is created with coloumn 'id' as not null , however when inserting a new record in the this table you are doing it like


Hibernate will be expecting that you specify an appropriate value for couloumn 'id' as well otherwise it will fail at the binding process like it is as seen in your logs below

5:09:29,367 DEBUG SQL:104 - insert into Phone (color, phoneNumber, id) values (?, ?, ?)
15:09:29,369 TRACE BasicBinder:84 - binding parameter [1] as [VARCHAR] - Black
15:09:29,370 TRACE BasicBinder:84 - binding parameter [2] as [VARCHAR] - 1111111111
15:09:29,371 TRACE BasicBinder:72 - binding parameter [3] as [INTEGER] - <null>
15:09:29,376 WARN SqlExceptionHelper:145 - SQL Error: 1048, SQLState: 23000
15:09:29,377 ERROR SqlExceptionHelper:147 - Column 'id' cannot be null
15:09:29,393 INFO AbstractBatchImpl:195 - HHH000010: On release of batch it still contained JDBC statements


 
Rav Nomula
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Asif,

Thanks for your reply but shouldn't id column value of Phone table be auto inserted from the student table id value? Its a foreign key to the student table too.

Hibernate: alter table Phone add index FK_aoj0eivd0ap3drxnoyk4xj10q (id), add constraint FK_aoj0eivd0ap3drxnoyk4xj10q foreign key (id) references Student (id)

I want all the inserts to happen from the student side and not from both sides. How do I make this happen?
 
Saif Asif
Ranch Hand
Posts: 439
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this take a look at it , it might be useful. Also , Why not use cascading for that matter ? Hibernate Cascading for saveOrUpdateOrphan
 
Rav Nomula
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Asif,

In my student.java class I am already doing the below


 
Greenhorn
Posts: 3
Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rav,

I am also facing the issue.

How did you resolve it?

Thanks in Advance!!!
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic