0

whenever I try to add something into my table documents using the userId as a parameter I get this error message in postman using this "localhost:9293/SpringMVC/servlet/documents/add?userId=2"

could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement

I already have a user created with the userId=2

my user.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;
}

my documents.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();
}

}

my documentsService.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;
}

my documentsController.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;  
}  
1
  • can you paste more stacktrace lines ? Commented Mar 19, 2021 at 1:23

1 Answer 1

0

I think, you are missing @JoinColumn(name = "user_id") annotation in your Documents entity

@OneToOne
@JoinColumn(name = "user_id")
private User user;

Please note that user_id must be the foreign key in your document table if it is the different field, then use that one.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.