簡體   English   中英

將外鍵保存在休眠狀態

[英]Saving the foreign key in a hibernate

我有兩個表1.用戶2. USersLocation,在位置表中,我使用userId作為外鍵。 當我將位置對象保存在數據庫中時,我還要在表中保存當前用戶ID。

到目前為止,我嘗試過的是

SocialLogin控制器

 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,我是新來的冬眠請幫我

謝謝

如果你說你要保存Location數據庫與沿UserId ,你有這種關系是錯誤的。 這應該是一個OneToOne ,因為每個位置將只有一個UserId關聯。

另一方面,如果您試圖設置一個能夠存儲許多userId的位置,那么您這樣做也是錯誤的,因為您應該保存一組用戶,例如:

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

目前尚不清楚您要做什么,但我很確定您的錯誤在於您以錯誤的方式處理了關系。

將整個用戶對象保存到如下所示的會話中,而不是僅保存userId,因為稍后需要在location.setUserId(user);中設置完整的User對象。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM