JPA non-entity not mapped error with select-new syntax












0















I'm using the select new syntax to query a non-entity in the database and get a not mapped error...



The object (not an Entity):



package user;

public class User {
private int id;
private String lastName;
private String firstName;
private String mail;
}


The table (P042USER) is created like this:



CREATE TABLE P042USER (id INT PRIMARY KEY NOT NULL, lastname VARCHAR(100), firstname VARCHAR(100), email VARCHAR(255));


Then i want to do a request with the select new syntax:



Query q = em.createQuery("SELECT NEW user.User(u.id, u.lastname, u.firstname, u.email) FROM P042USER u ORDER BY u.id");
return q.getResultList();


Finally i got the error:



Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: P042USER is not mapped 


Of course it is not mapped, that's why I use the select new syntax... Have you got any idea? Thanks.










share|improve this question



























    0















    I'm using the select new syntax to query a non-entity in the database and get a not mapped error...



    The object (not an Entity):



    package user;

    public class User {
    private int id;
    private String lastName;
    private String firstName;
    private String mail;
    }


    The table (P042USER) is created like this:



    CREATE TABLE P042USER (id INT PRIMARY KEY NOT NULL, lastname VARCHAR(100), firstname VARCHAR(100), email VARCHAR(255));


    Then i want to do a request with the select new syntax:



    Query q = em.createQuery("SELECT NEW user.User(u.id, u.lastname, u.firstname, u.email) FROM P042USER u ORDER BY u.id");
    return q.getResultList();


    Finally i got the error:



    Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: P042USER is not mapped 


    Of course it is not mapped, that's why I use the select new syntax... Have you got any idea? Thanks.










    share|improve this question

























      0












      0








      0








      I'm using the select new syntax to query a non-entity in the database and get a not mapped error...



      The object (not an Entity):



      package user;

      public class User {
      private int id;
      private String lastName;
      private String firstName;
      private String mail;
      }


      The table (P042USER) is created like this:



      CREATE TABLE P042USER (id INT PRIMARY KEY NOT NULL, lastname VARCHAR(100), firstname VARCHAR(100), email VARCHAR(255));


      Then i want to do a request with the select new syntax:



      Query q = em.createQuery("SELECT NEW user.User(u.id, u.lastname, u.firstname, u.email) FROM P042USER u ORDER BY u.id");
      return q.getResultList();


      Finally i got the error:



      Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: P042USER is not mapped 


      Of course it is not mapped, that's why I use the select new syntax... Have you got any idea? Thanks.










      share|improve this question














      I'm using the select new syntax to query a non-entity in the database and get a not mapped error...



      The object (not an Entity):



      package user;

      public class User {
      private int id;
      private String lastName;
      private String firstName;
      private String mail;
      }


      The table (P042USER) is created like this:



      CREATE TABLE P042USER (id INT PRIMARY KEY NOT NULL, lastname VARCHAR(100), firstname VARCHAR(100), email VARCHAR(255));


      Then i want to do a request with the select new syntax:



      Query q = em.createQuery("SELECT NEW user.User(u.id, u.lastname, u.firstname, u.email) FROM P042USER u ORDER BY u.id");
      return q.getResultList();


      Finally i got the error:



      Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: P042USER is not mapped 


      Of course it is not mapped, that's why I use the select new syntax... Have you got any idea? Thanks.







      jpa jpql






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 27 '15 at 14:07









      Jerome VDLJerome VDL

      1,63232229




      1,63232229
























          2 Answers
          2






          active

          oldest

          votes


















          1














          In JPQL you work with entities. In your case what you need is a native query and SqlResultSetMapping. Check this ConstructorResult JavaDoc for an example.






          share|improve this answer
























          • I do not have an Entity, so where do I put the annotation @SqlResultSetMapping ? By the way, it seems very complicated. I've seen many references where select new seems to work... When i use createNativeQuery instead of createQuery, i have another error: Caused by: org.h2.jdbc.JdbcSQLException: Column "NEW" not found;

            – Jerome VDL
            Oct 27 '15 at 14:48













          • It is true that SqlResultSetMapping may only be used on an Entity, however it may be used on any Entity at all, so you just need to place it at an arbitrary entity within your persistence context.

            – Petros Splinakis
            Oct 27 '15 at 16:05



















          0














          Alternatively, you can modify your User class to be an entity (annotated by @Entity) that can be mapped to a specific table other than your P042USER table. For example, you map it to table named ANOTHER_USER. When you create new User entities, they can be persisted to your ANOTHER_USER. At the same time, when you execute native SQL queries against your P042USER table, results of your native SQL can be mapped to the User class.



          A sample of this implementation can be seen in my github repo.






          share|improve this answer
























          • The best solution indeed would be to have entities for sure but it is quite a hard work today because there are nearly hundred objects to map (moreover table names and column names don't match object names and fields). No one has a solution with the select new syntax ??

            – Jerome VDL
            Oct 28 '15 at 7:33











          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%2f33370190%2fjpa-non-entity-not-mapped-error-with-select-new-syntax%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          In JPQL you work with entities. In your case what you need is a native query and SqlResultSetMapping. Check this ConstructorResult JavaDoc for an example.






          share|improve this answer
























          • I do not have an Entity, so where do I put the annotation @SqlResultSetMapping ? By the way, it seems very complicated. I've seen many references where select new seems to work... When i use createNativeQuery instead of createQuery, i have another error: Caused by: org.h2.jdbc.JdbcSQLException: Column "NEW" not found;

            – Jerome VDL
            Oct 27 '15 at 14:48













          • It is true that SqlResultSetMapping may only be used on an Entity, however it may be used on any Entity at all, so you just need to place it at an arbitrary entity within your persistence context.

            – Petros Splinakis
            Oct 27 '15 at 16:05
















          1














          In JPQL you work with entities. In your case what you need is a native query and SqlResultSetMapping. Check this ConstructorResult JavaDoc for an example.






          share|improve this answer
























          • I do not have an Entity, so where do I put the annotation @SqlResultSetMapping ? By the way, it seems very complicated. I've seen many references where select new seems to work... When i use createNativeQuery instead of createQuery, i have another error: Caused by: org.h2.jdbc.JdbcSQLException: Column "NEW" not found;

            – Jerome VDL
            Oct 27 '15 at 14:48













          • It is true that SqlResultSetMapping may only be used on an Entity, however it may be used on any Entity at all, so you just need to place it at an arbitrary entity within your persistence context.

            – Petros Splinakis
            Oct 27 '15 at 16:05














          1












          1








          1







          In JPQL you work with entities. In your case what you need is a native query and SqlResultSetMapping. Check this ConstructorResult JavaDoc for an example.






          share|improve this answer













          In JPQL you work with entities. In your case what you need is a native query and SqlResultSetMapping. Check this ConstructorResult JavaDoc for an example.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 27 '15 at 14:22









          Petros SplinakisPetros Splinakis

          47524




          47524













          • I do not have an Entity, so where do I put the annotation @SqlResultSetMapping ? By the way, it seems very complicated. I've seen many references where select new seems to work... When i use createNativeQuery instead of createQuery, i have another error: Caused by: org.h2.jdbc.JdbcSQLException: Column "NEW" not found;

            – Jerome VDL
            Oct 27 '15 at 14:48













          • It is true that SqlResultSetMapping may only be used on an Entity, however it may be used on any Entity at all, so you just need to place it at an arbitrary entity within your persistence context.

            – Petros Splinakis
            Oct 27 '15 at 16:05



















          • I do not have an Entity, so where do I put the annotation @SqlResultSetMapping ? By the way, it seems very complicated. I've seen many references where select new seems to work... When i use createNativeQuery instead of createQuery, i have another error: Caused by: org.h2.jdbc.JdbcSQLException: Column "NEW" not found;

            – Jerome VDL
            Oct 27 '15 at 14:48













          • It is true that SqlResultSetMapping may only be used on an Entity, however it may be used on any Entity at all, so you just need to place it at an arbitrary entity within your persistence context.

            – Petros Splinakis
            Oct 27 '15 at 16:05

















          I do not have an Entity, so where do I put the annotation @SqlResultSetMapping ? By the way, it seems very complicated. I've seen many references where select new seems to work... When i use createNativeQuery instead of createQuery, i have another error: Caused by: org.h2.jdbc.JdbcSQLException: Column "NEW" not found;

          – Jerome VDL
          Oct 27 '15 at 14:48







          I do not have an Entity, so where do I put the annotation @SqlResultSetMapping ? By the way, it seems very complicated. I've seen many references where select new seems to work... When i use createNativeQuery instead of createQuery, i have another error: Caused by: org.h2.jdbc.JdbcSQLException: Column "NEW" not found;

          – Jerome VDL
          Oct 27 '15 at 14:48















          It is true that SqlResultSetMapping may only be used on an Entity, however it may be used on any Entity at all, so you just need to place it at an arbitrary entity within your persistence context.

          – Petros Splinakis
          Oct 27 '15 at 16:05





          It is true that SqlResultSetMapping may only be used on an Entity, however it may be used on any Entity at all, so you just need to place it at an arbitrary entity within your persistence context.

          – Petros Splinakis
          Oct 27 '15 at 16:05













          0














          Alternatively, you can modify your User class to be an entity (annotated by @Entity) that can be mapped to a specific table other than your P042USER table. For example, you map it to table named ANOTHER_USER. When you create new User entities, they can be persisted to your ANOTHER_USER. At the same time, when you execute native SQL queries against your P042USER table, results of your native SQL can be mapped to the User class.



          A sample of this implementation can be seen in my github repo.






          share|improve this answer
























          • The best solution indeed would be to have entities for sure but it is quite a hard work today because there are nearly hundred objects to map (moreover table names and column names don't match object names and fields). No one has a solution with the select new syntax ??

            – Jerome VDL
            Oct 28 '15 at 7:33
















          0














          Alternatively, you can modify your User class to be an entity (annotated by @Entity) that can be mapped to a specific table other than your P042USER table. For example, you map it to table named ANOTHER_USER. When you create new User entities, they can be persisted to your ANOTHER_USER. At the same time, when you execute native SQL queries against your P042USER table, results of your native SQL can be mapped to the User class.



          A sample of this implementation can be seen in my github repo.






          share|improve this answer
























          • The best solution indeed would be to have entities for sure but it is quite a hard work today because there are nearly hundred objects to map (moreover table names and column names don't match object names and fields). No one has a solution with the select new syntax ??

            – Jerome VDL
            Oct 28 '15 at 7:33














          0












          0








          0







          Alternatively, you can modify your User class to be an entity (annotated by @Entity) that can be mapped to a specific table other than your P042USER table. For example, you map it to table named ANOTHER_USER. When you create new User entities, they can be persisted to your ANOTHER_USER. At the same time, when you execute native SQL queries against your P042USER table, results of your native SQL can be mapped to the User class.



          A sample of this implementation can be seen in my github repo.






          share|improve this answer













          Alternatively, you can modify your User class to be an entity (annotated by @Entity) that can be mapped to a specific table other than your P042USER table. For example, you map it to table named ANOTHER_USER. When you create new User entities, they can be persisted to your ANOTHER_USER. At the same time, when you execute native SQL queries against your P042USER table, results of your native SQL can be mapped to the User class.



          A sample of this implementation can be seen in my github repo.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 27 '15 at 17:06









          IshIsh

          2,6901917




          2,6901917













          • The best solution indeed would be to have entities for sure but it is quite a hard work today because there are nearly hundred objects to map (moreover table names and column names don't match object names and fields). No one has a solution with the select new syntax ??

            – Jerome VDL
            Oct 28 '15 at 7:33



















          • The best solution indeed would be to have entities for sure but it is quite a hard work today because there are nearly hundred objects to map (moreover table names and column names don't match object names and fields). No one has a solution with the select new syntax ??

            – Jerome VDL
            Oct 28 '15 at 7:33

















          The best solution indeed would be to have entities for sure but it is quite a hard work today because there are nearly hundred objects to map (moreover table names and column names don't match object names and fields). No one has a solution with the select new syntax ??

          – Jerome VDL
          Oct 28 '15 at 7:33





          The best solution indeed would be to have entities for sure but it is quite a hard work today because there are nearly hundred objects to map (moreover table names and column names don't match object names and fields). No one has a solution with the select new syntax ??

          – Jerome VDL
          Oct 28 '15 at 7:33


















          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%2f33370190%2fjpa-non-entity-not-mapped-error-with-select-new-syntax%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







          Popular posts from this blog

          List item for chat from Array inside array React Native

          Thiostrepton

          Caerphilly