Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate not inserting rows

 
Rav Nomula
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Anybody?? Is my schema wrong? Please advice!!
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Hi Asif,

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


 
Ranchhor Das
Greenhorn
Posts: 2
Java Linux Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rav,

I am also facing the issue.

How did you resolve it?

Thanks in Advance!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic