-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Hibernate binding NULL as foreign key
PostPosted: Thu Sep 11, 2003 7:59 am 
Newbie

Joined: Thu Sep 11, 2003 7:17 am
Posts: 2
I'm just starting out with Hibernate, however, I've come across a problem relating to persisting my MailboxesEntity class to the database. I'm currently using hibernate-2.0.3, and MySQL with primary_key as auto_increment.

I have listed the hibernate mappings for both classes and the hibernate DEBUG. It seems that when I create a brand new (Transient) CustomersEntity class (initialising all properties except for the customer_id which is set to NULL) and attempt to persist it to the database, it fails.

From the DEBUG, hibernate seems to be happy storing the CustomerEntity class, it manages to perform a SELECT LAST_INSERT_ID() and retrieve the customer_id (shown in red), however when it stores this ID as a foreign key to the MailboxesEntity class, it is using NULL (show in red).

Why is hibernate not storing the correct customer_id value as a foreign key to the MailboxesEntity class?

Appreciate any help or pointers!!

Code:
<hibernate-mapping>
    <class
        name="uk.co.blueyonder.selfcareSS.model.CustomersEntity"
        table="customers"
        dynamic-update="false"
        dynamic-insert="false"
        >

        <id
            name="customer_id"
            column="customer_id"
            type="integer"
            >
            <generator class="native">
            </generator>
        </id>

        <property
            name="mailboxname"
            type="string"
            update="true"
            insert="true"
            column="mailboxname"
            />

        <property
            name="active"
            type="char"
            update="true"
            insert="true"
            column="active"
            />

        <set
            name="mailboxes"
            table="mailboxes"
            lazy="false"
            inverse="false"
            cascade="all"
            sort="unsorted"
            >

            <key
                column="customer_id"
                />

            <one-to-many
                class="uk.co.blueyonder.selfcareSS.model.MailboxesEntity"
                />
        </set>
       
    </class>

</hibernate-mapping>

<hibernate-mapping>
    <class
        name="uk.co.blueyonder.selfcareSS.model.MailboxesEntity"
        table="mailboxes"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="mailbox_id"
            column="mailbox_id"
            type="integer"
        >
            <generator class="native">
            </generator>
        </id>

        <property
            name="username"
            type="string"
            update="true"
            insert="true"
            column="username"
        />

        <property
            name="customer_id"
            type="integer"
            update="true"
            insert="true"
            column="customer_id"
            not-null="true"
        />

        <set
            name="preferences"
            table="preferences"
            lazy="false"
            inverse="false"
            cascade="all"
            sort="unsorted"
        >

              <key
                  column="mailbox_id"
              />

              <one-to-many
                  class="uk.co.blueyonder.selfcareSS.model.PreferencesEntity"
              />
        </set>

        <set
            name="aliases"
            table="aliases"
            lazy="false"
            inverse="false"
            cascade="all"
            sort="unsorted"
        >

              <key
                  column="mailbox_id"
              />

              <one-to-many
                  class="uk.co.blueyonder.selfcareSS.model.AliasesEntity"
              />
        </set>
       
    </class>

</hibernate-mapping>


    DEBUG JDBCTransaction - begin
    DEBUG Cascades - unsaved-value strategy NULL
    DEBUG SessionImpl - saveOrUpdate() unsaved instance with id: null
    DEBUG SessionImpl - saving [uk.co.blueyonder.selfcareSS.model.CustomersEntity#<null>]
    DEBUG Cascades - processing cascades for: uk.co.blueyonder.selfcareSS.model.CustomersEntity
    DEBUG Cascades - done processing cascades for: uk.co.blueyonder.selfcareSS.model.CustomersEntity
    DEBUG SessionImpl - Wrapped collection in role: uk.co.blueyonder.selfcareSS.model.CustomersEntity.mailboxes
    DEBUG EntityPersister - Inserting entity: uk.co.blueyonder.selfcareSS.model.CustomersEntity (native id)
    DEBUG BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
    DEBUG SessionFactoryImpl - prepared statement get: insert into customers (mailboxname, active) values (?, ?)
    DEBUG SessionFactoryImpl - preparing statement
    DEBUG EntityPersister - Dehydrating entity: uk.co.blueyonder.selfcareSS.model.CustomersEntity#null
    DEBUG StringType - binding 'bnwk01010' to parameter: 1
    DEBUG CharacterType - binding 'Y' to parameter: 2
    DEBUG BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
    DEBUG SessionFactoryImpl - closing statement
    DEBUG BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
    DEBUG SessionFactoryImpl - prepared statement get: SELECT LAST_INSERT_ID()
    DEBUG SessionFactoryImpl - preparing statement
    DEBUG EntityPersister - Natively generated identity: 2
    DEBUG BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
    DEBUG SessionFactoryImpl - closing statement
    DEBUG Cascades - processing cascades for: uk.co.blueyonder.selfcareSS.model.CustomersEntity
    DEBUG Cascades - cascading to collection: uk.co.blueyonder.selfcareSS.model.CustomersEntity.mailboxes
    DEBUG Cascades - cascading to saveOrUpdate()
    DEBUG Cascades - unsaved-value strategy NULL
    DEBUG SessionImpl - saveOrUpdate() unsaved instance with id: null
    DEBUG SessionImpl - saving [uk.co.blueyonder.selfcareSS.model.MailboxesEntity#<null>]
    DEBUG Cascades - processing cascades for: uk.co.blueyonder.selfcareSS.model.MailboxesEntity
    DEBUG Cascades - done processing cascades for: uk.co.blueyonder.selfcareSS.model.MailboxesEntity
    DEBUG SessionImpl - Wrapped collection in role: uk.co.blueyonder.selfcareSS.model.MailboxesEntity.preferences
    DEBUG SessionImpl - Wrapped collection in role: uk.co.blueyonder.selfcareSS.model.MailboxesEntity.aliases
    DEBUG EntityPersister - Inserting entity: uk.co.blueyonder.selfcareSS.model.MailboxesEntity (native id)
    DEBUG BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
    DEBUG SessionFactoryImpl - prepared statement get: insert into mailboxes (username, customer_id) values (?, ?)
    DEBUG SessionFactoryImpl - preparing statement
    DEBUG EntityPersister - Dehydrating entity: uk.co.blueyonder.selfcareSS.model.MailboxesEntity#null
    DEBUG StringType - binding 'bnwk01010_2' to parameter: 1
    DEBUG IntegerType - binding null to parameter: 2
    DEBUG JDBCExceptionReporter - SQL Exception
    java.sql.SQLException: General error: Column 'customer_id' cannot be null
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 10:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
PLease read Parent/Child Relationship page of the Hibernate manual.


Top
 Profile  
 
 Post subject: Null in FKEY
PostPosted: Thu Sep 11, 2003 2:04 pm 
Beginner
Beginner

Joined: Wed Aug 27, 2003 6:44 pm
Posts: 20
Hi,
You need to two things .one is Inverse = true at parent end and Link between child and parent at the child level.(i.e Bidirectional Association)
(for e.g ) in
<many-to-one name="team" class="example.Team" column="team_id"/>

Hope I am right in my assumption.I am not a hibernate expert.

Ashok


Top
 Profile  
 
 Post subject: Many thanks!
PostPosted: Fri Sep 12, 2003 5:26 am 
Newbie

Joined: Thu Sep 11, 2003 7:17 am
Posts: 2
Sound advice from Gavin made me realise that my child entity class requires a link to the parent class via a many-to-one mapping. It even states in the doc that it will violate the not NULL constraint if I didn't!

Many thanks Ashoknan and Gavin!

-Burhan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.