2017-01-09 54 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 –