简体   繁体   中英

JPQL Query returns object with no field names

I am using Dropwizard and Hibernate.

I got this JPQL query:

String queryString = "select u.portalUserId, p.personName, p.personMobile, p.personEmail, u.portalUsertype, p.personNotes "
                  + "FROM Persons p, PortalUsers u WHERE p.personId = u.portalUserPersonId";

This is a simple join between 2 entities and get some info on users.

The way I run it is like that:

Query q = sessionFactory.getCurrentSession().createQuery(queryString); 
List<PortalUserBasicUserInfo> l = q.list();

PortalUserBasicUserInfo is a class which has all the fields of the select clause.

When I run the query, I get only the values of each field but without the field names. For example, a result I receive is:

"[[2,\"\",null,null,\"MANAGER\",null]]"

This will not work since I want to return a JSON result with field names. How do I add the fields name so someone who is reading the json can parse it properly?

What I eventually did was changing to query to return the type I need:

String queryString = "select new com.myPackage.Users.PortalUserBasicUserInfo(u.portalUserId ,p.personName, p.personMobile, p.personEmail, u.portalUserStatus, u.portalUsertype, p.personNotes) "
                  + "FROM Persons p, PortalUsers u WHERE p.personId = u.portalUserPersonId";

Notice the new com.myPackage.Users.PortalUserBasicUserInfo . This has created a list of the correct type I expected it to be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM