10

My situation is described here:

eclipse Duplicate generator named "ID_GENERATOR" defined in this persistence unit

However my question is different and the selected answer does not solve it :

"Is it valid to have multiple @SequenceGenerator with the same name even though it is used for this purpose Hibernate : How override an attribute from mapped super class ?"

If not valid, is there an alternative ?

Thank you very much for your answer.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
fabien7474
  • 16,300
  • 22
  • 96
  • 124
  • I think it's valid, because in the end of the day, this is just how hibernate will map the entity to the sequence that will generate the ID when it persist it in the DB. Oracle for example does not care which tables use which sequence, since the sequence itself is an independent entity. IMO this warning (or error) makes more sense depending on the DBMS you're using. IMO, I'd just disable the error warning in eclipse. – Leo Jan 14 '16 at 12:27
  • Thanks Leo. Your comment could be a valid answer. – fabien7474 Jan 14 '16 at 14:09

2 Answers2

4

According to section 11.1.48 SequenceGenerator Annotation of the JPA 2.1 specification:

The scope of the generator name is global to the persistence unit (across all generator types).

So you can't have duplicate generators.

If you try to add the following two entities:

@Entity(name = "Post")
public static class Post {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "pooled")
    @GenericGenerator(
        name = "pooled",
        strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
        parameters = {
            @Parameter(name = "sequence_name", value = "sequence"),
            @Parameter(name = "initial_value", value = "1"),
            @Parameter(name = "increment_size", value = "5"),
        }
    )
    private Long id;
}

@Entity(name = "Announcement")
public static class Announcement {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "pooled")
    @GenericGenerator(
            name = "pooled",
            strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
            parameters = {
                @Parameter(name = "sequence_name", value = "sequence"),
                @Parameter(name = "initial_value", value = "1"),
                @Parameter(name = "increment_size", value = "10"),
            }
    )
    private Long id;
}

Hibernate will generate the following error message:

Multiple references to database sequence [sequence] were encountered attempting to set conflicting values for 'increment size'. Found [10] and [5]

That's because the identifier generator is global and those two sequence configurations will be conflicting.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • thanks. So it not valid what alternatives can I use for my situation ? – fabien7474 Jan 25 '16 at 18:19
  • The only solution is to avoid duplicated generators. – Vlad Mihalcea Jan 25 '16 at 19:43
  • 1
    As described in my question, I have multiple sub-classes that inherit from an abstract class BaseEntity having the @Id defined there. What should I do if I want to generate unique Id sequence for each subclass ? – fabien7474 Jan 26 '16 at 08:31
  • 3
    Just move the @Id from base class into each subclass. – Vlad Mihalcea Jan 26 '16 at 08:51
  • 1
    Hi @VladMihalcea, If you change the value of the second generator's increment_size to 5, in which case both generators have the same parameters, then hibernate will show no error. – sify Nov 01 '19 at 07:06
1

I think it's valid, because in the end of the day, this is just how hibernate will map the entity to the sequence that will generate the ID when it persist it in the DB. Oracle for example does not care which tables use which sequence, since the sequence itself is an independent entity. IMO this warning (or error) makes more sense depending on the DBMS you're using. IMO, I'd just disable the error warning in eclipse.

Leo
  • 751
  • 4
  • 29
  • 2
    This is no longer valid after Hibernate 5.13. Hibernate itself will throw an exception on application startup. – Czar Apr 03 '18 at 08:34