facebook

Hibernate one to many Example using MyEcilpse?

  1. MyEclipse IDE
  2.  > 
  3. General Development
Viewing 6 posts - 16 through 21 (of 21 total)
  • Author
    Posts
  • #284683

    Riyad Kalla
    Member

    Mork,
    Nipun figured this out. He sat down and created a local example using the MyEclipse Derby database, and it seems you need to add the:

    
    cascade="all"
    

    property to the one-to-many property so when you persist the one (User) to will cascade and persist the many (Visits). It ends up looking something like this in our example:

    
    <set name="employees" inverse="true" cascade="all">
        <key>
            <column name="OFFICECODE" length="10" />
        </key>
        <one-to-many  class="com.hibernate.derby.Employee" />
    </set>
    
    #284839

    Mork
    Participant

    I didn’t see these latest replies until I saw your reply on the other thread.

    I added the cascade=all, but now I get a nullable exception.

    Updated section of User’s mapping file is now:

    <set name=”uservisithistories” inverse=”true” >
    <key>
    <column name=”usr_pk” not-null=”true” cascade=”all”>
    <comment>foreign key back to users table.</comment>
    </column>
    </key>
    <one-to-many class=”TestProject.Uservisithistory” />
    </set>

    The error, after adding the cascade=”all” was:

    Entity retrieval failed.
    org.hibernate.PropertyValueException: not-null property references a null or transient value: TestProject.Uservisithistory.users

    (if I remove the cascade = all, then the many side doesn’t get populated but the one side does again…)

    I’m not sure what this error is complaining about unless I’m not populating something correctly in the Set before I added it.

    Do I need to do the work to add the PK value that was just added on the one side and add it to the Set so that it will be used as the FK on the many side? I figured this part would be automatic.

    // add new visit
    Uservisithistory uvh = new Uservisithistory();
    uvh.setUvhDateTimeOfVisit(dateToStore);
    uvh.setUvhPageFrom(“home page”);
    HashSet<Uservisithistory> set = new HashSet <Uservisithistory>();
    set.add(uvh);

    newUser.setUservisithistories(set);

    Between using both MyEclipse, which didn’t work right out of the box on this, and Hibernate with no clear examples from the two books I’ve used, it’s unclear what’s wrong.

    This has got to be close now, right?

    Thanks again.

    M

    <many-to-one name=”users” class=”TestProject.Users” fetch=”select”>
    <column name=”usr_pk” not-null=”true”>
    <comment>foreign key back to htusers table.</comment>
    </column>
    </many-to-one>

    #284867

    Riyad Kalla
    Member

    Mork,
    I feel your pain learning Hibernate, there are intracacies like this that go along with learning any persistence framework.

    I created an example project for you that does exactly what you are trying to do, but does it using the MyEclipse Derby CLASSICCARS schema. I hope you find it helpful.

    Attachments:
    You must be logged in to view attached files.
    #284871

    Mork
    Participant

    Hello R,

    Thanks very much for all your help.

    I looked around in the various threads, but didn’t see your example. It may be my firewall settings. Where did you post your example? I’m looking forward to seeing it. 🙂

    I’m assuming this example used auto PK generation like MySQL, right? If so, it should be very close to what I’m trying to learn.

    Thanks again in advance.

    M

    #284901

    Riyad Kalla
    Member

    I looked around in the various threads, but didn’t see your example. It may be my firewall settings. Where did you post your example? I’m looking forward to seeing it. 🙂

    Woops, it’s attached to my post above but you need to be logged into the forum to see it.

    I’m assuming this example used auto PK generation like MySQL, right? If so, it should be very close to what I’m trying to learn.

    Oh no it doesn’t, I manually assign the PK for the sake of the example BUT that’s easy enough to fix, you should just change the generator to “native” I believe and then the autoincrement value for MySQL will take precendence.

    And yes I believe when you do the persistence operation, Hibernate will auto-populate that field with the assigned value from the DB.

    #284904

    Mork
    Participant

    Thanks, but…

    I don’t want to generate PKs manually, that’s what the database is for.

    I believe the problem, based on the errors posted, centers around the auto PK generation and it’s use as the FK in the many side.

    These kinds of difficult-to-track-down problems are why frameworks are double edged (appealing until you get stuck, that is).

    Setting Hibernate aside for now….(no more time to spend on this, unfortunately).

    Thanks again.

    M

Viewing 6 posts - 16 through 21 (of 21 total)
Reply To: Hibernate one to many Example using MyEcilpse?

You must be logged in to post in the forum log in