0

I'm learning Spring boot and i have some exceptions when i want to create a "Lot" that i don't understand.

** Utilisateur entity **

@JsonIgnoreProperties
@Entity
@Data
public class Utilisateur {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column
private String nom;

@Column
private String prenom;

@Column
private String adresse;

@Column
private String numeroTelephone;

@Column
private String email;

@OneToMany(cascade = CascadeType.ALL)
private List<Lot> lotList;

@OneToMany(cascade = CascadeType.ALL)
private List<Tentative> tentativeList;

}

The "utilisateur" (means User) is related to n "Lot" (means prize), and can do three tries ("Tentative") per day.

** Lot entity **

@JsonIgnoreProperties
@Entity
@Data
public class Lot {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
@Column(name = "nom")
private String nom;

@NotNull
@Column(name = "description")
private String description;

@Column(name = "date_time_declenchement")
private LocalDateTime dateTimeDeclenchement;

@NotNull
@Column(name = "is_gagne")
private boolean isGagne;

@ManyToOne(cascade = CascadeType.ALL)
private Utilisateur utilisateur;

}

** Tentative entity **

@JsonIgnoreProperties
@Entity
@Data
public class Tentative {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
@Column
private LocalDateTime dateTentative;

@ManyToOne(cascade = CascadeType.ALL)
private Utilisateur utilisateur;
}

And my create.sql !

CREATE TABLE Utilisateur
(
 id INT PRIMARY KEY AUTO_INCREMENT,
 nom VARCHAR(100) NOT NULL,
 prenom VARCHAR(100) NOT NULL,
 adresse VARCHAR(100) NOT NULL,
 numero_telephone VARCHAR(100) NOT NULL,
 email VARCHAR(100) NOT NULL UNIQUE,
);

CREATE TABLE Lot
(
 id INT PRIMARY KEY AUTO_INCREMENT,
 nom VARCHAR(100) NOT NULL,
 description VARCHAR(255) NOT NULL,
 date_time_declenchement DATE,
 is_gagne BOOLEAN NOT NULL,
);

CREATE TABLE Tentative
(
 id INT PRIMARY KEY AUTO_INCREMENT,
 id_utilisateur INT,
 id_lot INT,
 date_tentative DATE NOT NULL,
);

ALTER TABLE Tentative
ADD CONSTRAINT fk_id_utilisateur_tentative FOREIGN KEY (id_utilisateur) 
REFERENCES Utilisateur(id);

ALTER TABLE Tentative
ADD CONSTRAINT fk_id_lot_tentative FOREIGN KEY (id_lot) REFERENCES Lot(id);

When i want to create a "LOT" with the body :

{
 "nom":"TV Samsung",
 "description":"Télévision Plasma Samsung 106cm"
}

I get this exception :

{
"timestamp": 1510068319912,
"status": 500,
"error": "Internal Server Error",
"exception": 
"org.springframework.dao.InvalidDataAccessResourceUsageException",
    "message": "could not prepare statement; SQL [insert into lot (id, 
date_time_declenchement, description, is_gagne, nom, utilisateur_id) values 
(null, ?, ?, ?, ?, ?)]; nested exception is 
org.hibernate.exception.SQLGrammarException: could not prepare statement",
    "path": "/creerlot"
}

I don't see why i've this error. Firsty, i've done my project with only hibernate / Jpa. With the interface CRUDRepository it was easy to find / save my objects. But now that i have sql, all my methods return an exception, except the one where i create an "Utilisateur".

Thanks for all !

Steven Klinger
  • 284
  • 8
  • 21

0 Answers0