1 Reply Latest reply on Apr 4, 2013 2:01 PM by 998288

    Understanding the SecondaryDatabase


      my conf:
      berkeley db je 5.0.73
      I use DPL

      I have seen several exemples where you use SecondaryDatabase or not and I can't understand in wich case I need it. I ask the question because I have currently an exception : primary record contains a key that is not present in the secondary. It happens when I try to delete my Tab class wich I just have updated with a new class Url in it, here is some code.

      I have 2 classes in my application, Tab wich can have one or more Urls in it.

      public class Tab implements GenericModel {

           @PrimaryKey(sequence = "tabSequenceId")
           private long id;

           @SecondaryKey(relate = Relationship.MANY_TO_ONE)
           private String userPicUniqueId;

           @SecondaryKey(relate = Relationship.ONE_TO_MANY, relatedEntity = Url.class, onRelatedEntityDelete = DeleteAction.NULLIFY)
           private Set<Long> urlIds = new HashSet<Long>();

           private String type;

           public Tab() {}

           public Tab(String userPicUniqueId, String type) {
                this.userPicUniqueId = userPicUniqueId;
                this.type = type;

      public class Url implements GenericModel{

           @PrimaryKey(sequence = "urlSequenceId")
           private long id;
           @SecondaryKey(relate = Relationship.MANY_TO_ONE, relatedEntity = Tab.class, onRelatedEntityDelete = DeleteAction.CASCADE)
           private long tabId;
           private String url;
           public Url() {}
           public Url(String url) {
                this.url = url;

      I configured the environment and store exactly like this :

      EnvironmentConfig envConfig = new EnvironmentConfig();
      env = new Environment(dataDirectory, envConfig);

      StoreConfig storeConfig = new StoreConfig();
      store = new EntityStore(env, "MyStore", storeConfig);

      So i have several questions.

      1) Do I need to configure primary and secondary databases ?
      2) If I have @SecondaryKey used as foreign keys, do I need to have secondary database ?

      Edited by: dgoman on 4 avr. 2013 06:13