How to create Index

#1

I have an entity marked as

@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

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

But if I query for the indexes

> 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?

0 Likes

#2

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.

0 Likes

#3

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.

0 Likes

#4

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

0 Likes

#5

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?

0 Likes

#6

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.

0 Likes

#7

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

0 Likes

#8

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:

@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

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

I hope this makes sense,
Cheers

0 Likes

#9

thanks this helps indeed :slight_smile:

0 Likes