Sequence Generator not available to child class (entity) in certain cases












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?










share|improve this question



























    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?










    share|improve this question

























      0












      0








      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?










      share|improve this question














      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?







      java hibernate java-annotations






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 3 at 19:27









      Mark NenadovMark Nenadov

      3,68721826




      3,68721826
























          1 Answer
          1






          active

          oldest

          votes


















          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...






          share|improve this answer
























          • 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 4 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 4 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 5 at 4: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 6 at 13:13












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54028614%2fsequence-generator-not-available-to-child-class-entity-in-certain-cases%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          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...






          share|improve this answer
























          • 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 4 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 4 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 5 at 4: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 6 at 13:13
















          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...






          share|improve this answer
























          • 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 4 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 4 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 5 at 4: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 6 at 13:13














          0












          0








          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...






          share|improve this answer













          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...







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 4 at 1:01









          Carlitos WayCarlitos Way

          2,1741322




          2,1741322













          • 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 4 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 4 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 5 at 4: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 6 at 13:13



















          • 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 4 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 4 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 5 at 4: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 6 at 13:13

















          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 4 at 16:24







          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 4 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 4 at 20:18







          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 4 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 5 at 4:10







          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 5 at 4: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 6 at 13:13





          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 6 at 13:13




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54028614%2fsequence-generator-not-available-to-child-class-entity-in-certain-cases%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown