Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!

Rav Nomula

Greenhorn
+ Follow
since Jun 29, 2013
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Rav Nomula

I have a Student table with an auto generated id as primary key and one to many mapping to Phone table.

My Phone table has a composite key PhonePK with phone number and the foreign key id to the Student table as primary keys.

If I do phonePk.setStudent it works but I really don't want to do this. How can I achieve this? If its not achievable, I would like to settle with setting the student id but not the whole student object. How do I do this? Would really appreciate if someone can give an insight on this.

Student.java



Phone.java


PhonePK.java



Main.java


hibernate.cfg.xml

Hi Asif,

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


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?
Anybody?? Is my schema wrong? Please advice!!
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.





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