Hibernate 架构参数在 @SequenceGenerator 注释中不起作用

发布于 2024-08-28 05:35:35 字数 705 浏览 3 评论 0原文

我有以下代码:

@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;

}

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

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

select nextval('my_table_id_seq')

因此没有模式前缀,因此无法找到序列。当我写下sequenceName时,

sequenceName = "my_schema.my_table_id_seq"

一切正常。

我对模式参数的含义有误解还是一个错误?有什么想法如何使架构参数起作用吗?

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.

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

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 = "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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

中性美 2024-09-04 05:35:36

我的解决方法如下所示(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)

My workaround looks like this (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)
∞觅青森が 2024-09-04 05:35:36

这里同样的问题,对我来说看起来像是一个错误。我正在使用休眠 3.6.7
查看源代码,我看到一个方法 org.hibernate.cfg.annotations.reflection.JPAOverridenAnnotationReader#buildSequenceGeneratorAnnotation(Element element) 似乎复制了 name 的值,< code>sequence-name、initial-valueallocation-size 属性,但我没有看到对 catalog的引用>schema

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

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

buildTableGeneratorAnnotation 具有

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

因此,即使有点 hackish,至少对于这个版本来说,解决方法似乎是像你所说的那样在 sequenceName 前面加上前缀。

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-value and allocation-sizeattributes, but I see no reference to catalogor schema

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

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

or buildTableGeneratorAnnotation which has

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 sequenceName as you say.

心凉 2024-09-04 05:35:36

我在 postgresql 9.6 中通过仅在序列名称之前添加我的模式解决了这个问题,如下所示:

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

及其工作原理。查看代码:

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

查看sequenceName之前的“sisbp”。序列名称是“seq_area_tematica”,现在是“sisbp”(模式)+“seq_area_tematica”。

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

(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".

彩扇题诗 2024-09-04 05:35:36

在这里使用 Hibernate 4.2.0.Final 也有同样的问题。看起来这是一个错误,就像其他用户回答的那样。我想对序列使用动态架构,具体取决于会话的架构集,但对于某些序列,我想使用 public 架构。因此,对我来说,我必须使用您提出的解决方案:将模式名称放在我想要使用特定模式的序列名称上:

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

对于我想使用会话模式集的情况,我使用了 sequenceName< /code> 不带模式前缀。

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

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

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

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;
    }
}

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

Using Hibernate 4.2.0.Final here 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 public schema. 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:

@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 sequenceName without the schema prepended.

For those that want the same schema for all sequences you can use hibernate.default_schema property. With that you don't need to change your @SequenceGenerator properties:

<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 sequenceName property defined on @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.

私藏温柔 2024-09-04 05:35:36

这听起来像是一个错误:JPA 提供程序应该尊重“新”(自 Java Persistence 2.0 起)架构@SequenceGenerator 注释。我建议提出 Jira 问题(注释和实体管理器项目现在已在 core 下),找不到任何现有的。

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

美人如玉 2024-09-04 05:35:36

同样的问题,在 Oracle Database 11g 企业版版本 11.2.0.1.0 上使用 Hibernate 4.3.6.Final 和 Spring 4.1.4.RELEASE。

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

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

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.

Looks like this is a bug => 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

橘和柠 2024-09-04 05:35:36

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

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

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

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

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

半﹌身腐败 2024-09-04 05:35:36

尝试将 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;

}

会为 MySQL 生成此值

create table my_schema.my_table_id_seq (
     next_val bigint 
);

insert into my_schema.my_table_id_seq values ( 1 );

,为 PostgreSQL 生成此值

create sequence my_schema.my_table_id_seq start 1 increment 50;

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

@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

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

create sequence my_schema.my_table_id_seq start 1 increment 50;
北方。的韩爷 2024-09-04 05:35:36

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

版本:Hibernate 3.6.10.Final

persistence.xml 中的 hibernate.id.new_generator_mappings = false 时会出现此问题。当设置为 true 或简单删除时,Hibernate 将使用序列方案来构建 SQL。 解决方案:

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

代码中的问题出在哪里?

AnnotationBinder.java 位于第 445 行和第 480 行之间的类。在第 455 行的 if 块中,您可以看到正在设置的方案。 else 中未设置方案。

希望这有帮助!

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

Version: Hibernate 3.6.10.Final

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

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

Where's the problem in the code?

In the AnnotationBinder.java class between lines 445 and 480. In the if block, line 455, you can see the scheme being set. In the else the scheme is not set.

Hope this helps!

若水微香 2024-09-04 05:35:36

当使用 hibernate 和 spring 时
请设置

<prop key="hibernate.id.new_generator_mappings">true</prop>

when using hibernate with spring
please set

<prop key="hibernate.id.new_generator_mappings">true</prop>
九厘米的零° 2024-09-04 05:35:36

我在我的应用程序中遇到了同样的问题,该应用程序在 Oracle Database 19c 上使用 Hibernate 5.2.7 和 Spring 4.3.6。将sequenceName =“MY_SEQUENCE”更改为sequenceName =“MY_SCHEMA.MY_SEQUENCE”效果很好。

@Id
@Column(name = "ID")
@SequenceGenerator(schema = "MY_SCHEMA", name = "MY_SEQ_GENERATOR", sequenceName = "MY_SCHEMA.MY_SEQUENCE")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "MY_SEQ_GENERATOR")
private Long id;

I had the same problem in my app which is using Hibernate 5.2.7, with Spring 4.3.6, on Oracle Database 19c. Changing sequenceName = "MY_SEQUENCE" to sequenceName = "MY_SCHEMA.MY_SEQUENCE" worked perfectly.

@Id
@Column(name = "ID")
@SequenceGenerator(schema = "MY_SCHEMA", name = "MY_SEQ_GENERATOR", sequenceName = "MY_SCHEMA.MY_SEQUENCE")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "MY_SEQ_GENERATOR")
private Long id;
冧九 2024-09-04 05:35:36

您好,我遇到了同样的问题

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

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

Hello i was having same problem

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

<property name="hibernate.hbm2ddl.auto">update</property>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文