• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unable to create many-to-one mapping

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear friends,
I am using JDBC to create two tables in MYSQL and then insert data into these two tables. The tables are named as Publisher and Book. For each row in table Publisher there could be multiple rows in table Book.

The code looks like the folowing:

PreparedStatement pstmt1 = null;
PreparedStatement pstmt2 = null;
PreparedStatement pstmt3 = null;
PreparedStatement pstmt4 = null;

try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(Exception e){
System.out.println(e.getMessage());
}
try{
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/hibernatetutorial?" +
"user=root&password=root");

pstmt1 = conn.prepareStatement("CREATE Table Publisher(ID bigint(20) primary key,CODE varchar(4),PUBLISHER_NAME varchar(100),ADDRESS varchar(200))");
boolean check1 = pstmt1.execute();

pstmt2 = conn.prepareStatement("CREATE Table Book(ID bigint(20),ISBN varchar(50),BOOK_NAME varchar(100),PUBLISH_DATE date,PRICE int(11),PUBLISHER_ID bigint(20),foreign key(PUBLISHER_ID) REFERENCES Publisher(ID))");
boolean check2 = pstmt2.execute();

pstmt3 = conn.prepareStatement("INSERT INTO Publisher values(?,?,?,?)");
pstmt3.setLong(1,(long)1);
pstmt3.setString(2,"adfs");
pstmt3.setString(3,"zfkljfj dalldl");
pstmt3.setString(4,"100 skhdkhke,sojol,lskjs");
boolean check3 = pstmt3.execute();

pstmt4 = conn.prepareStatement("INSERT INTO Book values(?,?,?,?,?,?)");

Date date = Date.valueOf("03-10-2008");
pstmt4.setLong(1,(long)1);
pstmt4.setString(2,"abcdef");
pstmt4.setString(3,"xyz");
pstmt4.setDate(4,date);
pstmt4.setInt(5,100);
pstmt4.setLong(6,(long)5);
boolean check4 = pstmt4.execute();


When i execute this program,however, i am getting the following message:

Duplicate key or integrity constraint violation message from server: "Cannot add or update a child row: a foreign key constraint fails (`hibernatetutorial/book`, CONSTRAINT `book_ibfk_1` FOREIGN KEY (`PUBLISHER_ID`) REFERENCES `publisher` (`ID`))"

What could have gone wrong ? Kindly help.

Thanks,
Subhash
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PUBLISHER_ID in Book is a foreign key to ID of Publisher.
Any record you are inserting in Book should have corresponding record in Publisher.

In your case:-
publisher_id value is 5 but there is no record in Publisher table with ID having value 5.

I think this is the reason for this error.
 
subhashchandra medhiassam
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, thanks Bittoo. I changed 5 to 1 and it's working now.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bittoo doesn't sound like your genuine name, its more like a nick or fictitious name.
 
grapes are vegan food pellets. Eat this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic