postgresql 休眠模式参数在@SequenceGenerator 注释中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2606339/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-20 00:05:55  来源:igfitidea点击:

Hibernate schema parameter doesn't work in @SequenceGenerator annotation

hibernatepostgresqlschemasequencejpa-2.0

提问by tabdulin

I have the following code:

我有以下代码:

@Entity
@Table(name = "my_table", schema = "my_schema")
@SequenceGenerator(name = "my_table_id_seq", sequenceName = "my_table_id_seq", 
                   schema = "my_schema")
public class MyClass {
    @Id
    @GeneratedValue(generator = "my_table_id_seq", 
                    strategy = GenerationType.SEQUENCE)
    private int id;

}

Database: Postgresql 8.4, Hibernate annotations 3.5.0-Final.

数据库:Postgresql 8.4,Hibernate 注释 3.5.0-Final。

When saving the object of MyClass it generates the following SQL query:

保存 MyClass 的对象时,它会生成以下 SQL 查询:

select nextval('my_table_id_seq')

So there is no schema prefix and therefore the sequence cannot be found. When I write the sequenceName like

所以没有模式前缀,因此无法找到序列。当我像这样写 sequenceName 时

sequenceName = "my_schema.my_table_id_seq"

everything works.

一切正常。

Do I have misunderstandings for meaning of schema parameter or is it a bug? Any ideas how to make schema parameter working?

我对模式参数的含义有误解还是错误?任何想法如何使架构参数工作?

回答by jambriz

Same problem here, looks like a bug to me. I′m using hibernate 3.6.7 Looking at the source code i see a method org.hibernate.cfg.annotations.reflection.JPAOverridenAnnotationReader#buildSequenceGeneratorAnnotation(Element element)that seems to copy the values of name, sequence-name, initial-valueand allocation-sizeattributes, but I see no reference to catalogor schema

同样的问题在这里,对我来说看起来像是一个错误。我使用Hibernate 3.6.7查看源代码,我看到了一个方法org.hibernate.cfg.annotations.reflection.JPAOverridenAnnotationReader#buildSequenceGeneratorAnnotation(Element element),似乎要复制的值namesequence-nameinitial-valueallocation-size属性,但我看不出有任何引用catalogschema

i expected to see something analogous to method getTable(Element tree, XMLContext.Default defaults)(of the same class) which has

我希望看到类似于方法getTable(Element tree, XMLContext.Default defaults)(同一类)的东西

