Hibernate 架构参数在 @SequenceGenerator 注释中不起作用
我有以下代码:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我的解决方法如下所示(JPA 2.1、Hibernate 4.3.8.Final、PostgreSQL 9.4):
My workaround looks like this (JPA 2.1, Hibernate 4.3.8.Final, PostgreSQL 9.4):
这里同样的问题,对我来说看起来像是一个错误。我正在使用休眠 3.6.7
查看源代码,我看到一个方法
org.hibernate.cfg.annotations.reflection.JPAOverridenAnnotationReader#buildSequenceGeneratorAnnotation(Element element)
似乎复制了name
的值,< code>sequence-name、initial-value
和allocation-size
属性,但我没有看到对catalog
或的引用>schema
我希望看到类似于方法
getTable(Element tree, XMLContext.Default defaults)
(同一类)的方法,该方法具有或
buildTableGeneratorAnnotation
具有因此,即使有点 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 ofname
,sequence-name
,initial-value
andallocation-size
attributes, but I see no reference tocatalog
orschema
i expected to see something analogous to method
getTable(Element tree, XMLContext.Default defaults)
(of the same class) which hasor
buildTableGeneratorAnnotation
which hasSo, even if a little hackish, the way around -for this version at least- seems to be prefixing the
sequenceName
as you say.我在 postgresql 9.6 中通过仅在序列名称之前添加我的模式解决了这个问题,如下所示:
及其工作原理。查看代码:
查看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:
And its works. See the code:
See the "sisbp" before the sequenceName. The sequenceName was "seq_area_tematica" and now is "sisbp" (schema) + "seq_area_tematica".
在这里使用 Hibernate
4.2.0.Final
也有同样的问题。看起来这是一个错误,就像其他用户回答的那样。我想对序列使用动态架构,具体取决于会话的架构集,但对于某些序列,我想使用public
架构。因此,对我来说,我必须使用您提出的解决方案:将模式名称放在我想要使用特定模式的序列名称上:对于我想使用会话模式集的情况,我使用了
sequenceName< /code> 不带模式前缀。
对于那些希望所有序列具有相同架构的人,您可以使用
hibernate.default_schema
属性。这样,您就不需要更改@SequenceGenerator
属性:我正在使用 PostgreSQL DBMS。如果您想在 Hibernate 调用 nextval('my_sequence') 时动态更改序列名称,您可以扩展数据库方言类并配置 Hibernate 使用。您只需重写 getSequenceNextValString() 方法即可。提供给该方法的唯一信息是
@SequenceGenerator
上定义的sequenceName
属性:我没有使用最后一种方法来更改序列的名称,因为它似乎不太合适就我的情况而言。
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 usepublic
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: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: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 overridegetSequenceNextValString()
method. The only information provided to the method is thesequenceName
property defined on@SequenceGenerator
:I didn't use this last way to change the name of the sequences because it seems less appropriated to my case.
这听起来像是一个错误: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
andcatalog
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.同样的问题,在 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
嗯,我不太了解 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
尝试将 SequenceGenerator 注释从 POJO 的类声明移动到 id 字段声明。对于 Hibernate 3.6.4,这
会为 MySQL 生成此值
,为 PostgreSQL 生成此值
Try moving the SequenceGenerator annotation from the POJO's class declaration to id field declaration. With Hibernate 3.6.4 this
produces this for MySQL
and this for PostgreSQL
就我而言,执行不会经过@jambriz 指示的位置。
版本:Hibernate 3.6.10.Final
当
persistence.xml
中的hibernate.id.new_generator_mappings = false
时会出现此问题。当设置为 true 或简单删除时,Hibernate 将使用序列方案来构建 SQL。 解决方案:代码中的问题出在哪里?
在 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
inpersistence.xml
. When set totrue
or simply remove, the sequence scheme is used by Hibernate to build SQL. Solution: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!
当使用 hibernate 和 spring 时
请设置
when using hibernate with spring
please set
我在我的应用程序中遇到了同样的问题,该应用程序在 Oracle Database 19c 上使用 Hibernate 5.2.7 和 Spring 4.3.6。将sequenceName =“MY_SEQUENCE”更改为sequenceName =“MY_SCHEMA.MY_SEQUENCE”效果很好。
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.
您好,我遇到了同样的问题
,但将您的 hibernate.hbm2ddl.auto 设置为更新并运行。
Hello i was having same problem
but set your hibernate.hbm2ddl.auto to update and run.