I'm using Hibernate 3.2.7.ga and I'm wondering if the following is possible.
Let's say there are 3 objects: Person, Town and Mayor.
When registering a Person in the application, you're asked to enter his or her town of residence. This field is saved as a String.
In another part of the application, you can register a Town (with it's name, number of inhabitants, etc.). After the Town is registered, you can add a Mayor to this town.
However, it is not desired to create a Town object for every town that is entered with a Client. An object is only desired when a Mayor needs to be registered.
I'm wondering if, just bij using the String field in Person, you can get the Mayor of the Town the Person lives in.
I'm thinking along these lines of code:
public class Client.java
{
@Id
public Long id;
@Column(nullable = true, ??? )
public String townOfResidence;
public Mayor getMayor()
{
...
}
}
public class Town.java
{
@Id
public Long id;
@Column(nullable = false, unique = true)
public String name;
@OneToOne
public Mayor mayor;
}
I just tried thinking along the following lines:
@Transient
private Town townObject;
@ManyToOne(fetch = FetchType.LAZY, cascade = javax.persistence.CascadeType.ALL, targetEntity = Town.class, optional = true)
@JoinColumn(referencedColumnName = "town")
public Town getTown()
{
return townObject;
}