Java 批量插入中的 Postgres 错误:关系“hibernate_sequence”不存在位置 17
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29771730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Postgres error in batch insert : relation "hibernate_sequence" does not exist position 17
提问by kirti
I am performing hibernate jpa batch update and its giving me following error
我正在执行 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
I am using postgres database and my ID is auto generated
我正在使用 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;
This is my batch insert code snippet
这是我的批量插入代码片段
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();
}
I have added the following property in persistence.xml
我在persistence.xml中添加了以下属性
<property name="hibernate.id.new_generator_mappings" value="true"/>
Please suggest what i am doing wrong.
请建议我做错了什么。
回答by Mayur
Can you try following :
您可以尝试以下操作:
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "auto_gen")
@SequenceGenerator(name = "auto_gen", sequenceName = "A")
@Column(name = "ID")
private int id;
Thanks
谢谢
回答by Beri
If you don't want to change your entity definition, then you need to create a sequence in your postgreSQL schema with name hibernate_sequence
.
如果您不想更改实体定义,则需要在 postgreSQL 模式中使用 name 创建一个序列hibernate_sequence
。
CREATE SEQUENCE hibernate_sequence START 1;
UPDATE:
更新:
You are missing second sequence generatef, which you defined for your entity, just add it like previous one:
您缺少为实体定义的第二个序列 generatef,只需像上一个一样添加它:
CREATE SEQUENCE my_seq_gen START 1;
Sore useful tutorial: http://www.concretepage.com/hibernate/generatedvalue-strategy-generationtype-sequence-hibernate
酸痛有用的教程:http: //www.concretepage.com/hibernate/generatedvalue-strategy-generationtype-sequence-hibernate
回答by Sameer
I hope you get the answer but if you are still finding the answer this could be helpful.
我希望您能得到答案,但如果您仍在寻找答案,这可能会有所帮助。
I had same problem and resolved it annotating getter method of the id with @SequenceGenerator
and @GeneratedValue
.
我遇到了同样的问题并解决了它用@SequenceGenerator
和注释 id 的 getter 方法@GeneratedValue
。
@SequenceGenerator(name="seq-gen",sequenceName="MY_SEQ_GEN", initialValue=205, allocationSize=12)
@GeneratedValue(strategy= GenerationType.SEQUENCE, generator="seq-gen")
public int getId(){
return id;
}
回答by Vsevolod Poletaev
Try to annotate your id
with @Id
and @GeneratedValue(strategy = GenerationType.IDENTITY)
.
尝试id
用@Id
和注释你的@GeneratedValue(strategy = GenerationType.IDENTITY)
。
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
UPDATE:It will only work if your id column was declared as SERIAL
or BIGSERIAL
types.
更新:它仅在您的 id 列被声明为SERIAL
或BIGSERIAL
类型时才有效。
回答by Sergeant
I was with the same problem.I solved puting auto increment on id column from the table on postgres it works, like that.
我遇到了同样的问题。我解决了将自动增量放在 postgres 上表中的 id 列上的问题,就像那样。
ALTER TABLE mytable ALTER COLUMN id SET DEFAULT nextval('mytable_id_seq');
ALTER SEQUENCE mytable_id_seq OWNED BY mytable.id;
回答by Vijay Vikranth
In my case adding property name="hibernate.hbm2ddl.auto" value="update", solved the issue. You have to add the mentioned property in persistence.xml
在我的情况下,添加 property name="hibernate.hbm2ddl.auto" value=" update",解决了问题。您必须在persistence.xml中添加提到的属性
回答by Ifesinachi Bryan
According to a post, hibernate could not get next sequence value , set your @GeneratedIdcolumn with strategy GenerationType.IDENTITYinstead of GenerationType.SEQUENCE. So, you would have
根据一篇文章,hibernate 无法获取下一个序列值 ,请使用策略GenerationType.IDENTITY而不是GenerationType.SEQUENCE设置您的@GeneratedId列。所以,你会有
@Id
@SequenceGenerator(name="seq-gen",sequenceName="MY_SEQ_GEN"initialValue=205, allocationSize=12)
@GeneratedValue(strategy= GenerationType.IDENTITY, generator="seq-gen")
@Column(name="\"ID\"",unique=true,nullable=false)
private int id;
回答by Zon
Sometimes with @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
annotation you can have your sequence with empty intervals(after delete) and incorrect next autoincrement position. Try to set next autoincrement valueto the position after biggest id value:
有时使用@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
注释,您可以让序列具有空间隔(删除后)和不正确的下一个自动增量位置。尝试将下一个自动增量值设置为最大 id 值之后的位置:
ALTER SEQUENCE schema.entity_id_seq RESTART WITH 40072;
回答by Nyasha Chitiyo
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
回答by Māris Rubenis
Add the following to your application.properties
将以下内容添加到您的 application.properties
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
This will explicitly set to create or update all tables during Spring startup.
这将显式设置为在 Spring 启动期间创建或更新所有表。