Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom index for ebean?

I'm using ebean, it's very easy to use but I don't know create my custom index.

@Entity
@Table(name="users")
public class User {
    public String name;

    // how to create index for this column?
    public long displayOrder;
}

I tried to create index for the column displayOrder, but how to do it?

There is no annotation for this, and the generated ddl file will be recreated each time, so I can't add my own ddl statements there. And it's not convenient to add index in database directly.

Is there any simple solution to do it?

like image 261
Freewind Avatar asked Feb 26 '26 01:02

Freewind


1 Answers

There is no direct answer, but I think, that you should concern on it for a while.

AFAIK there's no way to declare 'KEY' index via annotations, you can only add @Column(unique=true) to it (if you need an unique field).

Of course for primary key you just need to use @Id annotation.

On the other hand I do not recommend using automatic DDL creation every time - remember, that will destroy all your DB structure and data after next evolution applying!!! Instead go with this way:

  • prepare as much models as possible in the first step, and use automatic DDL to apply it to the DB
  • Disable Ebean's auto generation
  • Start using own evolutions (2.sql, 3.sql etc) for adding missing elements - like for an example missing indexes.
  • Never turn back :) From now you need to ALTER an existing tables instead CREATE them every time from the scratch.
like image 77
biesior Avatar answered Mar 01 '26 06:03

biesior



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!