Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6136911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:39:43+00:00 2026-05-23T17:39:43+00:00

Please help me with this hibernate problem, I’m new to hibernate and still trying

  • 0

Please help me with this hibernate problem, I’m new to hibernate and still trying to get my head around it. I can’t seem to work this issue out. I imagine I’m missing something pretty simple.

I’ve followed the example here to achieve a many-to-one mapping, as my requirements are almost identical: http://www.coderanch.com/t/217519/ORM/java/Hibernate-Newbie-Many-Relation-Tutorial

Please note that when I try to persist the Picture object, the user variable is (at that point in time) empty, as is every other variable bar image.

Also note that I’ve set hibernate to generate the database schema by itself via config in the hibernate config file.

Here are my mapping files (declarations removed)

User.hbm.xml

<class name="msc.model.User" table="USER">
    <id name="id" column="USER_ID">
        <generator class="native"/>
    </id>
    <property name="username"/>
    <property name="email"/>

    <bag name="pictures"  
       table="PICTURE"  
       lazy="true"  
       cascade="save-update">  
        <key column="PICTURE_ID"/>  
        <one-to-many class="msc.model.Picture" />  
    </bag>

</class>

And Picture.hbm.xml

<class name="msc.model.Picture" table="PICTURE">
    <id name="id" column="PICTURE_ID">
        <generator class="native"/>
    </id>
    <property name="story"/>
    <property name="tattooist"/>
    <property name="pic"/>

    <many-to-one name="user"  
           class="msc.model.User"  
           column="USER" />     

    <property name="image" type="blob">
        <column name="IMAGE" not-null="true" />
    </property>    

</class>

The class files (getters and setters stripped)

Picture.java

package msc.model;

import java.io.File;
import java.sql.Blob;

public class Picture {

private Long id = null;
private User user = null;
private File pic = null;
private String story = null;
private String tattooist = null;
private Blob image = null;
}

User.java

package msc.model;

import java.util.ArrayList;
import java.util.List;

public class User {

private Long id = null;
private String username = null;
private String email = null;
private List<Picture> pictures = null;
}

The persistence code (note that bFile is byte stream created from a file):

    Session hib_ses = HibernateUtil.getSessionFactory().getCurrentSession();

    hib_ses.beginTransaction();
    Picture picture = new Picture();
    picture.setImage(Hibernate.createBlob(bFile));

    Long id = (Long) hib_ses.save(picture);

    hib_ses.getTransaction().commit();

Here is the exception:

Cannot add or update a child row: a foreign key constraint fails (`msc`.`picture`, CONSTRAINT `FK85BE8DE2885129D` FOREIGN KEY (`PICTURE_ID`) REFERENCES `user` (`USER_ID`))

Please help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:39:43+00:00Added an answer on May 23, 2026 at 5:39 pm

    There is something very strange going on if that is the real error you get.

    Cannot add or update a child row: a foreign key constraint fails (`msc`.`picture`, CONSTRAINT `FK85BE8DE2885129D` FOREIGN KEY (`PICTURE_ID`) REFERENCES `user` (`USER_ID`))
    

    This says that PICTURE.PICTURE_ID is a reference to USER.USER_ID. But PICTURE_ID is the PK of picture, which Hibernate will generate upon insertion. Did you mean to create a constraint from PICTURE.USER to USER.USER_ID?

    Oh, I see you wrote you generate the schema via Hibernate. I think the error is in your “bag” definition. The key column should not be PICTURE_ID, but USER.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hope someone can help with this please. I am trying to query an
Can you please help me to map this class using Hibernate? public class MyClass{
I am new to hibernate. Please help me in understanding this. I have Department
Please help this would be my last problem in dealing with access database with
Please help me solve this problem with capybara I have a button like this
Could you please help me converting this c++ code into python: I am trying
can you please help me with this issue the String class does not have
I am new to Spring and hibernate please help, I am using (Jboss 6.0
My infuriating problem of the day is this: I'm trying to use Hibernate to
Please help this beginner here... I have a SQL Server 2008 R2 running on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.