Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use Morphia with Java / GWT
    text
    copied!<p>I am trying to use MongoDB with Morphia as my back-end DB, I have implemented a utility class to simplify access to the database. I implemented basic add user function with However I am getting `lots of exceptions: </p> <p><code>java.lang.IndexOutOfBoundsException</code> exception when I put </p> <blockquote> <p>Query query = datastore.createQuery(User.class).filter("name = ", username);</p> </blockquote> <p>for checking user before comitting.</p> <p>When removed: I get these two exceptions:</p> <pre><code>java.lang.RuntimeException: java.lang.NumberFormatException: </code></pre> <p>How to fix this issue? </p> <p>Here are the code I have for the project:</p> <p>MorphiaUtil.java:</p> <pre><code>public class MorphiaUtil { protected final Log log = LogFactory.getLog(getClass()); private static Mongo mongo; private static Datastore datastore; static { try { // Create the database connection mongo = new Mongo("localhost"); datastore = new Morphia().createDatastore(mongo, "mygwtapp"); } catch (UnknownHostException e) { System.err.println("Caught Unknown host exception:"+e); e.printStackTrace(); } catch (MongoException e) { System.err.println("Initial Datastore creation failed:"+e); e.printStackTrace(); } } public static Datastore getDatastore() { return datastore; } } </code></pre> <p>UserServiceImpl.java</p> <pre><code>public class UserServiceImpl extends RemoteServiceServlet implements UserService { @Override public void addUser(String username, String password) throws IllegalArgumentException { try { Datastore datastore = MorphiaUtil.getDatastore(); Query query = datastore.createQuery(User.class).filter("name = ", username); User user = (User) query.asList().get(0); if (user == null) { user = new User(username, password); datastore.save(user); } } catch (Exception e) { System.err.print("Caught exception:"+e); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload