This discussion is archived
3 Replies Latest reply: Nov 3, 2012 4:01 PM by greybird RSS

Creating Tables Berkeley DB

957451 Newbie
Currently Being Moderated
Hi,

Is it possible to create a entity with multiple annotations like this:

@PrimaryKey
@SecondaryKey(relate = Relationship.MANY_TO_ONE, relatedEntity = Language.class)
private int lang_id;

-----

PrimaryIndex<Integer, Text_Question> pkTextQuestion = entityStore.getPrimaryIndex(Integer.class, Text_Question.class);
SecondaryIndex<Integer, Integer, Text_Question> skQuestionType = entityStore.getSecondaryIndex(pkTextQuestion, Integer.class, "lang_id");

The idea was that lang_id should be a Primary Key and a Foreign Key
  • 1. Re: Creating Tables Berkeley DB
    greybird Expert
    Currently Being Moderated
    Hi,

    No, the two annotations are not used together on the same field.

    The primary key should be defined as usual using @PrimaryKey. To define it as a foreign key, you define a secondary key in another entity class, and relatedEntity in the @SecondaryKey annotation.

    An example of this is in the Foreign Key Constraints for Related Entities section of the SecondaryIndex javadoc:
    http://docs.oracle.com/cd/E17277_02/html/java/com/sleepycat/persist/SecondaryIndex.html

    Are saying that you want to use the same value as the primary key for two different entities? If so, you can't define foreign key constraints on it.

    If you explain more about what you're doing, and include more details on both entity classes, I may be able to give you a better answer.

    --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
  • 2. Re: Creating Tables Berkeley DB
    957451 Newbie
    Currently Being Moderated
    Thank you very much, the example site is great!

    so my Entities look like this
    -----
    @Entity
    public class Language {

    @PrimaryKey
    private int lang_id;

    private String lang_name;
    private String locale;

    public Language() {
    super();
    }
    }

    @Entity
    public class Text_Question {

    @PrimaryKey
    private int text_question_id;

    @SecondaryKey(relate = Relationship.MANY_TO_ONE, relatedEntity = Language.class)
    private int lang_id;

    private String text;

    public Text_Question() {
    }
    }
    -----
    Now my source code
    PrimaryIndex<Integer, Language> pkLanguage = entityStore.getPrimaryIndex(Integer.class, Language.class);
    SecondaryIndex<Integer, Integer, Language> skLanguage = entityStore.getSecondaryIndex(pkLanguage, Integer.class, "lang_id");
    PrimaryIndex<Integer, Text_Question> pkTextQuestion= entityStore.getPrimaryIndex(Integer.class, Text_Question.class);
    SecondaryIndex<Integer, Integer, Text_Question> skTextQuestion = entityStore.getSecondaryIndex(pkTextQuestion, Integer.class, "question_id");

    //fill my db
    pkLanguage.put(new Language(1, "Deutsch", "de_AT"));
    pkLanguage.put(new Language(2, "English", "en_US"));
    pkTextQuestion.put(new Text_Question(1, 1, "Wie heißen Sie?"));
    pkTextQuestion.put(new Text_Question(2, 2, "What's your name?"));

    ----
    if I run my programm I always get this exception
    Not a secondary key: com.demoproject.berkeleydb.Language#lang_id

    What am i doing wrong here? (I tried to delete the DB Files and let it create a new DB but it didn't work)
    I hope you can help me.
    Thank you

    Edited by: oraclebeginner93 on 03.11.2012 23:49
  • 3. Re: Creating Tables Berkeley DB
    greybird Expert
    Currently Being Moderated
    SecondaryIndex<Integer, Integer, Language> skLanguage = entityStore.getSecondaryIndex(pkLanguage, Integer.class, "lang_id");
    I assume the exception is coming from this call. I'm not sure what you're trying to do. You have not defined a secondary key in the Language class, so this fails.

    You have defined a secondary key in the Text_Question class. To create the secondary index object for that key, you would do this:
    SecondaryIndex<Integer, Integer, Text_Question> skTextQuestion = entityStore.getSecondaryIndex(pkTextQuestion, Integer.class, "lang_id");

    --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points