-->
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.  [ 5 posts ] 
Author Message
 Post subject: Reliably annotating nullability of ToOne-Associations
PostPosted: Tue Jun 17, 2008 8:23 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:04 am
Posts: 27
Hello forumers,

A few months ago I stumbled over what I consider a bug in the translation of javax.persistence Annotations to the configuration model used by the hibernate core. Briefly put, under very specific circumstances the "optional"-Attribute of a @OneToOne or @ManyToOne is lost in translation, causing Hibernate to proxy the remote end of the association even if no associated object exists.

I reported that. Emanuel Bernard closed the bug, stating that he disagrees with my understanding of the semantics of "optional", and gave a work-around. I pointed out why the work around doesn't work reliably for us, but apparently nobody reads comments on a closed bug report ...

So I ask here: How can I reliably and non-redundantly specify that a @OneToOne should be nullable?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 22, 2008 12:52 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

Have you tried to map your optional one-to-one association via a join table. I believe this is the recommended way in this case. There are two ways of achieving this. Either via with a @JoinTable or with @SecondaryTable.

The former could look like this:
Code:
    @OneToOne(targetEntity = InfoAutoVerrechnungImpl.class, fetch = FetchType.LAZY, optional = true)
    @JoinTable(
    name = "INFO_AUTO_VERRECHNUNG",
    joinColumns = @javax.persistence.JoinColumns({
            @javax.persistence.JoinColumn(name = "MI0153A", referencedColumnName = "MI0153A", nullable = false, updatable = false, insertable = false),
            @javax.persistence.JoinColumn(name = "MI1997A", referencedColumnName = "MI1997A", nullable = false, updatable = false, insertable = false) })
     inverseJoinColumns = @JoinColumn(name = "???")
    public InfoAutoVerrechnung getInfoAutoVerrechnung();


The book "Java Persistence with Hibernate" covers this type of mapping in more detail.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 8:55 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:04 am
Posts: 27
We have to share the database with a legacy application; we therefore can't change the schema. (==> no join table ...)

I don't get how @SecondaryTable would help with an optional association.

I have looked in the book, and didn't find anything applicable. I might have missed something, which chapter are you referring to?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 9:54 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

I am referring to chapter 7.1.3 - the Person and Desk example.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 10:23 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:04 am
Posts: 27
Ah, this is for the join table approach. Our association is only nullable in one direction, so the purist mapping would be a nullable foreign key column (chapter 7.1.2). But that's a moot point, because we can not change our schema from the shared primary key variant (chapter 7.1.1).


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