I get an error ( 500-internal server error ) when posting with Spring Boot

Viewed 27

No matter what I do, I couldn't fix the problem, what do you think could be the problem ?

This is my model class :

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name="users")
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public long id;
    
    @Column(name="first_name")
    public String first_name;
    
    @Column (name="last_name")
    public String last_name;
}

This is my repository :

public interface CommentRepository extends JpaRepository<User , Long>{

    // All C.R.U.D database methods
    
    @Query (value = "Select * from users where first_name=:name" , nativeQuery=true)
    public List<User> getUser (String name);
}

Finally , This is my controller :

@RestController
@RequestMapping(path="/api/v1/user")
public class CommentController {
    
    @Autowired
    CommentRepository repository ;
    
    @GetMapping(path="/list")
    public List<User> users() {
        
        return repository.findAll();
    }  
    
    @GetMapping(path="/person")
    public List<User> user(@RequestParam String name ) {
        
        return repository.getUser(name);
    } 
    
    @PostMapping(path="/save")
    public void save(@RequestBody User user ) {
        
        repository.save ( user );
    } 
    
    @DeleteMapping(path="/delete")
    public void delete(@RequestBody User user ) {
        
        repository.delete( user );
    } 
}

Logcat :

java.sql.SQLException: Field 'id' doesn't have a default value
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.30.jar:8.0.30]
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.30.jar:8.0.30]
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:916) ~[mysql-connector-java-8.0.30.jar:8.0.30]
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1061) ~[mysql-connector-java-8.0.30.jar:8.0.30]
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1009) ~[mysql-connector-java-8.0.30.jar:8.0.30]
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1320) ~[mysql-connector-java-8.0.30.jar:8.0.30]
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:330) ~[hibernate-core-5.6.10.Final.jar:5.6.10.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:123) ~[hibernate-core-5.6.10.Final.jar:5.6.10.Final]
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:185) ~[hibernate-core-5.6.10.Final.jar:5.6.10.Final]
    at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]

According to this output, I should not give an empty value to the id value, but the id variable is automatically assigned on mysql.

I know I'm bad at giving model and class names I'm just doing a trial run :)

1 Answers

Your error indicates error in type of id. Either you pass it in request or set to following to prevent it:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)

instead of following:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Related