Mongo:morphia不会在jongo请求中映射_id

时间:2012-08-13 08:42:03

标签: mongodb mapping morphia jongo

这些是我的对象:

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public abstract class AdtagEntity extends GenericEntity implements Serializable {

private static final long serialVersionUID = -6624915864457400750L;

    @Id
    @XmlElement
    protected ObjectId id;

    @XmlElement
    protected Date createdDate;

    @XmlElement
    @Indexed
    protected Date modifiedDate;

    @XmlElement
    protected boolean deleted;

    public ObjectId getId() {
        return id;
    }
    ....
}

和:

@Entity
@XmlRootElement(name = "offerAdvantage")
public class OfferAdvantage extends AdtagEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @XmlElement
    @NotNull
    private Date beginDateValidity;
    ...
}

这是OfferAdvantage集合的内容:

myset:PRIMARY> db.OfferAdvantage.findOne()
{
    "_id" : ObjectId("502533ddc131e6beb0b07cae"),
    "company" : "pio",
    "version" : 666,
    "createdDate" : ISODate("2012-08-10T16:16:29.106Z"),
    "modifiedDate" : ISODate("2012-08-10T16:16:29.106Z"),
    "deleted" : false,
    "beginDateValidity" : ISODate("2012-08-03T13:20:00Z"),
    "endDateValidity" : ISODate("2015-10-04T23:06:40Z"),
    "name" : "deuxieme demarque",
    "description" : "",
    "pois" : [
        ObjectId("4fe1ebb9e4b0ef9431abd904")
    ]
}

我的pom.xml:

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>2.6.5</version>
</dependency>
<dependency>
    <groupId>org.jongo</groupId>
    <artifactId>jongo</artifactId>
    <version>0.2</version>
</dependency>

问题在于,当我想使用OfferAdvantage对象时,对于集合的每个对象,优势的id为null(优势集合是jongo api的MongoCollection):

Iterable<OfferAdvantage> iterable = advantageCollection.find().as(OfferAdvantage.class);
Iterator<OfferAdvantage> iterator = iterable.iterator();
ObjectId advantageId = null;

while(iterator.hasNext()) {
    OfferAdvantage advantage = iterator.next();
}

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

根据Jongo documentation,您可以在Morphia注释的基础上使用注释@JsonProperty("_id")来使用Jongo:

public abstract class AdtagEntity extends GenericEntity implements Serializable {

private static final long serialVersionUID = -6624915864457400750L;

    @Id
    @JsonProperty("_id")
    @XmlElement
    protected ObjectId id;

告诉它是否有效,我非常感兴趣。 ;)