如何将 Morphia 与 Java / GWT 结合使用

发布于 2024-11-19 11:31:48 字数 1945 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彩虹直至黑白 2024-11-26 11:31:48

我创建了所有 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文