带PostgreSQL的Hibernate import.sql重复主键 [英] Hibernate import.sql with PostgreSQL repeats primary keys
问题描述
我有一个实体类,所有其他实体继承,它使用 GenerationType.AUTO
或 GenerationType.IDENTITY
。它们都不起作用:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $公共类` long serialVersionUID = -991512134416936719L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
public Entidade(){
}
public Entidade(int id){
this.id = id;
}
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
}
这里是我的persistence.xml file:
<?xml version =1.0encoding =UTF-8?>
< persistence version =2.1
xmlns =http://xmlns.jcp.org/xml/ns/persistencexmlns:xsi =http://www.w3.org/ 2001 / XMLSchema-instance
xsi:schemaLocation =http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd >
< persistence-unit name =...>
< jta-data-source> ...< / jta-data-source>
< class> ...< / class>
<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.PostgreSQLDialect/>
< property name =hibernate.hbm2ddl.autovalue =create-drop/>
< property name =hibernate.default_schemavalue =jcat/>
< property name =hibernate.hbm2ddl.import_files_sql_extractor
value =org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor/>
< property name =hibernate.show_sqlvalue =true/>
< property name =hibernate.hbm2ddl.import_filesvalue =import.sql/>
< property name =hibernate.c3p0.min_sizevalue =5/>
< property name =hibernate.c3p0.max_sizevalue =20/>
< property name =hibernate.c3p0.max_statementsvalue =50/>
< property name =hibernate.c3p0.timeoutvalue =3000/>
< property name =hibernate.c3p0.idle_test_periodvalue =1800/>
< / properties>
< / persistence-unit>
< /余辉>
可能Hibernate使用全局数据库范围的序列 hibernate_sequence
。您也可以从其他数据库导入它。您可以在 hibernate_sequence
中向您的脚本添加一个插入
语句,以获取大ID值。
请在SQL日志中查看Hibernate是否使用 hibernate_sequence
?
更新
@DouglasDeRizzoMeneghetti的一些额外笔记
我按照您的说法行事并且工作正常。 FK变成了可以为空的整数,现在我在数据库中只有一个序列,而不是每个ID。我发现即使连接表有自己的序列创建,所以通过使用一个序列,一切都变得更清洁,更有组织。
I am using Hibernate with PostgreSQL. I have an import.sql file that Hibernate executes after the schema is created/validated. The content is imported accordingly, but when I try to persist new entries through my application, Hibernate tries to use automatically generated primary keys starting from 1, which were already used by my import file. Is there way to fix this?
I have a single entity class that all other entities inherit from, which uses either GenerationType.AUTO
or GenerationType.IDENTITY
. Both of them did not work:
@MappedSuperclass
public class Entidade implements Serializable {
private static final long serialVersionUID = -991512134416936719L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
public Entidade() {
}
public Entidade(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
And here is my persistence.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="...">
<jta-data-source>...</jta-data-source>
<class>...</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.default_schema" value="jcat" />
<property name="hibernate.hbm2ddl.import_files_sql_extractor"
value="org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.import_files" value="import.sql" />
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="20" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.timeout" value="3000" />
<property name="hibernate.c3p0.idle_test_period" value="1800" />
</properties>
</persistence-unit>
</persistence>
Probably, Hibernate uses a global database-wide sequence called hibernate_sequence
. You can import it form an other database too. You can add to your script an insert
statement for a large id value in hibernate_sequence
.
Please, see in the SQL log does Hibernate use hibernate_sequence
?
Update
Some additional notes by @DouglasDeRizzoMeneghetti
I did as you said and it worked. The FKs became integers, which are nullable, and now I have only one sequence in the database, instead of one for each ID. I found out that even join tables had their own sequences created, so by using a single sequence, everything got cleaner and more organized.
这篇关于带PostgreSQL的Hibernate import.sql重复主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!