gpt4 book ai didi

java - 将 Morphia 与 Java EE 结合使用

转载 作者:太空宇宙 更新时间:2023-11-04 14:31:10 25 4
gpt4 key购买 nike

我正在尝试将 Morphia 用作我使用 Java EE 创建的 Web 服务的一部分。

我阅读了关于 DAO 支持的吗啡文档,其中写道:

In a web application environment, we would probably use a dependency injection framework (like Guice or Spring) to inject the dependencies into the DAO, and then inject the DAO into a controller, so the controller would never directly deal with the gritty details.

因此,我有一个标有 @Stateless 注释的 EJB(以便我可以在需要时注入(inject)),它扩展了 morhpia 提供的 BasicDAO ,如下所示:

@Stateless
public class PlayerDAO extends BasicDAO<Player, ObjectId>{

@EJB
ConnectionFactory factory;

public PlayerDAO(){};

public PlayerDAO(Morphia morphia, MongoClient mongo){
super(mongo, morphia, "testdb");
}
}

我的问题是我需要提供采用 MorphiaMongoClient 参数并调用 super 构造函数的构造函数,这意味着我还需要提供一个无参数构造函数来满足 Java EE 的要求。

当我添加此构造函数时,NetBeans 显示错误:

public PlayerDAO(){};
constructor BasicDAO.BasicDAO(Datastore) is not applicable
(actual and formal argument lists differ in length)
constructor BasicDAO.BasicDAO(MongoClient,Morphia,String) is not applicable
(actual and formal argument lists differ in length)
constructor BasicDAO.BasicDAO(Class,Datastore) is not applicable
(actual and formal argument lists differ in length)
constructor BasicDAO.BasicDAO(Class,MongoClient,Morphia,String) is not applicable
(actual and formal argument lists differ in length)

有没有办法解决这个问题,或者我应该采取不同的方法来使用 MongoDB 作为 java ee web 服务的一部分?

最佳答案

这就是我们所做的,效果很好。下面的实现仅是 CDI(部署到 Tomcat),但也适用于 @Stateless beans。我们让 MongoMorphia 由工厂进行 @Produce 处理。 @QConfig 带注释的名称由生产者生成,该生产者从包含 MongoDB 数据库名称的类路径读取 .properties 文件。

public interface EntityDao<T, K> {
T findById(K id);
K save(T object);
T update(T object); // returns an up-to-date version of the entity
}

public class BaseDaoMorphiaImpl<T extends BaseEntity, K>
implements EntityDao<T, K>, Serializable {

@Inject
private @QConfig String mongoDbName;
@Inject
private @QMongo Instance<Mongo> mongo;
@Inject
private @QMorphia Instance<Morphia> morphia;

// BasicDAO is not serializeable; always use wrapper getBasicDao()
private transient BasicDAO<T, K> basicDao;
private Class clazz;

public BaseDaoMorphiaImpl(Class clazz) {
this.clazz = clazz;
}

@PostConstruct
public void init() {
setupBasicDao();
}

protected BasicDAO<T, K> getBasicDao() {
if (basicDao == null)
setupBasicDao();
return basicDao;
}

private void setupBasicDao() {
basicDao = new BasicDAO<T, K>(clazz, mongo.get(), morphia.get(), mongoDbName);
basicDao.ensureIndexes();
}

@Override
public T findById(K id) {
return getBasicDao().get(id);
}

@Override
public K save(T o) {
Key<T> key = getBasicDao().save(o);
return (K) key.getId();
}

@Override
public T update(T o) {
return findById((K) o.getId());
}

protected QueryResults<T> find( Query<T> query ) {
return getBasicDao().find(query);
}

protected T findOne( Query<T> query ) {
return getBasicDao().findOne(query);
}

protected Query<T> createQuery() {
return getBasicDao().getDatastore().createQuery(clazz);
}
}

示例:

public interface UserDao extends EntityDao<User, ObjectId> {
User findByEmail( String email );
User findByName( String name );
List<User> findAll();
}

public class UserDaoMorphiaImpl extends BaseDaoMorphiaImpl<User, ObjectId> implements UserDao {

@Inject
private Logger logger;

public UserDaoMorphiaImpl() {
super(User.class);
}

@Override
public User findByEmail(String email) {
Datastore ds = getBasicDao().getDatastore();
User res = ds.find(User.class).filter("email = ", email).get();
return res;
}

@Override
public User findByName(String name) {
Datastore ds = getBasicDao().getDatastore();
User res = ds.find(User.class).filter("name = ", name).get();
return res;
}

@Override
public List<User> findAll() {
return getBasicDao().find().asList();
}

@Override
public ObjectId save(User user) {
return super.save(user);
}
}

对于更具体的查询,您可以使用如下内容:

@Override
public Iterator<Document> fetchNewest( int start, int count ) {
return find(createQuery().order("-uploadDate").offset(start).limit(count)).iterator();
}

关于java - 将 Morphia 与 Java EE 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26123606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com