Java 查询mongodb hibernate ogm返回始终为空
我想在这里查询mongodb我的代码 Persistence.xmlJava 查询mongodb hibernate ogm返回始终为空,java,mongodb,hibernate,hibernate-ogm,Java,Mongodb,Hibernate,Hibernate Ogm,我想在这里查询mongodb我的代码 Persistence.xml <?xml version="1.0"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence htt
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="primary" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<properties>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform"/>
<property name="hibernate.ogm.datastore.provider" value="mongodb" />
<property name="hibernate.ogm.datastore.database" value="******" />
<property name="hibernate.ogm.datastore.host" value="******" />
<property name="hibernate.ogm.datastore.port" value="******" />
<property name="hibernate.ogm.datastore.username" value="******" />
<property name="hibernate.ogm.datastore.password" value="******" />
</properties>
</persistence-unit>
</persistence>
main
public static void main(String[] args) throws ClassNotFoundException{
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory( "primary" );
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
enl_flux_f12_entry f = entityManager.find(Flux.class, "*id*");
System.out.println(f.id);
entityManager.flush();
entityManager.close();
}
mongodb
{
"_id" : ObjectId("rzerzer"),
"SYS_FluxName" : "zerzerze.xml",
"SYS_ReadDateTime" : Timestamp(6300883749567463, 83),
"SYS_BaseNameZip" : "rferfer.zip",
"SYS_Status" : NumberLong(1),
"SYS_DateCreaERDF" : "2016-03-01T20:38:48Z"
}
问题是entityManager.find返回值总是空的。我的代码中有什么问题吗?我认为它返回null,因为映射或JSON对象中有一些奇怪的东西,它找不到您要查找的实体 要获取的JSON对象具有
\u id:ObjectId(“rzerzer”)
,这看起来不正确,因为:
即使数据库中的对象是正确的,它也被映射为字符串,因此Hibernate OGM不需要ObjectId
实体上的id映射应为:
@Id
@Type(type = "objectid")
public String id;
或
另一件奇怪的事情是您使用find的方式:
enl_flux_f12_entry f = entityManager.find(Flux.class, "*id*");
find
方法需要实体的准确id。如果映射是正确的,那么这应该可以工作entityManager.find(Flux.class,“rzerzer”)代码>
如果不确定数据库中的id值,也可以使用HQL:
List<Flux> entries = entityManager.createQuery( "from Flux" ).list();
List entries=entityManager.createQuery(“来自流量”).List();
我认为它返回null,因为映射或JSON对象中有一些奇怪的东西,它找不到您要查找的实体
要获取的JSON对象具有\u id:ObjectId(“rzerzer”)
,这看起来不正确,因为:
即使数据库中的对象是正确的,它也被映射为字符串,因此Hibernate OGM不需要ObjectId
实体上的id映射应为:
@Id
@Type(type = "objectid")
public String id;
或
另一件奇怪的事情是您使用find的方式:
enl_flux_f12_entry f = entityManager.find(Flux.class, "*id*");
find
方法需要实体的准确id。如果映射是正确的,那么这应该可以工作entityManager.find(Flux.class,“rzerzer”)代码>
如果不确定数据库中的id值,也可以使用HQL:
List<Flux> entries = entityManager.createQuery( "from Flux" ).list();
List entries=entityManager.createQuery(“来自流量”).List();
thx感谢您的回复。我解决了所有这些问题,但它仍然是时间戳。Hibernate OGM目前不支持将什么类型的时间戳放入SYS_readdatetimestamp。您可以尝试将其映射为Stringno它不起作用:无法设置java.lang.String字段。。。。请转到org.bson.types.BSONTimestampthx以获取您的答复。我解决了所有这些问题,但它仍然是时间戳。Hibernate OGM目前不支持将什么类型的时间戳放入SYS_readdatetimestamp。您可以尝试将其映射为Stringno它不起作用:无法设置java.lang.String字段。。。。到org.bson.types.BSONTimestamp