5

I am using bean property ro wmapper to match my DB column names with variables to do select * from table to data class. But if the column name is something like this which has number after underscore:

WEEK_7DAYS, I have property name in my POJO as week7days. but that is not matching and no values ar set to week&days. I tried week7Days too. That is also not working. how to match it. Please help.

Thanks, Sreenivas

user1918641
  • 135
  • 2
  • 11
  • But then i have to write every single field in my select. i have around 40 fields. so, I want to do just select * from TABLE_NAME – user1918641 Feb 15 '14 at 08:20

3 Answers3

3

Specifically, this scenario deals with underscores in front of numbers..

Fail condition: Example: WEEK_7DAYS -> getWeek7Days()

Strangely, spring attempts to ADD IN the underscores of camelCased method signatures to derive the key in the result set.

In your case, Spring would guess that getWeek7Days() would translate to something like WEEK7_DAYS, and never in any case would it try to place an underscore in front of a number.

If you want your method to be handled properly, you have to retain any underscores that are proceeded by a number.

your successful method name would be getWeek_7days()

rheaghen
  • 31
  • 5
2

Use aliases in your query:

select t.id, t.WEEK_7DAYS as week7days from mytable t
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • But then i have to write every single field in my select. i have around 40 fields. so, I want to do just select * from TABLE_NAME – user1918641 Feb 15 '14 at 08:20
  • 2
    You can't have everything for free. Spring can't magically know that your intention is that the WEEK_7DAYS column maps to the week7days field. So you have to tell him somehow. – JB Nizet Feb 15 '14 at 11:12
  • @user1918641: Doing SELECT * is generally a bad idea. See: http://stackoverflow.com/questions/3639861/why-is-select-considered-harmful – loneboat Nov 26 '14 at 21:18
0

I suggest you alter the table field name as WEEK_7_DAYS or alter the property name in the POJO as week7days. Be careful of the lower case and the upper case.

chaser
  • 46
  • 1
  • 6