spring data jpa save 手动分配标识符
spring data jpa save manually assign identifier
我使用 spring 保存
HighWay highWay = new HighWay();
highWay.setId("000");
HighWayRepository hRepository = (HighWayRepository) context
.getBean("highWayRepository");
hRepository.save(highWay);
hRepository.flush();
public interface HighWayRepository extends JpaRepository<HighWay, String> {
}
table 就像 f_id
varchar(256) NOT NULL,
public class HighWay {
@Id
@Column(name="f_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String id;}
但抛出异常
Caused by: java.sql.SQLException: Field 'f_id' doesn't have a default
value at
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946) at
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870) at
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573) at
com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1169)
at
com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:693)
at
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1404)
at
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1318)
at
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1303)
at
com.alibaba.druid.pool.DruidPooledPreparedStatement.executeUpdate(DruidPooledPreparedStatement.java:253)
at
org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
... 51 more
我查看文档,
Id-Property 检查(默认)默认情况下 Spring Data JPA 检查给定实体的 Id-Property。如果 Id-Property 为 null,则该实体将被假定为新的,否则为非新的。
sql 就像 Hibernate:insert into us_highway (blockTime, blockType, endNum, predictTime, publishTime, roadName, situation, startNum, tips) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
不是 id 是 insert!
因为我想手动分配id,如果是new,id属性不为null,如何配置保存?
如果您在 id 注释下方分配 Id manually.Remove。
@GeneratedValue(strategy = GenerationType.IDENTITY)
我使用 spring 保存
HighWay highWay = new HighWay();
highWay.setId("000");
HighWayRepository hRepository = (HighWayRepository) context
.getBean("highWayRepository");
hRepository.save(highWay);
hRepository.flush();
public interface HighWayRepository extends JpaRepository<HighWay, String> {
}
table 就像 f_id
varchar(256) NOT NULL,
public class HighWay {
@Id
@Column(name="f_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String id;}
但抛出异常
Caused by: java.sql.SQLException: Field 'f_id' doesn't have a default value at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573) at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1169) at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:693) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1404) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1318) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1303) at com.alibaba.druid.pool.DruidPooledPreparedStatement.executeUpdate(DruidPooledPreparedStatement.java:253) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208) ... 51 more
我查看文档, Id-Property 检查(默认)默认情况下 Spring Data JPA 检查给定实体的 Id-Property。如果 Id-Property 为 null,则该实体将被假定为新的,否则为非新的。
sql 就像 Hibernate:insert into us_highway (blockTime, blockType, endNum, predictTime, publishTime, roadName, situation, startNum, tips) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
不是 id 是 insert!
因为我想手动分配id,如果是new,id属性不为null,如何配置保存?
如果您在 id 注释下方分配 Id manually.Remove。
@GeneratedValue(strategy = GenerationType.IDENTITY)