---
title: "morphia中id陷阱"
author: "dikar"
date: "2011-05-23T18:45:18Z"
tags: []
categories: ["编程语言"]
slug: "morphia-id"
draft: false
lastmod: "2011-05-23T18:45:18Z"
id: 1055601
url: "https://dikar.iteye.com/blog/1055601"
---

morphia中提供了以下2个annotation

    @Id  
    @Property

 

如果你有个字段叫userId 你想把这个当作主键。那么可以在userId字段上增加@Id这个annotation，如果你还加了@Property来缩短他的命名，例如叫“ud”，那么记得实际生成的还是叫“\_id”,这个在查询构造query时需要特别留意，需要传递的是“\_id” 而不是“ud”，也就是说在用了@Id这个annotation后，最好不要使用@Propery，即使用了也不能起其他名字，因为没有作用，叫“\_id”最好，也保证你后续的查询是一致的。