Hello,
I am trying to create a simple OneToOne relationship but only want to define the relationship in the owner object.
I have three classes:
@Entity
@Table(name="user")
public class UserBean extends BaseEntity {
@ManyToOne
@JoinColumn(name="client_id")
private ClientBean client;
@Id @GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="user_id")
private int id;
@Column (name="name")
private String name;
@Column (name="password")
private String password;
...
}
@Entity
@Table (name="client")
public class ClientBean extends BaseEntity {
@Column (name="name")
private String name;
@Id @GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="client_id")
private int id;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "client")
@Sort(type = SortType.NATURAL)
private SortedSet<UserBean> users;
...
}
These classes work fine, and the tables are setup with the correct foreign key created in the "user" table.
Now I have a another class called OrderBean in which contains a reference to the user which created the order.
@Entity
@Table(name="order")
public class OrderBean extends BaseEntity {
@OneToOne(cascade=CascadeType.REFRESH)
@PrimaryKeyJoinColumn
UserBean user;
@Column(name="client_ref")
private String clientRef;
@Column(name="create_time")
private Date createTime;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="order_id")
int id;
...
}
Want I want to do is setup a unidirectional OneToOne between OrderBean and UserBean, i.e. I don't want any references to OrderBean in UserBean. So essentially I want to add an extra column to the "order" table called "user_id" and set this up as a foreign key to to the "user" table. Then I want to be able to retrieve OrderBean objects with the UserBean such as:
em.findById(OrderBean.class, id);
and not worry about having to retrieve the UserBean object separately.
Any help would be much appreciated.
Hibernate version:
Hibernate 3.2.4.sp1
Name and version of the database you are using:
MySQL 4.1
|