2014-09-10 116 views
-2

确实DataNucleus将JPA对MongoDB的JPA原生查询的MongoDB

例如支持:

entityManager.createNativeQuery("db.Movie.find()"); 
+1

http://www.datanucleus.org/products/accessplatform_3_0/datastore_features.html – 2014-09-10 08:55:53

+0

感谢您的回复@ KonstantinV.Salikhov。我试过了,但是它返回null并且没有抛出任何异常。所以我需要知道它是否可以完成。 – 2014-09-10 09:08:00

+0

为什么不直接执行“SELECT m FROM Movie m”的JPQL查询?你认为在幕后执行什么?假设你实际上没有看日志告诉你 – 2014-09-10 10:21:54

回答

1

它几乎没有什么意义做你正在做的事情。我的意思是你可以访问底层的MongoDB“DB”对象(即JPA使用的),并使用本地MongoDB API进行操作,而不是期望DataNucleus发明一些基于它的顶层的人工查询语言(该字符串数据库.BLAH.find()在MongoDB本地API中不存在,而是你执行db.getCollection(“BLAH”),然后施加约束等,最后调用find()。相反,你可以尝试(像)这

import org.datanucleus.ExecutionContext; 
import org.datanucleus.store.NucleusConnection; 

ExecutionContext ec = em.unwrap(ExecutionContext.class); 
NucleusConnection conn = ec.getStoreManager().getNucleusConnection(ec); 
DB db = (DB)conn.getNativeConnection(); 

此后你有DB对象使用,使用后你应该调用

conn.close(); 

手回JPA(DataNucleus将)。

+0

感谢@BillFrost +1为你:) – 2014-09-19 12:45:09