javajdodatanucleus

javax.jdo.JDOUserException: Persistent class "ChatUsers" has no table in the database


I am doing retrieving data from h2 database. my problem is in q.setClass(). here I am trying to set the table chatUsers.class, the database has the same class But this is showing that there are no Table ChatUsers. So what can I do for that??

If i do not write q.setClass(). then it is giving data but there problem is that then i could not able to get the List of that class.

ChatUser class

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import java.util.List;
import java.util.Properties;

@PersistenceCapable(detachable="true")
public class ChatUsers {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.INCREMENT)
    int id;
    String user;
    String mobileNo;
    String email;
    String password;

    public ChatUsers(String user, String password, String mobileNo, String email) {
        this.user = user;
        this.mobileNo = mobileNo;
        this.email = email;
        this.password = password;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getMobileNo() {
        return mobileNo;
    }

    public void setMobileNo(String mobileNo) {
        this.mobileNo = mobileNo;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

Retrieving Logic

public static List<ChatUsers> getAllUsersList() {
        PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(setProperties());
        PersistenceManager pm = pmf.getPersistenceManager();
        List<ChatUsers> chatUsersList = null;
        pm.currentTransaction().begin();
        try {
            Query q = pm.newQuery("javax.jdo.query.SQL","SELECT * FROM `CHATUSERS`");
            q.setClass(ChatUsers.class);//This line has error
             chatUsersList = (List<ChatUsers>) q.execute();
        }catch (Exception ex)
        {
            System.out.println(ex);
        }

        return chatUsersList;
    }

Error code

javax.jdo.JDOUserException: Persistent class "ChatUsers" has no table in the database, but the operation requires it. Please check the specification of the MetaData for this class.
NestedThrowables:
org.datanucleus.store.rdbms.exceptions.NoTableManagedException: Persistent class "ChatUsers" has no table in the database, but the operation requires it. Please check the specification of the MetaData for this class.


Solution

  • When you execute() the query, you should use the executeList() instead. [1]

    A different solution could be using setResultClass() instead of setClass(). [1]

    An example for the first solution would be:

        Query query = pm.newQuery("javax.jdo.query.SQL", "SELECT MY_ID, MY_NAME FROM MYTABLE");
    query.setClass(MyClass.class);
    List<MyClass> results = query.executeList();
    

    [1] Search for "Setting candidate class " and "Defining a result type " in the documentation URL: http://www.datanucleus.org/products/accessplatform/jdo/query.html