This is the error:
java.sql.SQLException: Field ‘userid’ doesn’t have a default value at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:130) ~[mysql-connector-j-8.3.0.jar:8.3.0]
This is my Entity class:
@Entity
@Table(name = "users")
public class Users {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int userId;
}
Table:
CREATE TABLE `users` (
`user_id` int NOT NULL AUTO_INCREMENT,
`email` varchar(255) DEFAULT NULL,
`is_active` bit(1) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`registration_date` datetime(6) DEFAULT NULL,
`user_type_id` int DEFAULT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `UK_6dotkott2kjsp8vw4d0m25fb7` (`email`),
KEY `FK5snet2ikvi03wd4rabd40ckdl` (`user_type_id`),
CONSTRAINT `FK5snet2ikvi03wd4rabd40ckdl` FOREIGN KEY (`user_type_id`) REFERENCES `users_type` (`user_type_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
when i make insert i get that error:
Error Code: 1364. Field ‘userid’ doesn’t have a default value
2
Answers
There might be several reasons for that error. Let’s look more closer – the error comes from database and it is about
userid
field.user_id
so it is not a table where you insert record. May be Hibernate creates another table and you miss it.userId
. How this name in camel case translates to the field name in database depends on Hibernate’s settings and version. Typically it creates database fields in snake case, but you should be aware about it.hibernate.show_sql: true
in application.yml orhybernate.show_sql=true
if you have application.properties and look what it printsThere is a mismatch typing names.
in your Users entity class, the field is called userId, but in your MySQL table, the column is named user_id. To resolve this issue, you need to ensure that the names used in the Java entity class match those in the database table schema.
You can fix this by modifying your entity class to include the correct column name using the @Column annotation: