简体   繁体   中英

Beginners Hibernate problem - simple mapping with non-simple exception!

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!

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.

It looks like you are trying to save the Picture before saving the User. Try saving the User first.

[edit]

From the mapping - it looks like there is a Parent/Child relationship between User and Picture.

There is a good example in the Hibernate Documentation for a Parent Child relationship.

If you want the User to be able to be null, then a uni-directional relationship would be better.

[edit]

Another good reference about mapping collections with Hibernate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM