Datanucleus mongodb嵌入物

时间:2015-11-19 08:47:37

标签: mongodb orm datanucleus

Datanucleus是否支持mongodb嵌入对象?

我有收集" group"其中一个字段表示嵌入对象:

@PersistenceCapable(table = "group")
public class MyGroup
{
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY, column = "_id")
    private String id;
    private String name;
    @Embedded
    private Person person;

    // getters/setters
    ...
}

@EmbeddedOnly
@PersistenceCapable
public class Person
{
    private String _id;
    private String firstName;
    private String lastName;

    // getters/setters
    ...
}

按ID检索记录(并与Jackson合并为JSON)

MyGroup myGroup = persistenceManager.getObjectById(MyGroup.class, id)

配置:

@Bean
public PersistenceManager persistenceManager() throws IOException
{
    Properties jdoProperties = getProperties();

    PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(jdoProperties);
    return pmf.getPersistenceManager();
}

我得到的结果是:

{
  "id": "523119baac8c950b674d9245",
  "name": "GXVTXCCXB",
  "person": {
    "_id": null,
    "firstName": null,
    "lastName": null
  }
}

嵌入对象不会被其值填充。

1 个答案:

答案 0 :(得分:0)

如果您的JDO提供程序未生成现有数据,则必须确保映射与数据库中的映射匹配。在这种情况下,您似乎希望嵌入字段的“列”为“firstName”和“lastName”,但您的类未配置为使用这些名称嵌入它们。将注释更新为

@Embedded(members={
            @Persistent(name="firstName", columns=@Column(name="firstName")),
            @Persistent(name="lastName", columns=@Column(name="lastName"))})
private Person person;

请参阅JDO docs for embedded fields