开发者

Why can't I use JPA, database and primefaces together?

I have a User class, whitch is a database @Entity for JPA.

I have a UserServiceImpl class, which have this getAllUser() method (from the Exception below: UserServiceImpl.java:16=the second row):

public UserServiceImpl(){
    em = EMService.getEntityManager();
}
@Override
@SuppressWarnings("unchecked")
public List<User> getAllUser() {
    String jpql = "SELECT u FROM User u";
    Query q = em.createQuery(jpql);
    List<User> u开发者_如何学运维sers = (List<User>) q.getSingleResult();
    return users;
}

I have a UserBean class, whitch is managed bean in the faces-config.xml for the primefaces. And there will be the Exception (UserBean.java:19=5th row):

private List<User> users;
private User selectedUser;
private UserServiceImpl userService;
public UserBean() {
    userService = new UserServiceImpl();
    users = userService.getAllUser();
}
public List<User> getUsers() {
    return users;
}

I think, the problem is that a managed bean can't use another classes. This is the Exception:

java.lang.NoClassDefFoundError: javax/persistence/Persistence
at hu.pte.admin.services.EMService.getEntityManager(EMService.java:12)
at hu.pte.admin.services.usersmodul.UserServiceImpl.<init>(UserServiceImpl.java:16)
at hu.pte.admin.managedbeans.UserBean.<init>(UserBean.java:19)

This is the method of the EMService class (the second row is the wrong = EMService.java:12):

public static EntityManager getEntityManager(){
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("Admin");
    EntityManager em = factory.createEntityManager();
    return em;
}

And this is the users.xhtml (detail), where i want to use the managed bean (userBean) in the primefaces code:

<p:dataTable var="user" value="#{userBean.users}" paginator="true" rows="10"
        selection="#{userBean.selectedUser}" selectionMode="single" 
        onRowSelectUpdate="display" rowsPerPageTemplate="5,10,15">
        <f:facet name="header">The Users</f:facet>

        <p:column sortBy="#{user.id}" filterBy="#{user.id}">
            <f:facet name="header">
                <h:outputText value="ID" />
            </f:facet>
            <h:outputText value="#{user.id}" />
        </p:column>

The persistance.xml has the code to connect to my database. Please help me, i can't find any sample in the internet, whitch are use JPA and PrimeFaces together like this! Thanks!!


The exception clearly shows you:

java.lang.NoClassDefFoundError: javax/persistence/Persistence

There is no class definition found for javax.persistence.Persistence class. I'm assuming you're running a Java SE SDK. I suggest downloading and running the Java EE 6 SDK which inlcudes the Persistence API.


Apart from the NoClassDefFoundError, there is also a problem in your getAllUsers method:

List<User> users = (List<User>) q.getSingleResult();

Calling getSingleResult will return a single User entity if the query result is unique, otherwise it will throw an exception. To retrieve a list of users you need to call getResultList instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