2017-04-09 82 views
0

我有兩個簡單的POJO的公司和員工。我正在使用morphia將它們保存在MongoDB中。員工是公司類中的參考文檔。我成功地能夠保存公司對象,但當我嘗試檢索。我得到一個對象Morphia MongoDB空對象

Company.java

package com.hifi.model; 

import javax.xml.bind.annotation.XmlRootElement; 

import org.bson.types.ObjectId; 
import org.mongodb.morphia.annotations.Entity; 
import org.mongodb.morphia.annotations.Id; 
import org.mongodb.morphia.annotations.Reference; 

@XmlRootElement 
@Entity("companies") 
public class Company { 

private String companyName; 
private String numberOfEmployees; 
@Id 
private ObjectId companyId; 
@Reference 
private Employee emp; 
public ObjectId getCompanyId() { 
    return companyId; 
} 
public void setCompanyId(ObjectId companyId) { 
    this.companyId = companyId; 
} 

public String getCompanyName() { 
    return companyName; 
} 
public void setCompanyName(String companyName) { 
    this.companyName = companyName; 
} 
public String getNumberOfEmployees() { 
    return numberOfEmployees; 
} 
public void setNumberOfEmployees(String numberOfEmployees) { 
    this.numberOfEmployees = numberOfEmployees; 
} 
public Employee getEmp() { 
    return emp; 
} 
public void setEmp(Employee emp) { 
    this.emp = emp; 
} 

} 

Employee.java

package com.hifi.model; 

import javax.xml.bind.annotation.XmlRootElement; 

import org.bson.types.ObjectId; 
import org.mongodb.morphia.annotations.Entity; 
import org.mongodb.morphia.annotations.Id; 

@XmlRootElement 
@Entity("employees") 
public class Employee { 

private String name; 
private int age; 
@Id 
private ObjectId empId; 
public ObjectId getEmpId() { 
    return empId; 
} 
public void setEmpId(ObjectId empId) { 
    this.empId = empId; 
} 
public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public int getAge() { 
    return age; 
} 
public void setAge(int age) { 
    this.age = age; 
} 

} 

這裏是我的嗎啡查詢例如

package com.hifi; 

import java.util.ArrayList; 
import java.util.List; 

import org.mongodb.morphia.Datastore; 
import org.mongodb.morphia.Morphia; 
import org.mongodb.morphia.query.Query; 

import com.hifi.database.MongoDBSingleton; 
import com.hifi.model.Company; 
import com.hifi.model.Employee; 
import com.hifi.util.Constants; 
import com.mongodb.MongoClient; 

public class MorphiaQuery { 

MongoDBSingleton mongoInstance; 
private Datastore datastore; 
private Morphia morphia; 

private List<Company> friendsPostList = new ArrayList<Company>(); 

public List<Company> query() { 

    morphia = new Morphia(); 

    morphia.mapPackage("com.hifi.model"); 
    // create the Datastore connecting to the default port on the local 
    // host 
    datastore = morphia.createDatastore(new MongoClient(), 
    Constants.MONGO_DB); 
    // datastore.ensureIndexes(); 
    // mongoInstance = MongoDBSingleton.getInstance();// Get the 
    mongoDb 
    // connection 
    // datastore = mongoInstance.getDataStore(); 

    Query<Company> query = datastore.createQuery(Company.class); 

    Query<Employee> userQuery = datastore.createQuery(Employee.class); 

    userQuery.field("age").equal(12); 

    Employee emp = userQuery.get(); 
    System.out.println("Printing -------user " + emp); 
    query.criteria("emp").equal(emp); 
    // query.order("-postTimeStamp"); // Order by timestamp DESC 
    // query.limit(1); 
    friendsPostList = query.asList(); 

    return friendsPostList; 

} 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    MorphiaQuery m = new MorphiaQuery(); 
    System.out.println(m.query()); 

} 

} 

**控制檯輸出:**強大的文本****

Apr 09, 2017 12:30:06 PM 
org.mongodb.morphia.logging.MorphiaLoggerFactory chooseLoggerFactory 

INFO: LoggerImplFactory set to 
org.mongodb.morphia.logging.jdk.JDKLoggerFactory 

Apr 09, 2017 12:30:08 PM com.mongodb.diagnostics.logging.JULLogger log 
INFO: Cluster created with settings {hosts=[127.0.0.1:27017], 
mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 
ms', maxWaitQueueSize=500} 

Apr 09, 2017 12:30:08 PM com.mongodb.diagnostics.logging.JULLogger log 
INFO: Opened connection [connectionId{localValue:1, serverValue:26}] to 
127.0.0.1:27017 

Apr 09, 2017 12:30:08 PM com.mongodb.diagnostics.logging.JULLogger log 
INFO: Monitor thread successfully connected to server with description 
ServerDescription{address=127.0.0.1:27017, type=STANDALONE, 
state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 4, 2]}, 
minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, 
roundTripTimeNanos=970170} 

Apr 09, 2017 12:30:09 PM com.mongodb.diagnostics.logging.JULLogger log 
INFO: Opened connection [connectionId{localValue:2, serverValue:27}] to 
127.0.0.1:27017 
Printing -------user null 
[] 

我的JUnit測試用例

@Test 
    public void testCreateCompany() { 
    PostClient postClient = new PostClient(); 

    Company company = new Company(); 
    company.setCompanyName("ABC"); 
    company.setCompanyId(new ObjectId()); 
    company.setNumberOfEmployees("100"); 

    Employee user = new Employee(); 
    user.setName("siva"); 
    user.setAge(12); 
    user.setEmpId(new ObjectId()); 
    company.setEmp(user); 

    company = postClient.create(company); 
    assertNotNull(company); 

    } 

這是工作正常,直到昨天。 Btw iam使用嗎啡1.3.1 maven依賴。請幫助!

回答

0

您在單元測試中保存公司對象,但忘記了員工。

公司類中的@Reference註釋emp字段告訴Morphia只保存對Employee的引用,但它不會將該員工作爲插入的一部分進行存儲。

+0

您確定嗎?因爲我認爲mongodb隱含地爲你創建了用戶文檔。但我會試試這個肯定! –

+0

@PratikKulkarni是的,絕對。我自己嘗試過。祝你好運! –