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?
We all know the default behaviour of Hibernate when using @SequenceGenerator - it increases real database sequence by one, multiple this value by 50 (default allocationSize value) - and then uses this value as entity ID.
sequenceName is the name of the sequence in the DB. This is how you specify a sequence that already exists in the DB.
allocationSize. (Optional) The amount to increment by when allocating sequence numbers from the sequence.
Annotation Type SequenceGenerator. Defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation. A sequence generator may be specified on the entity class or on the primary key field or property.
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-size
attributes, but I see no reference to catalog
or 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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With