【发布时间】:2016-01-09 18:20:56
【问题描述】:
我有一个实体
@Entity
public class Book {
@Id
@Column(name = "book_id")
@SequenceGenerator(name = "book_book_id_seq", sequenceName = "book_book_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "book_book_id_seq")
private Long id;
// getter, setter & other fields
}
有架构
CREATE TABLE book
(
book_id bigint NOT NULL DEFAULT nextval('book_book_id_seq'::regclass),
CONSTRAINT book_pkey PRIMARY KEY (book_id)
)
我想要实现的是有时我想使用由数据库生成的序列/ID,但有时数据是在其他地方创建的,我想用现有的(手动)ID 创建。
我无法使用 Spring Data JPA 方式(使用 CrudRepository)或 JPA 方式(使用 EntityManager)手动设置 id,但原生查询没有问题。这是 JPA 限制吗?我的问题有什么解决方法吗?
Book book01 = new Book();
bookRepo.save(book01); // Book with id 1 is created
Book book02 = new Book();
book02.setId(5555L);
bookRepo.save(book02); // Does not create book with id 5555, but 2
Query nativeQuery = entityManager.createNativeQuery("INSERT INTO book VALUES (6666);");
nativeQuery.executeUpdate(); // Book with id 6666 is created
Query nativeQuery02 = entityManager.createNativeQuery("INSERT INTO book DEFAULT VALUES;");
nativeQuery02.executeUpdate(); // Book with id 3 is created
我正在使用 PostgreSQL 9.4、Hibernate 5 和 Java 8。
【问题讨论】:
-
我认为这可能是 jpa 的
@GeneratedValue的工作方式(我不确定)。但是,这并不重要,因为如果您想将生成的 id 与手动插入的 id 混合,则需要使用序列 won't be the best choice(任何基于int/bigint的解决方案)。你应该考虑使用完全不同的东西,f.ex。uuids.
标签: postgresql jpa nativequery