annotation.setValue("schema", table.schema());
annotation.setValue("catalog", table.catalog());` 

or buildTableGeneratorAnnotationwhich has

或者buildTableGeneratorAnnotation

copyStringAttribute(ad, element, "catalog", false);
copyStringAttribute(ad, element, "schema", false);

So, even if a little hackish, the way around -for this version at least- seems to be prefixing the sequenceNameas you say.

所以,即使有点hackish,方法 - 至少对于这个版本 - 似乎是sequenceName你所说的前缀。

回答by Vlad

My workaround looks like this (JPA 2.1, Hibernate 4.3.8.Final, PostgreSQL 9.4):

我的解决方法如下(JPA 2.1、Hibernate 4.3.8.Final、PostgreSQL 9.4):

@SequenceGenerator(name = "seq_name", sequenceName = "my_schema.seq_name", schema = "my_schema", allocationSize = 1, initialValue = 1)

回答by WMS

I solved this problem in postgresql 9.6 by only adding my schema before the sequence name, like this:

我在 postgresql 9.6 中解决了这个问题,只在序列名称之前添加了我的架构,如下所示:

(before) sequence name: seq_area_tematica
(after) sequence name: sisbp.seq_area_tematica

And its works. See the code:

以及它的作品。看代码:

@Id
@Column(name="seq_area_tematica")
@GeneratedValue(generator="sequence",strategy=GenerationType.SEQUENCE) 
@SequenceGenerator(name="sequence",sequenceName="sisbp.seq_area_tematica")
private Long id;

See the "sisbp" before the sequenceName. The sequenceName was "seq_area_tematica" and now is "sisbp" (schema) + "seq_area_tematica".

请参阅sequenceName 之前的“sisbp”。sequenceName 是“seq_area_tematica”,现在是“sisbp”(模式)+“seq_area_tematica”。

回答by Rafael Fontoura

Using Hibernate 4.2.0.Finalhere and have the same problem. Looks like it is a bug like answered by another users. I wanted to use a dynamic schema for sequences, depending on the schema set for the session, but for some sequences I wanted to use publicschema. So for me I had to use the solution you proposed: put the schema name on sequence name where I want to use a specific schema:

4.2.0.Final在这里使用 Hibernate并有同样的问题。看起来这是另一个用户回答的错误。我想对序列使用动态模式,具体取决于为会话设置的模式,但对于某些序列,我想使用public模式。所以对我来说,我不得不使用你提出的解决方案:将模式名称放在我想使用特定模式的序列名称上:

@SequenceGenerator(name = "my_table_id_seq", sequenceName="my_schema.my_table_id_seq",
 schema = "my_schema")

For the cases where I wanted to use the schema set for the session I used the sequenceNamewithout the schema prepended.

对于我想为会话使用模式集的情况,我使用了sequenceName没有预先设置的模式。

For those that want the same schema for all sequencesyou can use hibernate.default_schemaproperty. With that you don't need to change your @SequenceGeneratorproperties:

对于那些希望所有序列都使用相同模式的人,您可以使用 hibernate.default_schema属性。这样你就不需要改变你的@SequenceGenerator属性:

<prop key="hibernate.default_schema">my_schema_name</prop>

I am using PostgreSQL DBMS. If you want to change dynamically the name of the sequence when Hibernate calls nextval('my_sequence')you can extend your database dialect class and configure Hibernate to use. You need just to override getSequenceNextValString()method. The only information provided to the method is the sequenceNameproperty defined on @SequenceGenerator:

我正在使用 PostgreSQL DBMS。如果您想在 Hibernate 调用时动态更改序列的名称,nextval('my_sequence')您可以扩展数据库方言类并配置 Hibernate 以使用。你只需要覆盖getSequenceNextValString()方法。提供给该方法的唯一信息是在 上sequenceName定义的属性@SequenceGenerator

public class SchemaPostgreSQLDialect extends PostgreSQL82Dialect {
    @Override
    public String getSequenceNextValString(String sequenceName) {
        String seqFinalName = mySequenceNameModifierMethod(sequenceName);       
        return "select nextval('" + seqFinalName + "')";
    }

    private String mySequenceNameModifierMethod(String originalSequenceName) {
        // magic modification here
        return modifiedSequenceName;
    }
}

I didn't use this last way to change the name of the sequences because it seems less appropriated to my case.

我没有使用最后一种方法来更改序列的名称,因为它似乎不太适合我的情况。

回答by Pascal Thivent

This sounds like a bug: the JPA provider should honor the "new" (since Java Persistence 2.0) schemaand catalogattributes of the @SequenceGeneratorannotation. I suggest to raise a Jira issue(the annotations and entity manager projects are now under core), couldn't find any existing one.

这听起来像是一个错误:JPA 提供者应该尊重注解的“新”(自 Java Persistence 2.0 起)schemacatalog属性@SequenceGenerator。我建议提出一个 Jira 问题(注释和实体管理器项目现在处于核心之下),找不到任何现有的问题。

回答by johnm

Same problem, using Hibernate 4.3.6.Final, with Spring 4.1.4.RELEASE, on Oracle Database 11g Enterprise Edition Release 11.2.0.1.0.

同样的问题,在 Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 上使用 Hibernate 4.3.6.Final 和 Spring 4.1.4.RELEASE。

Looks like this is a bug => https://hibernate.atlassian.net/browse/HHH-7232

看起来这是一个错误 => https://hibernate.atlassian.net/browse/HHH-7232

We got around the issue by creating a synonym in schema A pointing to the sequence in schema B. This synonym was what we used in the schema attribute of @SequenceGenerator annotation

我们通过在模式 A 中创建一个同义词指向模式 B 中的序列来解决这个问题。这个同义词是我们在@SequenceGenerator 注释的模式属性中使用的

回答by buzz3791

Try moving the SequenceGenerator annotation from the POJO's class declaration to idfield declaration. With Hibernate 3.6.4 this

尝试将 SequenceGenerator 注释从 POJO 的类声明移动到id字段声明。使用 Hibernate 3.6.4 这个

@Entity
@Table(name = "my_table", schema = "my_schema")
public class MyClass {
    @Id
    @GeneratedValue(generator = "my_table_id_seq", 
                strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name = "my_table_id_seq", sequenceName = "my_table_id_seq", schema = "my_schema")
    private int id;

}

produces this for MySQL

为 MySQL 生成这个

create table my_schema.my_table_id_seq (
     next_val bigint 
);

insert into my_schema.my_table_id_seq values ( 1 );

and this for PostgreSQL

这对于 PostgreSQL

create sequence my_schema.my_table_id_seq start 1 increment 50;

回答by Joshua D. Drake

Hmmm, I don't work with the internals of hibernate much but there is some info here:

嗯,我不太了解 hibernate 的内部结构,但这里有一些信息:

https://forum.hibernate.org/viewtopic.php?p=2406761

https://forum.hibernate.org/viewtopic.php?p=2406761

You can also set the default schema for a user connecting to PostgreSQL via ALTER USER ... SET SEARCH_PATH

您还可以通过 ALTER USER ... SET SEARCH_PATH 为连接到 PostgreSQL 的用户设置默认模式

回答by MhagnumDw

In my case, the execution does not go through the location indicated by @jambriz.

就我而言,执行不会经过@jambriz 指示的位置。

Version: Hibernate 3.6.10.Final

版本:Hibernate 3.6.10.Final

The problem occurs when hibernate.id.new_generator_mappings = falsein persistence.xml. When set to trueor simply remove, the sequence scheme is used by Hibernate to build SQL. Solution:

问题发生hibernate.id.new_generator_mappings = falsepersistence.xml. 当设置为true或简单地删除时,Hibernate 使用序列方案来构建 SQL。解决方案:

<property name="hibernate.id.new_generator_mappings" value="true" />
<!-- Or remove this property -->

Where's the problem in the code?

代码哪里出了问题?

In the AnnotationBinder.javaclass between lines 445 and 480. In the ifblock, line 455, you can see the scheme being set. In the else the scheme is not set.

在第445 行和第 480 行之间的AnnotationBinder.java类中。在第if455 行的块中,您可以看到正在设置的方案。在 else 中没有设置方案。

Hope this helps!

希望这可以帮助!

回答by ulima69

Hello i was having same problem

你好我有同样的问题

but set your hibernate.hbm2ddl.auto to update and run.

但是将您的 hibernate.hbm2ddl.auto 设置为更新和运行。

<property name="hibernate.hbm2ddl.auto">update</property>