java - "message": "无法执行语句;嵌套异常是 org.hibernate.exception.GenericJDBCException: 无法执行语句",
问题描述
每当我尝试使用 userId 作为参数将某些内容添加到我的表格文档中时,我都会在邮递员中使用此“localhost:9293/SpringMVC/servlet/documents/add?userId=2”收到此错误消息
无法执行语句;嵌套异常是 org.hibernate.exception.GenericJDBCException:无法执行语句
我已经有一个使用 userId=2 创建的用户
我的用户.java
@Entity(name="user")
public class User implements Serializable , UserDetails {
/**
*
*/
private static final long serialVersionUID = 1L;
private User user;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int userId;
private String name;
private int phone_number;
private String email;
private String password;
private String address;
private boolean verified;
private boolean subscribed;
private String idStrype;
//@Column(nullable = true, length = 64)
//private String documents;
@ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
@JsonIgnore
@JsonBackReference
private Role role;
@OneToMany(cascade = CascadeType.PERSIST,mappedBy="user",fetch=FetchType.LAZY)
//@JsonManagedReference
private Set<Ad> ads;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "user")
private Documents documents;
/*@OneToMany(cascade = CascadeType.PERSIST,mappedBy="user",fetch=FetchType.LAZY)
//@JsonManagedReference
private Set<Insurance> insurances;*/
@OneToMany(cascade = CascadeType.PERSIST,mappedBy="user",fetch=FetchType.LAZY)
//@JsonManagedReference
private Set<Offer> offers;
@OneToMany(cascade = CascadeType.PERSIST,mappedBy="user",fetch=FetchType.LAZY)
//@JsonManagedReference
private Set<Contract> contracts;
@OneToMany(cascade = CascadeType.PERSIST,mappedBy="user",fetch=FetchType.LAZY)
//@JsonManagedReference
private Set<Reclamation> reclamations;
public String getIdStrype() {
return idStrype;
}
public void setIdStrype(String idStrype) {
this.idStrype = idStrype;
}
/**
* @return the userId
*/
public int getUserId() {
return userId;
}
/**
* @param userId the userId to set
*/
public void setUserId(int userId) {
this.userId = userId;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the phone_number
*/
public int getPhone_number() {
return phone_number;
}
/**
* @param phone_number the phone_number to set
*/
public void setPhone_number(int phone_number) {
this.phone_number = phone_number;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
我的文档.java
@Entity
@Table(name= "document")
public class Documents implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name= "id")
private int id;
@Column(name="fichedepaie")
private String fichedepaie;
@Column(name="piecedidentite")
private String piecedidentite;
@Column(name="lettredengagement")
private String lettredengagement;
@Column(name="cautionnement")
private String cautionnement;
@OneToOne
private User user;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFichedepaie() {
return fichedepaie;
}
public void setFichedepaie(String fichedepaie) {
this.fichedepaie = fichedepaie;
}
public String getPiecedidentite() {
return piecedidentite;
}
public void setPiecedidentite(String piecedidentite) {
this.piecedidentite = piecedidentite;
}
public String getLettredengagement() {
return lettredengagement;
}
public void setLettredengagement(String lettredengagement) {
this.lettredengagement = lettredengagement;
}
public String getCautionnement() {
return cautionnement;
}
public void setCautionnement(String cautionnement) {
this.cautionnement = cautionnement;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Documents(String fichedepaie, String piecedidentite, String lettredengagement, String cautionnement) {
super();
this.fichedepaie = fichedepaie;
this.piecedidentite = piecedidentite;
this.lettredengagement = lettredengagement;
this.cautionnement = cautionnement;
}
public Documents() {
super();
}
}
我的文档Service.java
@Override
public Documents addDocuments(Documents d, int userid) {
// TODO Auto-generated method stub
d.setUser(userrepo.findById(userid).get());
docrepo.save(d);
return d;
}
我的文档Controller.java
@RestController
@RequestMapping("/documents")
public class DocumentsController {
@Autowired
DocumentsService docserv ;
@Autowired
UserRepository userrepo;
@PostMapping("/add")
private Documents addDocuments(@RequestBody Documents docs, @RequestParam("userId") int userId)
{
docserv.addDocuments(docs,userId);
return docs;
}
解决方案
我认为,您的实体中缺少@JoinColumn(name = "user_id")
注释Documents
@OneToOne
@JoinColumn(name = "user_id")
private User user;
请注意,user_id
如果它是不同的字段,则必须是文档表中的外键,然后使用那个。