如何使用Kundera在MongoDB中进行更新级联

时间:2015-11-04 20:24:33

标签: java mongodb jpa entity kundera

我正在使用Kundera在MongoDB中执行持久化。在我的模型中,我有一组具有一组变量的实体类,其中一些是另一个实体类的List,依此类推。例如,名为RecordVersion的实体类具有实体类Record的列表,而实体类Record又具有实体类AuxiliarData的列表。 像这样:RecordVersion>记录> AuxiliarData>参考

@Entity
@Table(name = "recordVersion", schema = "TestDB@records")
public class RecordVersion {
  @Id 
  @Column(name="idRecordVersion")
  private String idRecordVersion;

  //others variables..      

  @OneToMany (cascade={CascadeType.ALL}, fetch=FetchType.EAGER")
  @JoinColumn(name="idRecordVersion")
  private List<Record> record = Lists.newArrayList();

记录

 @Entity
 @Table(name = "Record", schema = "TestDB@records")
 public class Record {
    @Id 
    @Column(name="idRecord")
    private String idRecord;

    //others variables..

    @OneToMany (cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
    @JoinColumn(name="idRecord")
    private List<AuxiliarData> auxiliarData = Lists.newArrayList();

对于AuxiliarData

 @Entity
 @Table(name = "AuxiliarData", schema = "TestDB@records")
 public class AuxiliarData {
    @Id 
    @Column(name="idAuxiliarData")
    private String idAuxiliarData;

    //others variables..

    @OneToMany (cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
    @JoinColumn(name="idAuxiliarData")
    private List<References> references = Lists.newArrayList();

我可以坚持使用mongoDB,完成RecordVersion的数据,所有的嵌套直到Reference,但是,我无法对RecordVersion的记录进行更新,例如在为特定的RecordVersion添加更多记录之后收集Mongo。

我尝试使用em.merge(someRecordVersion),但没有用。

1 个答案:

答案 0 :(得分:0)

对于MongoDB,默认情况下WriteConcern ACKNOWLEDGED 在Kundera-MongoDB中使用相同(表示它将显示重复主键的错误等

您可以使用em level属性将WriteConcern设置为UNACKNOWLEDGED来避免这些错误:

 em.setProperty(MongoDBClientProperties.WRITE_CONCERN, WriteConcern.UNACKNOWLEDGED);

示例代码:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("records");
EntityManager em = emf.createEntityManager();        
em.setProperty(MongoDBClientProperties.WRITE_CONCERN, WriteConcern.UNACKNOWLEDGED);
RecordVersion recVersion = new RecordVersion();
//setting children
em.merge(recVersion);