Reputation: 45692
Spring's JpaRepository throws an exception when I try to save
entity without id:
MyConfig config = new MyConfig();
config.setValue("value");
myConfigRepository.save(config);
How to make Hibernate not include id
field into insert query?
org.h2.jdbc.JdbcSQLException: NULL not allowed for column "MY_CONFIG_ID"; SQL statement:
insert into my_config (value, id) values (?, ?) [23502-190]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.table.Column.validateConvertUpdateSequence(Column.java:305)
at org.h2.table.Table.validateConvertUpdateSequence(Table.java:749)
at org.h2.command.dml.Insert.insertRows(Insert.java:151)
at org.h2.command.dml.Insert.update(Insert.java:114)
at org.h2.command.CommandContainer.update(CommandContainer.java:78)
at org.h2.command.Command.executeUpdate(Command.java:253)
at org.h2.server.TcpServerThread.process(TcpServerThread.java:345)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:159)
at java.lang.Thread.run(Thread.java:745)
my entity:
@Entity
public class MyConfig {
@Id
@SequenceGenerator(sequenceName = "MY_CONFIG_SEQ", name = "MyConfSeq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "MyConfSeq")
private Long id;
@Column
private String value;
//getters & setters
and repository:
public interface MyConfigRepository extends JpaRepository<MyConfig, Long>, JpaSpecificationExecutor {}
Upvotes: 0
Views: 2595
Reputation: 12205
The column MY_CONFIG_ID
is not part of your Hibernate
mapping.
The query for insert inserts into the fields id
and value
. Then, there must be a third column named MY_CONFIG_ID
, which is not mentioned in the entity
, and thus will be inserted as null
. If this column then has a not null
constraint, it will fail.
If you want your id
-column to be named MY_CONFIG_ID
then you need to ovveride the default column name (id
, same as the variable) by annotating it with @Column(name="MY_CONFIG_ID")
, if you want to use the default name id
you would need to remove the column (or at least the not null constraint.)
Upvotes: 2