简体   繁体   中英

Problems! Entity data are not fetched using data access object (dao) and JPA?

I have this Dao which get events according to the user id. It return the events object.

public enum Dao {
INSTANCE;
public List<EventsDetail> getevents(String userId) {
    EntityManager em = EMFService.get().createEntityManager();
    Query q = em.createQuery("select t from EventsDetail t where t.MemberId = :userId");
    q.setParameter("userId", userId);
    @SuppressWarnings("unchecked")
    List<EventsDetail> events = q.getResultList();
    return events;
  }
}

here i have this jsp page which call dao as i have import the dao class. here i have used try and catch to throw exception where i get null pointer exception.

<%
Dao dao = Dao.INSTANCE;
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
List<EventsDetail> events = new ArrayList();
if (user != null) {
  pageContext.setAttribute("user", user);
  events = dao.getevents(user.getUserId());
    %>
}....

 <% 
try{
for (EventsDetail event : events) {
    pageContext.setAttribute("title", event.Title());
    pageContext.setAttribute("place", event.getPlace());
    pageContext.setAttribute("category", event.Category());
    pageContext.setAttribute("cdate", event.Cdate());
 %>
            <div class="row">
                <span class="r1"><%=count%></span>
                <span class="r2">${fn:escapeXml(cdate)}</span>
                <span class="r2">${fn:escapeXml(place)}</span>
                <span class="r3">${fn:escapeXml(title)}</span>
                <span class="r4">${fn:escapeXml(category)}</span>

            </div>
 <% }  
          } catch (Exception e)
          {
            System.out.println("Exception:" + e);
          }
             %>

the problem is that i get just empty data from the datastore where data are already persent in the datastore.

I think the problem is with your getEvents method.

you can try doing the following:

public enum Dao {
INSTANCE;
public List<EventsDetail> getevents(String userId) {
    EntityManager em = EMFService.get().createEntityManager();
    Query q = em.createQuery("select t from EventsDetail t where t.MemberId = " + userId);
    q.setParameter("userId", userId);
    @SuppressWarnings("unchecked")
    List<EventsDetail> events = q.getResultList();
    return events;
  }
}

and make sure you are passing correct userId.

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