4

I'm trying to create a @GenericGenerator annotation at the package level so it can be used by all the entities in the package.

1) I have a package-info.java class with the annotation:

@org.hibernate.annotations.GenericGenerator(name = "ID_GENERATOR",                  strategy = "enhanced-sequence", parameters = {
    @org.hibernate.annotations.Parameter(name = "sequence_name", value = "JPWH_SEQUENCE"),
    @org.hibernate.annotations.Parameter(name = "initial_value", value = "1000") })
package com;

2) In that same package, I have an entity with the following attribute:

@Id
@GeneratedValue(generator = "ID_GENERATOR")
private Long id;

this is resulting in an exception "Unknown Id.generator: ID_GENERATOR". If I include the @GenericGenerator annotation in the entity class, it works fine. However, I want to move this to the package level so I can reuse it in other entities.

Any ideas where the disconnect could be?

Thanks!

john
  • 41
  • 1
  • 2

3 Answers3

5

you can change the entity code to this:

@Id
@SequenceGenerator(name="my_id_generator", sequenceName="ID_GENERATOR")
@GeneratedValue(generator="my_id_generator")
private Long id;

source: link

Alex Torson
  • 289
  • 4
  • 13
1

I think that you make a design mistake. If you want a common thing between your model classes, use inheritance. You can create a AbstractEntity class which defines the id generator. And every classes of your model inherite from this abtract. It is a common pattern. You can also put the traditional common attributes created, createdBy, updated, updatedBy and why not an sysname. Using an hibernate id generator does not really have sense.

Bye

0

I put the package-info file in a package and then populated it with sub-packages as so:

org.persistence
    package-info
org.persistence.someclasses
    PersistenceClass
org.persistennce.otherclasess
    OtherPersistenceClass

This worked with no other action for some hours. Then I added, changed and deleted some values in the cfg.file and this worked no more, until I added the line <mapping package="org.persistence"/>

Then it worked again. I found that answer here:

Using @GenericGenerator in a package level annotation

Martin
  • 23
  • 1
  • 4