Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8099769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:31:11+00:00 2026-06-05T22:31:11+00:00

I have a table say MyTable (myId,myStringCol1,myStringCol2,myStringCol3) When Using QUERY1 hibernateTemplate.find(SELECT new(myId,myStringCol1) from MyTable)

  • 0

I have a table say

MyTable (myId,myStringCol1,myStringCol2,myStringCol3)

When Using

QUERY1

hibernateTemplate.find("SELECT new(myId,myStringCol1) from MyTable")

I am getting the MyTable Object and for this there should be one constructor in the MyTable class like this

MyTable{
      MyTable(Integer id,String col1){
          this.id = id;
          this.col1 = col1;
       }

}

But when in another method I am calling like this

QUERY2

hibernateTemplate.find("SELECT new(myId,myStringCol2) from MyTable")  ///myStringCol2

Here, there also be one constructor, like above, but we can’t create the same signature constructor.
So when running the second query (QUERY2) I am getting the myStringCol2 values in col1
because of the first constructor.

So how can I solve this problem..

Waiting for the suggestions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:31:13+00:00Added an answer on June 5, 2026 at 10:31 pm

    How about this:

    class MyTable1{
          MyTable1(Integer id,String col1, String col2){
              this.id = id;
              this.col1 = col1;
              this.col2 = col2;
           }
    
    }
    

    And then:

    Q1

    hibernateTemplate.find("SELECT new(myId,myStringCol1,'') from MyTable")
    

    Q2

    hibernateTemplate.find("SELECT new(myId,'',myStringCol2) from MyTable") 
    

    Would be nice if you could pass null instead of ”, but AFAIK you can not, because there is a bug that prevents you from doing that: https://forum.hibernate.org/viewtopic.php?f=1&t=985612&start=0. So, you will have to use ”

    Another option, inspired by Talha answer would be:

    class MyTable{
          MyTable(Integer id,String col1, Boolean isColumn2){
              this.id = id;
              if(isColumn2){
                 this.col2 = col2;
              }
              else{
                 this.col1 = col1;
              }
          }      
    }
    

    Then you can just write:

    Q1

    hibernateTemplate.find("SELECT new(myId,myStringCol1,false) from MyTable")
    

    Q2

    hibernateTemplate.find("SELECT new(myId,myStringCol2,true) from MyTable") 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a table which I query like so: select date, value from
Say I have this query: SELECT name::text, id::int, pass:bool FROM mytable; which gives me
I am new to oracle. I have a table referred using a dblink, say
i have one table in my database say mytable, which contents request coming from
Say I have a table: <table id=#myTable> <tr> Section 1 <td> <table> <tr> Sub
I have this problem where after a field (say Field3 in table MyTable) is
I have a numeric field (say num) in table along with pkey. select *
Okay, say i have a table with the field code. The query select code
lets say I have create table mytable( a VARCHAR(200) b VARCHAR(200) c VARCHAR(200) )
Let's say I have a table with 15 columns called MyTable and an UPDATE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.