-->
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.  [ 1 post ] 
Author Message
 Post subject: Persistence order of OneToOne foreign key-based relationship
PostPosted: Mon Jan 23, 2006 11:03 am 
Newbie

Joined: Thu May 26, 2005 11:46 am
Posts: 4
Location: London UK
Hibernate version: 3.1, annotations 3.1 beta 7

Hi

I am modelling a legacy Oracle 9 database using Hibernate 3.1 with annotations (3.1 beta 7) and I have come across an interesting problem.

I have two tables linked by a foreign key. Here's a simplified diagram:

Code:
+------------------------+
| MESSAGE                |
+------------------------+
| (PK) id: integer       |
| (FK) hash: varchar(40) |
+------------------------+

+------------------------+
| MESSAGE_BODY           |
+------------------------+
| (PK) id: varchar(40)   |
| data: varchar(blob)    |
+------------------------+


In addition there is a referential integrity constraint on the MESSAGE_BODY.ID column which states that the owner of the hash is the MESSAGE.HASH column, thus nothing can be inserted into the MESSAGE_BODY table without the hash already existing in MESSAGE.

The relevant methods in the corresponding POJOs are as follows.

Message.java:

Code:
   
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "HASH")
    public MessageBody getMessageBody() {
        return this.messageBody;
    }


MessageBody.java:

Code:
   
    @Id(generate = GeneratorType.NONE)
    @OneToOne(mappedBy = "messageBody")
    @Column(name = "ID", length = 40)
    public String getHash() {
        return hash;
    }


I am using the GenericHibernateDao persistence method.

When I attempt to persist a Message object, I get an "integrity constraint violated - parent key not found" error, because Hibernate attempts to persist the MessageBody object first.

I have two questions. 1) Is this behaviour to be expected?, and 2) Is there anything I can do to configure Hibernate such that the Message object is persisted before the MessageBody object?

Whilst this is probably an unusual database set up, I am unfortunately stuck with it for legacy reasons. I believe I could work around the issue by overriding makePersistent() in my DAO and writing specific INSERT queries to allow Message to be persisted first, but this does seem to be a rather desperate hack.

Thanks in advance.


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

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.