2017-01-09 52 views
1

我想在这里查询的MongoDB我的代码查询的MongoDB冬眠OGM回报总是空

的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 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> 

Flux.java

@Entity 
@Table(catalog="f12", schema="public", name="enl_flux_f12_entry") 
public class enl_flux_f12_entry{ 

    @Id 
    public String id; 

    public String SYS_FluxName; 
    public byte[] SYS_ReadDateTime; 
    public String SYS_BaseNameZip; 
    public Long SYS_Status; 
    public String SYS_DateCreaERDF; 
} 

主要

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返回总是空。我的代码有问题吗?

回答

0

我认为它会返回null,因为映射或JSON对象中有奇怪的东西,它无法找到您正在寻找的实体。

你想获得已_id: ObjectId("rzerzer"),这看起来并不JSON对象正确的,因为an ObjectId in MongoDB should be

The 12-byte ObjectId value consists of: 

a 4-byte value representing the seconds since the Unix epoch, 
a 3-byte machine identifier, 
a 2-byte process id, and 
a 3-byte counter, starting with a random value. 

即使在DB的对象是正确的,它被映射为一个字符串,因为Hibernate OGM不期望一个ObjectId。

在实体ID的映射应该是:

@Id 
@Type(type = "objectid") 
public String id; 

@Id 
public ObjectId id; 

另一个奇怪的事情是,你正在使用的查找方式:

enl_flux_f12_entry f = entityManager.find(Flux.class, "*id*"); 

find方法需要实体的确切ID。如果映射是正确的,这应该工作entityManager.find(Flux.class, "rzerzer");

如果你不能确定的id值在DB你也可以使用HQL:

List<Flux> entries = entityManager.createQuery("from Flux").list(); 
+0

THX您的答复。我修复了所有这些问题,但它仍然是时间戳。我应该把什么类型放到SYS_ReadDateTime –

+0

Hibernate OGM目前不支持时间戳。你可以尝试将它映射为字符串 – Davide

+0

不,它不起作用:无法将java.lang.String字段设置为org.bson.types.BSONTimestamp –