JPA Entity attribute names missing in generated JSON

后端 未结 3 1116
旧巷少年郎
旧巷少年郎 2021-01-28 04:34

I am using JPA and and Jackson is used for generating JSON

Emp.java

@Entity
@NamedQuery(name = \"Emp.findAll\", 
           query = \"select o.empNo, o.         


        
相关标签:
3条回答
  • 2021-01-28 04:45

    The difference is that in the query select o.empNo, o.empName from Emp o you select specific entity fields and the every result row is an Object[]. Thus that array of the specified properties.

    The second query select o from Emp o selects the entire entity with all its fields, and what you see is the JSON-marashalled entity.

    0 讨论(0)
  • 2021-01-28 04:52

    In the first case, your JPQL returns a List<Object[]>; each item in the list is an array containing the values of o.empNo and o.empName. Of course, when JSON is produced, it is produced as an array/list of items.

    In the second case, you get a List<Employee>, which each item being an Employee instance, so it is serialized like an object would be (list of attribute-values).

    0 讨论(0)
  • 2021-01-28 05:02

    NAmed Query select o.empNo, o.empName from Emp o will return List<Object[]> where as select o from Emp o will return List<Emp>, so accordingly json will be produced.

    You can change the query as below

    select new Emp(o.empNo, o.empName) from Emp o and have a constructor in your class accordingly.

    or try using

    select new Map(o.empNo as empNo , o.empName as empName) from Emp o

    0 讨论(0)
提交回复
热议问题