-->
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.  [ 2 posts ] 
Author Message
 Post subject: Mapping bags when both tables have primary id columns of ID
PostPosted: Thu May 03, 2007 12:39 pm 
Newbie

Joined: Mon Apr 09, 2007 10:46 am
Posts: 5
Hibernate version: 3.2

Mapping documents:
Code:
<hibernate-mapping>
    <class
        name="audit.History"
        table="HISTORY"
        lazy="false"
    >
        <id
            name="id"
            column="ID"
            type="long"
            unsaved-value="0"
        >
            <generator class="native">
                <param name="sequence">HISTORY_SEQ</param>
            </generator>
        </id>
        <bag
            name="propertyChangeList"
            lazy="false"
            cascade="save-update">
            <key>
                <column name="HistoryId"/>
            </key>
            <one-to-many class="audit.PropertyChange" />
      </bag>
      ...
    </class>
</hibernate-mapping>

Code:
<hibernate-mapping>
    <class
        name="audit.PropertyChange"
        table="PROPERTY_CHANGE"
        lazy="false"
    >
        <id
            name="id"
            column="ID"
            type="long"
            unsaved-value="0"
        >
            <generator class="native">
                <param name="sequence">PROP_CHNG_SEQ</param>
            </generator>
        </id>
        ...
    </class>
</hibernate-mapping>


I have two mapped classes, History and PropertyChange.
For each class, the primary ID field is "id"
I want to map a one to many relationship between these two classes (one History has many PropertyChange classes).

The problem I run into is that I cannot seem to tell in the Hibernate mapping file that the ID field from History should be mapped into the HistoryId field of PropertyChange.

Should I just rename the primary ID column for History to be HistoryId and be done (per all the examples I've seen, btw, I don't control the database so there is effort involved in getting changes implemented)

-or-

is there a way to specify this column mapping relationship in the Hibernate file?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 1:37 pm 
Newbie

Joined: Mon Apr 09, 2007 10:46 am
Posts: 5
Additional Information:
Unit test code
Code:
History hist = creatHistoryObject();
HibernateUtil.saveObject( hist );

Session session = HibernateUtil.getCurrentSession().openSession();
History hist2 = (History)session.createQuery( "from History where id = " + hist.getId() ).uniqueResult();

String fieldName = Long.toString( rand.nextLong() );
String oldValue  = Long.toString( rand.nextLong() );
String newValue  = Long.toString( rand.nextLong() );

hist2.addPropertyChange( fieldName, oldValue, newValue );

HibernateUtil.updateObject( hist2 );


When running the above test, the last debug statement from Hibernate is:
Code:
Hibernate:
    insert
    into
        PROPERTY_CHANGE
        (FIELD_NAME, OLD_VALUE, NEW_VALUE, ID)
    values
        (?, ?, ?, ?)

Then the errors start
Code:
May 3, 2007 1:17:46 PM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1400, SQLState: 23000
May 3, 2007 1:17:46 PM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: ORA-01400: cannot insert NULL into ("PROPERTY_CHANGE"."HISTORYID")

Database: Oracle 10.2g

Creation SQL
Code:
CREATE TABLE HISTORY(
        ID               NUMBER         NOT NULL,
        HISTORY_TYPE     VARCHAR2(24)   NOT NULL
);

ALTER TABLE HISTORY ADD CONSTRAINT PK_HISTORY PRIMARY KEY(ID);

CREATE TABLE PROPERTY_CHANGE(
        ID              NUMBER           NOT NULL,
        HISTORYID       NUMBER           NOT NULL,
        FIELD_NAME      VARCHAR2(30)     NOT NULL,
        OLD_VALUE       VARCHAR2(4000),               
        NEW_VALUE       VARCHAR2(4000)                       
);

ALTER TABLE PROPERTY_CHANGE ADD CONSTRAINT PK_PROP_CHNG PRIMARY KEY(ID);
ALTER TABLE PROPERTY_CHANGE ADD CONSTRAINT FK_PC_AUD FOREIGN KEY(HISTORYID) REFERENCES HISTORY(ID);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.