Forum Stats

  • 3,770,003 Users
  • 2,253,045 Discussions
  • 7,875,265 Comments

Discussions

SecondaryKey modeling question

3236964
3236964 Member Posts: 8
edited May 15, 2016 11:15AM in Berkeley DB Java Edition

Hi,

I have an Entity class that could have 20 or more  MANY_TO_ONE relationships to other simple lookup type entities.  So the typical Employee has an EmployeeType kinds of relationships. 

My questions is, if you do not need to search on these types of fields, is it worth it to still define a SecondaryKey for the foreign key constraint benefits?  Or would all those SecondaryKey's be too costly for just the constraint benefits?  If we didn't define the SecondaryKey and relationship, we would have to manage the integrity manually.

Thanks for any suggestions or tips,

Scott

Best Answer

  • Greybird-Oracle
    Greybird-Oracle Member Posts: 2,690
    edited May 14, 2016 5:39PM Accepted Answer

    If that is the only constraint you need, it is much less expensive to do it manually than to create a secondary index. The secondary index is only necessary to (efficiently) enforce constraints on deletion of the foreign key, the onRelatedEntityDelete aspect. Since you didn't say you need this, I advise doing the integrity check manually.

    --mark

Answers

  • Greybird-Oracle
    Greybird-Oracle Member Posts: 2,690
    edited May 13, 2016 11:16PM

    Hi Scott,

    Which particular type of constraints do you require? For example, which DeleteAction do you require?

    If you were to do it "manually", how would you do it?

    --mark

  • 3236964
    3236964 Member Posts: 8
    edited May 14, 2016 4:14PM

    Hi Mark,  thanks for your reply.  I was referring to the foreign constraint section in the javadoc for SecondayIndex.

    SecondaryIndex (Oracle - Berkeley DB Java Edition API)

    So I meant manually like this:

    public void storeEmployee(Employee employee) throws DatabaseException {

           if (departmentIndex.contains(employee.department)) {

                primaryIndex.putNoReturn(employee);

           } else {

                throw new IllegalArgumentException("Department does not exist: " +

           employee.department);

           }

    }

  • Greybird-Oracle
    Greybird-Oracle Member Posts: 2,690
    edited May 14, 2016 5:39PM Accepted Answer

    If that is the only constraint you need, it is much less expensive to do it manually than to create a secondary index. The secondary index is only necessary to (efficiently) enforce constraints on deletion of the foreign key, the onRelatedEntityDelete aspect. Since you didn't say you need this, I advise doing the integrity check manually.

    --mark

  • 3236964
    3236964 Member Posts: 8
    edited May 15, 2016 11:15AM

    thanks Mark.  That makes good sense to me.

This discussion has been closed.