如何将 Morphia 与 Java / GWT 结合使用
我正在尝试使用 MongoDB 和 Morphia 作为我的后端数据库,我实现了一个实用程序类来简化对数据库的访问。 我收到“很多异常:
我实现了基本的添加用户功能,但是当我放置时,
java.lang.IndexOutOfBoundsException
”异常查询 query = datastore.createQuery(User.class).filter("name = ", 用户名);
用于在提交之前检查用户。
删除后:我收到这两个异常:
java.lang.RuntimeException: java.lang.NumberFormatException:
如何解决此问题?
以下是我的项目代码:
MorphiaUtil.java:
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;
}
}
UserServiceImpl.java
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);
}
}
}
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:
java.lang.IndexOutOfBoundsException
exception when I put
Query query = datastore.createQuery(User.class).filter("name = ",
username);
for checking user before comitting.
When removed: I get these two exceptions:
java.lang.RuntimeException: java.lang.NumberFormatException:
How to fix this issue?
Here are the code I have for the project:
MorphiaUtil.java:
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;
}
}
UserServiceImpl.java
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我创建了所有 Bean 的服务器版本,在调用保存方法之前,我从 Simple Beans(在客户端使用)转换为 MorphiaBeans(仅用于吗啡操作)。
这不是解决此问题的最佳方法,但对我来说效果很好!
I created a server version of all my beans, and before calling saving methods, i convert from Simple Beans (used in client side) to MorphiaBeans (used only for morphia operations).
That's not the best method to fix this issue, but works fine for me!