我正在使用Morphia和DAO Class。我有一个带有Id ObjectId的类:
@Entity
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class GeoProfileVo extends ResponseVo implements Serializable{
private static final long serialVersionUID = -4975628041316056892L;
@Id private ObjectId id;
private String email;
private String name;
private String city;
//Getters, Setters and toString
}
GeoProfileDAO:
public class GeoProfileDAO extends BasicDAO<GeoProfileVo, ObjectId>{
public GeoProfileDAO(Morphia morphia, MongoClient mongoClient, String db) {
super(mongoClient, morphia, db);
}
public GeoProfileVo findByNme(String name){
return getDs().find(GeoProfileVo.class, "name", name).get();
}
//Here is the problem
public GeoProfileVo findById (ObjectId id){
//return getDs().find(GeoProfileVo.class).field("id").equal(id).get();
return getDs().get(GeoProfileVo.class, id);
}
public ObjectId saveGeoProfile(GeoProfileVo geoProfileVo){
return (ObjectId) getDs().save(geoProfileVo).getId();
}
}
我的服务:
public class GeolocationService implements Serializable{
private static final long serialVersionUID = 2071937170723089158L;
/** The Constant log. */
private static final Logger LOGGER = Logger.getLogger(GeolocationService.class.getName());
private MongoClient mongoClient;
private GeoProfileDAO geoProfileDAO;
public GeolocationService(){
super();
LOGGER.info("[GeolocationService - Constructor] - init");
//mongoClient:
mongoClient = new MongoClient("localhost",27017);
//morphia:
Morphia morphia = new Morphia();
morphia.map(GeoProfileVo.class);
//morphia dao:
geoProfileDAO = new GeoProfileDAO(morphia, mongoClient, "mydb");
}
public GeoProfileVo updateGeoProfile(GeoProfileVo geoProfileVo) throws GeoProfileNotFoundException{
LOGGER.info("[GeolocationService - updateGeoProfile] - init");
long currentSystemTime=System.currentTimeMillis();
if(geoProfileVo == null){
LOGGER.error("[GeolocationService - updateGeoProfile] - geoProfileVo cannot be null");
throw new IllegalArgumentException();
}
if(geoProfileVo.getId() == null){
LOGGER.error("[GeolocationService - updateGeoProfile] - ID cannot be null");
throw new IllegalArgumentException();
}
GeoProfileVo geoProfileVoEntity = geoProfileDAO.findById(geoProfileVo.getId());
if(geoProfileVoEntity==null){
LOGGER.error("[GeolocationService - updateGeoProfile] - geoProfileVo not found in BD");
throw new GeoProfileNotFoundException();
}
LOGGER.debug("[GeolocationService - updateGeoProfile] - Finish Timing:"+(System.currentTimeMillis()-currentSystemTime));
return geoProfileDAO.updateGeoProfile(geoProfileVo);
}
//CRUD
}
我可以通过电子邮件,姓名或城市进行搜索,在BD中插入对象,但我无法按ID搜索。在谷歌我只找到有关字符串ID的信息,我没有找到有关id类型ObjectId的更多信息。当我尝试通过ObjectId Id查找时,没有找到任何内容并返回null。
例如,当我通过电子邮件找到时,返回:
{
"id": {
"timestamp": 1432028968,
"machineIdentifier": 9913253,
"processIdentifier": 7516,
"counter": 8215016
},
"email": "aaa@gmail.com",
"name": "john",
"city": "Madrid"
}
我调用一个使用相同ID调用createGeoProfile服务的WS,但是在Eclipse IDE中,每个调用中的“id”都不同。也许问题出在这里,但我不明白。
如何找到id类型为ObjectId的对象?
答案 0 :(得分:3)
当我在BD中插入对象时,他的解决方案返回了一个ObjectId.toString()。我知道这个objectId类似于Hash,所以当我想通过Id找到时,我无法发送ObjectId类型,因为这个ObjectId会发生变化。出于这个原因,我们必须返回一个toString()(这部分是我没有看到的)。
使用此String,我们实例化一个ObjectId,我们使用它来通过id:
进行查找#!/bin/sh
/bin/rm -f configure config.h config.h.in src/lib/Makefile.in
autoreconf -ivf
./configure
最后,为了避免使用我不会使用它的ObjectId类型,我将GeoProfileVo中的Id更改为String类型。
public GeoProfileVo findById (String id){
ObjectId oid = new ObjectId(id);
return getDs().find(GeoProfileVo.class).field("id").equal(oid).get();
}
public String saveGeoProfile(GeoProfileVo geoProfileVo){
return getDs().save(geoProfileVo).getId().toString();
}
现在,FindById的响应包含String类型中的id:
@Id private String id;