# How to create Index

**URL:** <https://discourse.hibernate.org/t/how-to-create-index/2233>\
**Category:** Hibernate OGM\
**Created:** [February 7, 2019, 12:37am UTC](https://discourse.hibernate.org/t/how-to-create-index/2233 "2019-02-07T00:37:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![alfredopacino](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@alfredopacino](https://discourse.hibernate.org/u/alfredopacino)\
**Post date:** [February 7, 2019, 12:37am UTC](https://discourse.hibernate.org/t/how-to-create-index/2233/1 "2019-02-07T00:37:34Z")

</div>

I have an entity marked as

```auto
@MappedSuperclass
@Table(	name = "BigEvent",
		indexes = {
				@Index(columnList = "type", name = "type_idx"),
				@Index(columnList = "activity_id", name = "activity_id_idx")
})
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class BigEvent implements Serializable {
  ...
}

```

a class that extends the first one

```auto
@Entity
@Table(name="BigEvent")
@DiscriminatorColumn(name = "type")
public class GridListEvent extends BigEvent implements Serializable{
   ...
}

```

But if I query for the indexes

```auto
> db.BigEvent.getIndexes()
[
	{
		"v" : 2,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_",
		"ns" : "DB.BigEvent"
	}
]

```

what’s the correct way to ensure Indexes in hibernate-ogm for MongoDB?

side question: how can I print the actual mongodb query during a JPQL insert/select?

---

<div class="post-metadata">

**Author:** ![DavideD](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@DavideD](https://discourse.hibernate.org/u/DavideD)\
**Post date:** [February 7, 2019, 10:58am UTC](https://discourse.hibernate.org/t/how-to-create-index/2233/2 "2019-02-07T10:58:54Z")

</div>

I will answer first to the quertion about the queries.

Hibernate OGM converts queries into proper calls to the MongoDB java driver API. It means that it doesn’t generate an equivalent query that can be run using the Mongo CLI client.  
So it cannot print the native query.

---

<div class="post-metadata">

**Author:** ![DavideD](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@DavideD](https://discourse.hibernate.org/u/DavideD)\
**Post date:** [February 7, 2019, 11:46am UTC](https://discourse.hibernate.org/t/how-to-create-index/2233/3 "2019-02-07T11:46:50Z")

</div>

About the indexes, I think something is wrong in your mapping.

You are using `@Inheritance(strategy = InheritanceType.SINGLE_TABLE)` but the super class is not an entity and therefore doesn’t have a table (in theory) and it wouldn’t make sense to inherits the name of the table for all the children of the hierarchy. Probably, OGM is ignoring it.

I think you need to declare the indexes on `GridListEvent` because that’s the entity mapped to the table “BigEvent” (Very confusing by the way).

An alternatve solution that might work is to replace the `@MappedSuperclass` with `@Entity` but I wouldn’t go this way because it makes the mapping harder to understand.

---

<div class="post-metadata">

**Author:** ![DavideD](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@DavideD](https://discourse.hibernate.org/u/DavideD)\
**Post date:** [February 7, 2019, 11:48am UTC](https://discourse.hibernate.org/t/how-to-create-index/2233/4 "2019-02-07T11:48:30Z")

</div>

The index you see in the BigEvent table is created from GridListEvent that you decided to map on the table BigEvent.

---

<div class="post-metadata">

**Author:** ![alfredopacino](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@alfredopacino](https://discourse.hibernate.org/u/alfredopacino)\
**Post date:** [February 7, 2019, 12:29pm UTC](https://discourse.hibernate.org/t/how-to-create-index/2233/5 "2019-02-07T12:29:44Z")

</div>

I map multiple Entities (not just `GridListEvent`) on the table `BigEvent` using `type` as discriminator.  
So I have to set the indexes for each entity?

Why do you say it seems confusing?

---

<div class="post-metadata">

**Author:** ![DavideD](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@DavideD](https://discourse.hibernate.org/u/DavideD)\
**Post date:** [February 7, 2019, 1:08pm UTC](https://discourse.hibernate.org/t/how-to-create-index/2233/6 "2019-02-07T13:08:37Z")

</div>

I see, the thing I find confusing is that you use `@Table(name="BigEvent")`  
on the entity `GridListEvent`. You don’t need to tell Hibernate OGM about it. Because of the inheritance configuration it will figure it out on its own.

If in your example replace `@MappedSuperClass` with `@Entity`, it should work though.

---

<div class="post-metadata">

**Author:** ![DavideD](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@DavideD](https://discourse.hibernate.org/u/DavideD)\
**Post date:** [February 7, 2019, 1:12pm UTC](https://discourse.hibernate.org/t/how-to-create-index/2233/7 "2019-02-07T13:12:21Z")

</div>

But I guess it should work with `@MappedSuperclass`. I will have a better look at it soon.

---

<div class="post-metadata">

**Author:** ![DavideD](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@DavideD](https://discourse.hibernate.org/u/DavideD)\
**Post date:** [February 11, 2019, 2:13pm UTC](https://discourse.hibernate.org/t/how-to-create-index/2233/8 "2019-02-11T14:13:28Z")

</div>

I gave a better look at this issue and the problem is that `@MappedSuperclass` is used when you want to inherit properties, associations, and methods.

In this case you are actually creating a hierarchy between entities, therefore `@Entity` should be used. That’s because you want to have a single table mapping different entities (I think). Otherwise you wouldn’t use InheritanceType.SINGLE.

You can map the entities in the following way:

```auto
@Entity
@Table(	name = "BigEvent",
		indexes = {
				@Index(columnList = "type", name = "type_idx"),
				@Index(columnList = "activity_id", name = "activity_id_idx")
})
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
public abstract class BigEvent implements Serializable {
...
}

@Entity
public class GridListEvent extends BigEvent implements Serializable {
...
}

@Entity
public class OtherEvent extends BigEvent implements Serializable {
...
}

```

I decided to create a Jira for this: [https://hibernate.atlassian.net/browse/OGM-1556](https://hibernate.atlassian.net/browse/OGM-1556)

I think the index for the discriminator column could be created auto-magically.

I hope this makes sense,  
Cheers

---

<div class="post-metadata">

**Author:** ![alfredopacino](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@alfredopacino](https://discourse.hibernate.org/u/alfredopacino)\
**Post date:** [February 11, 2019, 2:37pm UTC](https://discourse.hibernate.org/t/how-to-create-index/2233/9 "2019-02-11T14:37:06Z")

</div>

thanks this helps indeed 🙂
