This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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
Author

OneToOne mapping problem

Roshan Wankhade
Greenhorn

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
@Entity
public class UserDetails {
@Id @GeneratedValue(strategy= GenerationType.AUTO)
private int userId;
private String userName;
@OneToOne
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
@Entity
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");

ud.setVehicle(v);

session.beginTransaction();
user.setCreatedDate(new Date());
session.save(user);
session.save(ud);
session.save(v);
session.getTransaction().commit();
session.close();


Ross Howard Miller
Greenhorn

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
Greenhorn

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

 
GeeCON Prague 2014
 
subject: OneToOne mapping problem