将外键保存在休眠状态

时间:2014-04-30 15:03:50

标签: java spring hibernate spring-mvc

我有两个表1.用户2. USersLocation,在位置表中我使用userId作为外键。当我在数据库中保存位置对象时,我想在表中保存当前用户ID。

到目前为止我尝试过的是

SocialLogin Controller

 SocialAuthManager manager = socialAuthTemplate.getSocialAuthManager();
 AuthProvider provider = manager.getCurrentAuthProvider();
 Profile userProfile = provider.getUserProfile();

位置控制器

  UsersLocation location = new UsersLocation();
  location.setSourceLat(SOURCE_LATITUDE);
  location.setSourceLng(SOURCE_LONGITUDE);
  location.setDestinationLat(DEST_LATITUDE);
  location.setDestinationLng(DEST_LONGITUDE);
  location.setUserId((User) WebUtils.getSessionAttribute(request, "userId"));
  placesService.saveLocaion(location);

Userslocation类是

@Entity
@Table(name = "locations")
public class UsersLocation implements Serializable{

private static final long serialVersionUID = 1L;
private double sourceLat;
private double sourceLng;
private double destinationLat;
private double destinationLng;

public UsersLocation(){}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int locationId;

@ManyToOne
@JoinColumn(name = "userId")
private User user;

public void setUserId(User user){
    this.user = user;
}

public User getUserId(){
    return user;
}

@Column(name = "SourceLat", nullable = false)
public double getSourceLat(){
    return sourceLat;
}

public void setSourceLat(double sourceLat){
    this.sourceLat = sourceLat;
}

@Column(name = "SourceLng", nullable = false)
public double getSourceLng(){
    return sourceLng;
}

public void setSourceLng(double sourceLng){
    this.sourceLng = sourceLng;
}

@Column(name = "DestinationLat", nullable = false)
public double getDestinationLat(){
    return destinationLat;
}

public void setDestinationLat(double destinationLat){
    this.destinationLat = destinationLat;
}

@Column(name = "DestinationLng", nullable = false)
public double getDestinationLng(){
    return destinationLng;
}

public void setDestinationLng(double destinationLng){
    this.destinationLng = destinationLng;
}

}

用户类

 @Entity
 @Table(name = "users")
 public class User implements Serializable{

private static final long serialVersionUID = 1L;

private Integer userId;
private String email;
private String lastName;
private String firstName;   
private String location;

public User() {

}

@Id
@Column(name = "userId", unique = true, nullable = false)
public Integer getUserId() {
    return this.userId;
}

public Integer setUserId(Integer socialId) {
    return this.userId = socialId;
}

@Column(name = "email", nullable = false, length = 50)
public String getEmail() {
    return email;
}


public void setEmail(String email) {
    this.email = email;
}

@Column(name = "firstName", nullable = true, length = 20)
public String getFirstName() {
    return firstName;
}


public void setFirstName(String firstName) {
    this.firstName = firstName;
}

@Column(name = "lastName", nullable = true, length = 20)
public String getLastName() {
    return lastName;
}


public void setLastName(String lastName) {
    this.lastName = lastName;
}

@Column(name = "location", nullable = true, length = 50)
public String getLocation() {
    return location;
}


public void setLocation(String location) {
    this.location = location;
}

}

当我保存位置时,我得到userId NULL,我是hibernate的新手,请帮助我

由于

2 个答案:

答案 0 :(得分:0)

如果您要将LocationUserId一起保存在数据库中,则表明您的关系错误。这应该是OneToOne,因为每个位置只有一个UserId关联。

另一方面,如果您尝试设置一个能够存储多个userId的位置,那么您也应该做错了,因为您应该保存一组用户,例如:

@OneToMany(mappedBy = "UsersLocation")
private Set<User> user;

你要做的事情并不是很清楚,但我很确定你的错误在于你以错误的方式处理关系。

答案 1 :(得分:0)

将整个用户对象保存到会话中,而不仅仅是userId,稍后您需要在location.setUserId(user)中设置完整的User对象;

WebUtils.setSessionAttribute(request, "userId", user)