Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate schema parameter doesn't work in @SequenceGenerator annotation

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?

like image 412
tabdulin Avatar asked Apr 09 '10 09:04

tabdulin


People also ask

What is @SequenceGenerator in Hibernate?

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.

What is sequenceName in @SequenceGenerator?

sequenceName is the name of the sequence in the DB. This is how you specify a sequence that already exists in the DB.

What is allocationSize in @SequenceGenerator?

allocationSize. (Optional) The amount to increment by when allocating sequence numbers from the sequence.

What is the use of @SequenceGenerator?

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.


2 Answers

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.

like image 50
jambriz Avatar answered Nov 13 '22 12:11

jambriz


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)
like image 7
Vlad Avatar answered Nov 13 '22 14:11

Vlad