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