-->
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: Unidirectional OneToOne relationship query
PostPosted: Wed Feb 20, 2008 1:41 am 
Newbie

Joined: Thu Feb 07, 2008 6:54 am
Posts: 1
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


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.