0

I've successfully defined a sequence generator via annotations in an inheritance relationship roughly like so:

@MappedSuperclass
public class DomainObject {
    @Id
    @Column( columnDefinition = "serial" )
    @GeneratedValue( generator = "id_sequence", strategy = GenerationType.IDENTITY )
    private long id = 0;
}

@Entity
@Table( name = "user" )
@SequenceGenerator( name = "id_sequence", sequenceName = "user_id_seq" )
public class User extends DomainObject {
}

In this example, the User class's sequence generator finds id_sequence from the Generated value annotation in DomainObject.

However, if I make DomainObject an abstract class and place it in another dependency (everything else about it remains the same), I get an exception:

org.hibernate.AnnotationException: Unknown Id.generator: id_sequence

In the changed version, the DomainObject dependency is in the @ComponentScan path, so I'm unsure as to why this isn't working. Any thoughts?

Mark Nenadov
  • 6,421
  • 5
  • 24
  • 32

1 Answers1

0

Why don't you do this:

@MappedSuperclass
public abstract class DomainObject {
   @Id
   @Column( columnDefinition = "serial" )
   @SequenceGenerator( name = "id_sequence", sequenceName = "user_id_seq" )
   @GeneratedValue( generator = "id_sequence", strategy = GenerationType.IDENTITY )
   private long id = 0;

}

@Entity
@Table( name = "user" )
public class User extends DomainObject {
}

I have done this configuration in other projects with success ... about not finding the annotated classes, be sure that DomainObject is in the class path and User Entity is really detected by spring...

Carlitos Way
  • 3,279
  • 20
  • 30
  • Because I don't want to use the same sequence for all the children of DomainObject. "User" is just one example of that. So essentially, I want to put as much as I can in DomainObject and have sequenceName = "user_id_seq" defined in the child. – Mark Nenadov Jan 04 '19 at 16:24
  • I believe is not possible ... for that you need to move @Id declaration (including its associated attribute, i.e. private long id;) to every child class ... then, you can define the sequence that you want – Carlitos Way Jan 04 '19 at 20:18
  • That is what I originally assumed. However....the question that I'm mulling over is this: why, then, does the sequence stuff WORK in the first code snippet I gave (where it is in a concrete parent class in the same package) and NOT WORK not in the second case (where I make the parent class abstract and have it reside in a seperate dependency)? It seems the SequenceGenerator can indeed be in the child and the generatedValue in the parent as long as the parent is not abstract and in the same package. – Mark Nenadov Jan 05 '19 at 04:10
  • In the case that does work, DomainObject and User shared the same package space, even each class is declared in different artefacts (dependency)? – Carlitos Way Jan 06 '19 at 13:13