Eclipselink NamedNativeQuery pass column name as parameter and not a valueNull or zero primary key encountered in unit of work cloneYou have attempted to set a value of type class entityOptional or Null Parameters JPAeclipselink.query-results-cache.ignore-null not caching any resultJPA downcasting with treatIN clause with a composite primary key in JPA criteriahow to pass positional parameters to createnativequery jpa javaJPA entity with join possibly on null keyEclipseLink - The primary key read from the row during the execution of the query was detected to be null. Primary keys must not contain nullEclipselink Discriminator Column Returns NULL

Why is the name Bergson pronounced like Berksonne?

Earliest evidence of objects intended for future archaeologists?

What allows us to use imaginary numbers?

What is bodily formation? Does it refer to the breath or the body?

Is recepted a word?

My new Acer Aspire 7 doesn't have a Legacy Boot option, what can I do to get it?

Can I submit a paper computer science conference using an alias if using my real name can cause legal trouble in my original country

Vegetarian dishes on Russian trains (European part)

Is there a way to make the "o" keypress of other-window <C-x><C-o> repeatable?

Indirect speech - breaking the rules of it

How to fix Sprinkles in rendering?

How could Tony Stark wield the Infinity Nano Gauntlet - at all?

Chess software to analyze games

9 hrs long transit in DEL

!I!n!s!e!r!t! !b!e!t!w!e!e!n!

Does git delete empty folders?

Atmospheric methane to carbon

Installing the original OS X version onto a Mac?

Why should I pay for an SSL certificate?

Levenshtein Neighbours

iPad or iPhone doesn't charge until unlocked?

What's the point of writing that I know will never be used or read?

Did Wernher von Braun really have a "Saturn V painted as the V2"?

Just one file echoed from an array of files



Eclipselink NamedNativeQuery pass column name as parameter and not a value


Null or zero primary key encountered in unit of work cloneYou have attempted to set a value of type class entityOptional or Null Parameters JPAeclipselink.query-results-cache.ignore-null not caching any resultJPA downcasting with treatIN clause with a composite primary key in JPA criteriahow to pass positional parameters to createnativequery jpa javaJPA entity with join possibly on null keyEclipseLink - The primary key read from the row during the execution of the query was detected to be null. Primary keys must not contain nullEclipselink Discriminator Column Returns NULL






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















Trying to pass column name as parameter but JPA sets it as a value surrounding it with single quotes.



@NamedNativeQueries(
@NamedNativeQuery(
name = "Genre.findAllLocalized",
query = "SELECT "
+ " CASE "
+ " WHEN ? IS NULL THEN genre_default"
+ " ELSE ? "
+ " END localized_genre "
+ "FROM genre ORDER BY localized_genre")
)


Then:



List<String> res = em.createNamedQuery("Genre.findAllLocalized")
.setParameter(1, colName)
.setParameter(2, colName)
.getResultList();


The problem is that the column names being passed are taken as values so the result will return result list with repeated values of "col_name" instead of selecting the value of the column passed as parameter.



Is this achievable?










