# Active Objects Composite Indexes and Foreign Keys

**URL:** <https://community.developer.atlassian.com/t/active-objects-composite-indexes-and-foreign-keys/34211>\
**Category:** Everything Else\
**Created:** [December 11, 2019, 5:16pm UTC](https://community.developer.atlassian.com/t/active-objects-composite-indexes-and-foreign-keys/34211 "2019-12-11T17:16:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TureHoefner](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/turehoefner/32/38907_2.png) [@TureHoefner](https://community.developer.atlassian.com/u/TureHoefner)\
**Post date:** [December 11, 2019, 5:16pm UTC](https://community.developer.atlassian.com/t/active-objects-composite-indexes-and-foreign-keys/34211/1 "2019-12-11T17:16:47Z")

</div>

Hello,  
I am going to use composite indexes for Active Objects, I’m refactoring existing code that only has indexes on single fields at this time. [[AO 1.2.0 upgrade guide](https://developer.atlassian.com/server/framework/atlassian-sdk/ao-1-2-0-upgrade-guide/)]([http://AO](http://AO) 1.2.0 upgrade guide)

Javadoc:  
[https://javadoc.io/static/net.java.dev.activeobjects/activeobjects/3.1.7/net/java/ao/schema/Indexed.html](https://javadoc.io/static/net.java.dev.activeobjects/activeobjects/3.1.7/net/java/ao/schema/Indexed.html)  
[https://javadoc.io/static/net.java.dev.activeobjects/activeobjects/3.1.7/net/java/ao/schema/Index.html](https://javadoc.io/static/net.java.dev.activeobjects/activeobjects/3.1.7/net/java/ao/schema/Index.html)

I have some queries that join a foreign key field (the ID of a foreign key AO) and some other local field. Is it possible (or necessary) to create a composite index for such a query?

For example:

```auto
public interface Parent extends Entity {
  
  @OneToMany(reverse = "getParent")
  getChilds()
...

```

* * *

```auto
@Indexes({
        // TODO: HOW? IS THIS RIGHT? OR NECESSARY?
        @Index(name="parent_something", methodNames = {"getParent", "getSomething"})
})
public interface Child extends Entity {
  getParent()
  setParent()

  @Indexed // for queries on only this field
  getSomething()
  setSomething()

 doSomeQuery() {
    ... "PARENT_ID = ? AND SOMETHING = ?" ... // composite index required?
  }
...

```

---

<div class="post-metadata">

**Author:** ![TureHoefner](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/turehoefner/32/38907_2.png) [@TureHoefner](https://community.developer.atlassian.com/u/TureHoefner)\
**Post date:** [December 13, 2019, 12:49pm UTC](https://community.developer.atlassian.com/t/active-objects-composite-indexes-and-foreign-keys/34211/3 "2019-12-13T12:49:13Z")

</div>

As it turns out, it just works (FK composite indexes). Great job, whoever implemented the AO indexing!

We create a composite index for 2 AO fields, one of which is a foreign key to another AO (List) and the other which is a property/field of our AO (archived), like this:

```auto
@Indexes({
        @Index(name = "list_archived", methodNames = {"getList", "isArchived"})
})

```

where List is a FK field for the List AO. The “archived” field is in this AO.

results in this in the indexes for the your AO table in the DB:

```auto
INDEX_NAME COLUMN_NAME
------------- -----------
INDEX_AO_9355AF_CAR201396793 LIST_ID
INDEX_AO_9355AF_CAR201396793 ARCHIVED

```

Ture Hoefner
