wood burning stoves
The moose likes Object Relational Mapping and the fly likes OneToOne mapping problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "OneToOne mapping problem" Watch "OneToOne mapping problem" New topic

OneToOne mapping problem

Roshan Wankhade

Joined: Dec 07, 2011
Posts: 21

Hi Friends,
I'm having problems in maintaining the OneToOne mapping in Hibernate.
though i think i'm doing every thing fine

My problem is -
In my UserDetails table its having the foreign key for the Vehicle Tables vehicleId, but i can enter same vehicleId for two defferent record in the UserDetails table

so how the OneToOne relationship maintains???

i just want one user can have one vehicle and one vehicle will be own by one user
how can i do it.

please help me...
thanks in advance.

My Classes are ---

//UserDetails class
public class UserDetails {
@Id @GeneratedValue(strategy= GenerationType.AUTO)
private int userId;
private String userName;
private Vehicle vehicle;
public int getUserId() {
return userId;
public void setUserId(int userId) {
this.userId = userId;
public String getUserName() {
return userName;
public void setUserName(String userName) {
this.userName = userName;
public Vehicle getVehicle() {
return vehicle;
public void setVehicle(Vehicle vehicle) {
this.vehicle = vehicle;

// Vehicle class
public class Vehicle {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int vehicleId;

private String vehicleName;

public int getVehicleId() {
return vehicleId;

public void setVehicleId(int vehicleId) {
this.vehicleId = vehicleId;

public String getVehicleName() {
return vehicleName;

public void setVehicleName(String vehicleName) {
this.vehicleName = vehicleName;

// i'm using this way

SessionFactory sessionFactory=new Configuration().configure("sqlserverconfig.cfg.xml").buildSessionFactory();
Session session=sessionFactory.openSession();
UserDetails ud = new UserDetails();
ud.setUserName("user name");

Vehicle v = new Vehicle();
v.setVehicleName("vehicle name");


user.setCreatedDate(new Date());

Ross Howard Miller

Joined: Jan 23, 2012
Posts: 11
Please surround you code with the code brackets makes life easier . I think you need to have a join in some way like this @JoinColumn(name = "vehicleId") or whatever your column name is in your database.
Ross Howard Miller

Joined: Jan 23, 2012
Posts: 11
so your code should look like this.

I agree. Here's the link: http://aspose.com/file-tools
subject: OneToOne mapping problem
It's not a secret anymore!