share|improve this question






























    1















    Trying to pass column name as parameter but JPA sets it as a value surrounding it with single quotes.



    @NamedNativeQueries(
    @NamedNativeQuery(
    name = "Genre.findAllLocalized",
    query = "SELECT "
    + " CASE "
    + " WHEN ? IS NULL THEN genre_default"
    + " ELSE ? "
    + " END localized_genre "
    + "FROM genre ORDER BY localized_genre")
    )


    Then:



    List<String> res = em.createNamedQuery("Genre.findAllLocalized")
    .setParameter(1, colName)
    .setParameter(2, colName)
    .getResultList();


    The problem is that the column names being passed are taken as values so the result will return result list with repeated values of "col_name" instead of selecting the value of the column passed as parameter.



    Is this achievable?










    share|improve this question


























      1












      1








      1








      Trying to pass column name as parameter but JPA sets it as a value surrounding it with single quotes.



      @NamedNativeQueries(
      @NamedNativeQuery(
      name = "Genre.findAllLocalized",
      query = "SELECT "
      + " CASE "
      + " WHEN ? IS NULL THEN genre_default"
      + " ELSE ? "
      + " END localized_genre "
      + "FROM genre ORDER BY localized_genre")
      )


      Then:



      List<String> res = em.createNamedQuery("Genre.findAllLocalized")
      .setParameter(1, colName)
      .setParameter(2, colName)
      .getResultList();


      The problem is that the column names being passed are taken as values so the result will return result list with repeated values of "col_name" instead of selecting the value of the column passed as parameter.



      Is this achievable?










      share|improve this question














      Trying to pass column name as parameter but JPA sets it as a value surrounding it with single quotes.



      @NamedNativeQueries(
      @NamedNativeQuery(
      name = "Genre.findAllLocalized",
      query = "SELECT "
      + " CASE "
      + " WHEN ? IS NULL THEN genre_default"
      + " ELSE ? "
      + " END localized_genre "
      + "FROM genre ORDER BY localized_genre")
      )


      Then:



      List<String> res = em.createNamedQuery("Genre.findAllLocalized")
      .setParameter(1, colName)
      .setParameter(2, colName)
      .getResultList();


      The problem is that the column names being passed are taken as values so the result will return result list with repeated values of "col_name" instead of selecting the value of the column passed as parameter.



      Is this achievable?







      eclipselink jpa-2.1






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 27 at 13:50









      esteban rinconesteban rincon

      1,02511 silver badges29 bronze badges




      1,02511 silver badges29 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          1














          Basically it makes no sense to create a prepared query like this, how would you name that query anyway: "*"? So the short answer is: no.




          1. But you could create named queries dynamically if this matches your requirement:



            String colName = "colName";
            String query = "SELECT WHEN " + colName + " IS NULL THEN genre_default";
            Query query = entitymanager.createQuery(query);



          2. Probably using a criteria builder is more the way you want to use JPA (code from https://en.wikibooks.org/wiki/Java_Persistence/Criteria):



            // Select the employees and the mailing addresses that have the same address.
            CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
            CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
            Root employee = criteriaQuery.from(Employee.class);
            Root address = criteriaQuery.from(MailingAddress.class);
            criteriaQuery.multiselect(employee, address);

            criteriaQuery.where( criteriaBuilder.equal(employee.get("address"), address.get("address"));
            Query query = entityManager.createQuery(criteriaQuery);
            List<Object[]> result = query.getResultList();






          share|improve this answer
























            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%2f55378871%2feclipselink-namednativequery-pass-column-name-as-parameter-and-not-a-value%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









            1














            Basically it makes no sense to create a prepared query like this, how would you name that query anyway: "*"? So the short answer is: no.




            1. But you could create named queries dynamically if this matches your requirement:



              String colName = "colName";
              String query = "SELECT WHEN " + colName + " IS NULL THEN genre_default";
              Query query = entitymanager.createQuery(query);



            2. Probably using a criteria builder is more the way you want to use JPA (code from https://en.wikibooks.org/wiki/Java_Persistence/Criteria):



              // Select the employees and the mailing addresses that have the same address.
              CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
              CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
              Root employee = criteriaQuery.from(Employee.class);
              Root address = criteriaQuery.from(MailingAddress.class);
              criteriaQuery.multiselect(employee, address);

              criteriaQuery.where( criteriaBuilder.equal(employee.get("address"), address.get("address"));
              Query query = entityManager.createQuery(criteriaQuery);
              List<Object[]> result = query.getResultList();






            share|improve this answer





























              1














              Basically it makes no sense to create a prepared query like this, how would you name that query anyway: "*"? So the short answer is: no.




              1. But you could create named queries dynamically if this matches your requirement:



                String colName = "colName";
                String query = "SELECT WHEN " + colName + " IS NULL THEN genre_default";
                Query query = entitymanager.createQuery(query);



              2. Probably using a criteria builder is more the way you want to use JPA (code from https://en.wikibooks.org/wiki/Java_Persistence/Criteria):



                // Select the employees and the mailing addresses that have the same address.
                CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
                CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
                Root employee = criteriaQuery.from(Employee.class);
                Root address = criteriaQuery.from(MailingAddress.class);
                criteriaQuery.multiselect(employee, address);

                criteriaQuery.where( criteriaBuilder.equal(employee.get("address"), address.get("address"));
                Query query = entityManager.createQuery(criteriaQuery);
                List<Object[]> result = query.getResultList();






              share|improve this answer



























                1












                1








                1







                Basically it makes no sense to create a prepared query like this, how would you name that query anyway: "*"? So the short answer is: no.




                1. But you could create named queries dynamically if this matches your requirement:



                  String colName = "colName";
                  String query = "SELECT WHEN " + colName + " IS NULL THEN genre_default";
                  Query query = entitymanager.createQuery(query);



                2. Probably using a criteria builder is more the way you want to use JPA (code from https://en.wikibooks.org/wiki/Java_Persistence/Criteria):



                  // Select the employees and the mailing addresses that have the same address.
                  CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
                  CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
                  Root employee = criteriaQuery.from(Employee.class);
                  Root address = criteriaQuery.from(MailingAddress.class);
                  criteriaQuery.multiselect(employee, address);

                  criteriaQuery.where( criteriaBuilder.equal(employee.get("address"), address.get("address"));
                  Query query = entityManager.createQuery(criteriaQuery);
                  List<Object[]> result = query.getResultList();






                share|improve this answer













                Basically it makes no sense to create a prepared query like this, how would you name that query anyway: "*"? So the short answer is: no.




                1. But you could create named queries dynamically if this matches your requirement:



                  String colName = "colName";
                  String query = "SELECT WHEN " + colName + " IS NULL THEN genre_default";
                  Query query = entitymanager.createQuery(query);



                2. Probably using a criteria builder is more the way you want to use JPA (code from https://en.wikibooks.org/wiki/Java_Persistence/Criteria):



                  // Select the employees and the mailing addresses that have the same address.
                  CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
                  CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
                  Root employee = criteriaQuery.from(Employee.class);
                  Root address = criteriaQuery.from(MailingAddress.class);
                  criteriaQuery.multiselect(employee, address);

                  criteriaQuery.where( criteriaBuilder.equal(employee.get("address"), address.get("address"));
                  Query query = entityManager.createQuery(criteriaQuery);
                  List<Object[]> result = query.getResultList();







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 27 at 21:10









                Marvin Emil BrachMarvin Emil Brach

                3,5411 gold badge26 silver badges60 bronze badges




                3,5411 gold badge26 silver badges60 bronze badges





















                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















                    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%2f55378871%2feclipselink-namednativequery-pass-column-name-as-parameter-and-not-a-value%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

                    How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

                    155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

                    Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?