查询mongodb hibernate ogm返回总是为null

时间:2017-01-09 15:47:10

标签: 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 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返回null。我的代码有什么问题吗?

1 个答案:

答案 0 :(得分:0)

我认为它返回null,因为映射或JSON对象中有些奇怪的东西,它找不到你要查找的实体。

您想要获得的JSON对象为_id: ObjectId("rzerzer"),这看起来并不正确,因为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中的对象是正确的,它也会被映射为String,因此Hibernate OGM不会期望ObjectId。

实体上id的映射应为:

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

@Id
public ObjectId id;

另一个奇怪的是你使用find的方式:

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

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

如果您不确定db中的id值,也可以使用HQL:

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