-->
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: Two bi-directional one-to-one associations to other class
PostPosted: Thu Jul 17, 2008 7:35 pm 
Newbie

Joined: Thu Jul 17, 2008 6:44 pm
Posts: 9
Hi,

I couldn't find solution for this:

I have a Person, which has two Addresses (say, homeAddress and workAddress). I want to create a bi-directional one-to-one association for each address.

But that leads me to this (which is invalid):

Code:
@Entity public class Address implements Serializable {
  @OneToOne( mappedBy = "homeAddress" )
  @OneToOne( mappedBy = "workAddress" )
  private Person person;
}


I can't use @Embedded, because the schema is defined by someone else and the address' table is separated.

Do I have to introduce another Person property in Address?
But each address can only belong to one Person, so it shouldn't be necessary (in principle). If I did so, I would at least let both setters and getters access the same field as it really is the same value - is that a good approach, or will it cause some problems with data integrity or so?

Code:
private Osoba osoba;
 
  @OneToOne( mappedBy = "adresa" )
  public Osoba getOsoba(){ return this.osoba; }
  public void  setOsoba( Osoba osoba ){ this.osoba = osoba; }
 
  @OneToOne( mappedBy = "adresa_prechodna" )
  public Osoba getOsobaPrechodna(){ return this.osoba; }
  public void  setOsobaPrechodna( Osoba osoba ){ this.osoba = osoba; }

}




I also noticed
5.3. Mapping a class more than once - can I use this to solve my issue? Can I use JPA for this?

Currently I use JPA, but of course I will switch to XML if needed.

The answer may be obvious, but I am strongly used to the "SQL-way of thinking" and can't figure the O/R way.

Thanks for hints.


Hibernate version:
hibernate-3.2
hibernate-annotations-3.3.1.GA
hibernate-entitymanager-3.3.2.GA

Mapping documents:

Code:
@Entity
@Table( name="persons" )
public class Person implements Serializable {

  @Id
  @GeneratedValue( strategy = GenerationType.AUTO )
  private Long id;
  ...
 
  @OneToOne
  @JoinColumn( name="id_adresa", referencedColumnName="id_adresa" )
  private Address adresa;
 
  @OneToOne
  @JoinColumn( name="id_adresa_prechodna", referencedColumnName="id_adresa" )
  private Address adresa_prechodna;


Code:
@Entity
@Table( name="addresses" )
public class Address implements Serializable {

  @OneToOne( mappedBy = "adresa" )
  //@OneToOne( mappedBy = "adresa_prechodna" )
  private Person person;

}


Name and version of the database you are using:
MySQL 5.0.x


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.