Java 在休眠状态下保存外键
我有两张桌子。用户2。USersLocation,在位置表中,我使用userId作为外键。在数据库中保存location对象时,我希望将当前用户Id也保存在表中 到目前为止,我所尝试的是 社交网络控制器Java 在休眠状态下保存外键,java,spring,hibernate,spring-mvc,Java,Spring,Hibernate,Spring Mvc,我有两张桌子。用户2。USersLocation,在位置表中,我使用userId作为外键。在数据库中保存location对象时,我希望将当前用户Id也保存在表中 到目前为止,我所尝试的是 社交网络控制器 SocialAuthManager manager = socialAuthTemplate.getSocialAuthManager(); AuthProvider provider = manager.getCurrentAuthProvider(); Profile userProf
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);
位置控制器
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;
}
}
当我保存位置时,我得到的用户ID为空,我是新的休眠请帮助我
谢谢如果您想在数据库中保存一个位置以及用户ID,那么这个关系就错了。这应该是一个OneToOne,因为每个位置只有一个关联的用户ID 另一方面,如果您试图设置一个能够存储多个用户标识的位置,那么您也做得不对,因为您应该保存一组用户,例如:
@OneToMany(mappedBy = "UsersLocation")
private Set<User> user;
现在还不清楚您想做什么,但我很确定您的错误在于您以错误的方式处理关系。将整个用户对象保存到会话中,如下图所示,而不仅仅是userId,因为稍后您需要在location.setUserIduser
WebUtils.setSessionAttribute(request, "userId", user)
您是否还可以显示数据库架构和用户类的映射?同时发布实际错误日志。保存位置对象时没有错误,但位置表中的用户ID为nullcreate table users userId integer not null,email varchar50 not null,firstName varchar20,lastName varchar20,位置varchar50,主键userId Ok检查这行代码是否返回一个有效的用户对象,其中包含预期的所有用户值。用户WebUtils.getSessionAttributerequest,userid但一个位置可以有不同的用户,但一次只能有一个用户,然后当我从会话位置获取它时。setUserUser WebUtils.getSessionAttributerequest,User;它向我提供了类强制转换异常org.brickred.socialauth.Profile不能强制转换为de.tum.in.aics.thesis.project.models.userYou是否在会话中保存相同的用户类。它需要是同一个类或在同一个有效继承层次结构中。它不是同一个类,我使用的是用于facebook登录的Socialuth UsersLocation.java如果您定义了public void setUserIdUser user,那么它将期望用户不是org.brickred.socialauth.Profile。您的User.java类看起来如何?可能需要从配置文件中填充用户对象。这是你的设计,我只需要假设没有看到你的代码。是的,我从配置文件填充我的用户,让我放更多的代码