-->
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: Unidirectional @OneToOne W/OChanging the Existing Schema
PostPosted: Sun Aug 16, 2009 8:08 am 
Newbie

Joined: Mon Dec 11, 2006 8:28 am
Posts: 13
I have an existing table/class for which I occasionally have extra information. I'd like to put the extra information into a separate table/class and associate it with the original class, without making any changes to the schema of the original table. Something like this:

Code:
class Char {
   @Id
   private int ID;
   private String font;
   private String size;

   @OneToOne
   private Style style;
}

class Style{
   private boolean bold;
   private boolean italic;
   private boolean underlined'
}
What I'd like to see in the schema is:

Code:
Table                    Create Table                                 
-----------------------  ---------------------------------------------
Char         CREATE TABLE `Char` (     
            `id` bigint(20) NOT NULL auto_increment,   
            `font` varchar(255),         
            `size` varchar(255),     
            PRIMARY KEY  (`id`)                       
                         ) ENGINE=InnoDB   

Style         CREATE TABLE `Style` (                                                                     
            `id` bigint(20) NOT NULL,   
            `bold` tinyint(1),                                                       
            `italic` tinyint(1),                                                                 
            `underlined` tinyint(1),                                                               
            PRIMARY KEY  (`id`)                       
            CONSTRAINT `FK89B773C384085057` FOREIGN KEY (`id`) REFERENCES `Char` (`id`) 
            ) ENGINE=InnoDB                                                         

It seems clear to me that this can work in terms of the SQL, and it's advantageous to me to not have to change the schema of the existing table (there's also a savings if only a small percentage of Chars have Styles).
I've been looking through many, many examples of unidirectional on-to-one mappings and can't see how to do this, or even whether it can be done.

Thanks in advance for any replies.

Michael

_________________
Michael Berkowitz


Top
 Profile  
 
 Post subject: Re: Unidirectional @OneToOne W/OChanging the Existing Schema
PostPosted: Mon Aug 17, 2009 6:04 am 
Beginner
Beginner

Joined: Fri Jun 26, 2009 6:59 am
Posts: 23
i ran into a similar problem with a unidirectional one-to-one, unfortunately. unlike the one-to-many relationship. you cannot simply used a mappedBy, or JoinColumn on the target table for a one-to-one relationship. It seems to be a severe limitation to me.

My solution was to add a FK to my source table to link it to my target with a single joincolumn annotation. However, if you truly CANNOT make this change. i would suggest changing your mapping from one-to-one to one-to-many and simply retrieve the Object at index 0 from the set, this ensures that the mapping is from target to source, and requires no changes to your source schema. It isn't an ideal situation, since you're not accurately representing the relationship, but it's the only one i've come across for now.

Hope this helps.


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.