我正在执行 hibernate jpa 批量更新,它给了我以下错误
2015-04-21 15:53:51,907 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (Thread-283 (HornetQ-client-global-threads-462057890)) SQL Error: 0, SQLState: 42P01
2015-04-21 15:53:51,908 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (Thread-283 (HornetQ-client-global-threads-462057890)) ERROR: relation "my_seq_gen" does not exist
我正在使用 postgres 数据库,我的 ID 是自动生成的
@Id
@SequenceGenerator(name="seq-gen",sequenceName="MY_SEQ_GEN"initialValue=205, allocationSize=12)
@GeneratedValue(strategy= GenerationType.SEQUENCE, generator="seq-gen")
@Column(name="\"ID\"",unique=true,nullable=false)
private int id;
这是我的批量插入代码片段
getEm().getTransaction().begin();
System.out.println("transaction started--------------");
try {
for (Receipt ReceiptEntity : arrReceiptEntity) {
getEm().persist(ReceiptEntity);
}
getEm().getTransaction().commit();
System.out.println("commited");
} catch (Exception exception) {
System.out.println("error----------------------------------------------------------------------");
if(getEm().getTransaction().isActive())
getEm().getTransaction().rollback();
LOG.error(exception);
} finally {
getEm().flush();
getEm().clear();
getEm().close();
}
我在persistence.xml中添加了以下属性
<property name="hibernate.id.new_generator_mappings" value="true"/>
请指出我做错了什么。
最佳答案
尝试用 @Id
和 @GeneratedValue(strategy = GenerationType.IDENTITY)
注释您的 id
。
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
更新:仅当您的 id 列被声明为 SERIAL
或 BIGSERIAL
类型时才有效。
关于java - 批量插入中的 Postgres 错误 : relation "hibernate_sequence" does not exist position 17,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29771730/