asList - No usable constructor

1,123 views
Skip to first unread message

Michal Majernik

unread,
May 12, 2013, 10:37:09 AM5/12/13
to mor...@googlegroups.com
Hi guys,

I am working on my diploma project, focused on crowdsourcing. I am using mongoDb and Morphia. I am getting an error and cannot find out why my code is not working. If I don't use asList and after that count items I got correct number. Just that list is not working.

in a servlet ServletMyTasks
HttpSession session = request.getSession(false);
User user = (User)session.getAttribute("user");
           
Query<UserTasks> q = userTasksDAO.getDatastore().createQuery(UserTasks.class);
q.field("user").equal(user);
q.offset((page-1)*tasksPerPage);
q.order("-created");
q.limit(tasksPerPage);
List<UserTasks> userTasks = userTasksDAO.find(q).asList(); //it crashes at asList - line 59

Entities
@Entity("userTasks")
public class UserTasks {
   
    @Id ObjectId id;
   
    @Reference private Task task;
    @Reference private User user;
   
    private String answer;
   
    private String status;
   
    private Date created;
   
    public UserTasks(Task task, User user, String answer, String status, Date created) {
        this.task = task;
        this.user = user;
        this.answer = answer;
        this.status = status;
        this.created = created;
    }

    // getters and setters
}

@Entity("tasks")
public class Task {
    @Id ObjectId id;
   
    @Reference private Dataset dataset;
   
    private String parameter;
   
    private int priority;
    private int limit;
    private int maxPeople;
   
    private String status; //finished, in progress

    public Task(Dataset dataset, String parameter, int priority, int limit, int maxPeople, String status) {
        this.dataset = dataset;
        this.parameter = parameter;
        this.priority = priority;
        this.limit = limit;
        this.maxPeople = maxPeople;
        this.status = status;
    }
    // getters and setters
}

@Entity("users")
public class User{
   
    @Id ObjectId id;
    private String username;
    private String password;
    private String email;
    private String role;
   
    private String name = null;
    private String surname = null;
   
    public User(String username, String password, String email, String name, String surname) {
        this.username = username;
        this.setPassword(password);
        this.email = email;
        this.role = "user";
        this.name = name;
        this.surname = surname;
    }

    // getters and setters
}


Error
java.lang.RuntimeException: com.google.code.morphia.mapping.MappingException: No usable constructor for entities.UserTasks
	com.google.code.morphia.mapping.DefaultCreator.createInst(DefaultCreator.java:119)
	com.google.code.morphia.mapping.DefaultCreator.createInstance(DefaultCreator.java:31)
	com.google.code.morphia.mapping.DefaultCreator.createInstance(DefaultCreator.java:41)
	com.google.code.morphia.mapping.Mapper.fromDBObject(Mapper.java:266)
	com.google.code.morphia.query.MorphiaIterator.processItem(MorphiaIterator.java:53)
	com.google.code.morphia.query.MorphiaIterator.next(MorphiaIterator.java:48)
	com.google.code.morphia.query.QueryImpl.asList(QueryImpl.java:234)
	servlet.ServletMyTasks.doGet(ServletMyTasks.java:59)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


I will be very grateful for any help.

Thank you.

Miso


Christian Autermann

unread,
May 12, 2013, 7:42:39 PM5/12/13
to mor...@googlegroups.com
The error just says that you shold provide a default constructor (a constructor without parameters) for your entities as morphia (correct me if I'am wrong) will create a empty object and will set the annotated properties using reflection and can't figure out the parameters for a contructor that is parameterized. The same applies for the contructor for Task and User…
Reply all
Reply to author
Forward
0 new messages