CINXE.COM
@Index | Mapping | Ebean
<!doctype html> <html lang="en"> <head> <title>@Index | Mapping | Ebean</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="shortcut icon" href="/images/favicon.ico"> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto|Source+Sans+Pro|Ubuntu&display=swap"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css" integrity="sha384-lKuwvrZot6UHsBSfcMvOkWwlCMgc0TaWr+30HWe3a4ltaBwTZhyTEggF5tJv8tbt" crossorigin="anonymous"> <link rel="stylesheet" href="/css/reset3.css"> <link rel="stylesheet" href="/css/site3.css"> <link rel="stylesheet" href="/css/pygments3.css"> </head> <body> <div id="main"> <div id="banner"> <header> <nav id="top"> <h1 id="breadcrumb"> <a class="nav-logo" href="/"><img src="/images/logo-200.png" height="35"></a> <a href="/docs">Documentation</a><span class="sep"> / </span><a href="/docs/mapping/">Mapping</a><span class="sep"> / </span><a href="/docs/mapping/extension">Extension</a><span class="sep"> / </span><span class="last">@Index</span> </h1> <ul> <li><a onclick="toggleTheme();" title="switch dark light theme"><i class="fas fa-adjust"></i></a></li> </ul> </nav> </header> </div> <div class="grid grid-docs"> <aside> <nav class="side"> <ul> <li class="nav0 "> <a href="/docs/getting-started">Getting started</a> </li> <li class="nav0 "> <a href="/docs/intro">Introduction</a> </li> <li class="nav0 active"> <a class="active" href="/docs">Documentation</a> <ul> <li class="nav1 "> <a href="/docs/best-practice">Best practice</a> </li> <li class="nav1 "> <a href="/docs/query">Query</a> </li> <li class="nav1 "> <a href="/docs/persist">Persist</a> </li> <li class="nav1 "> <a href="/docs/transactions">Transactions</a> </li> <li class="nav1 active"> <a class="active" href="/docs/mapping">Mapping</a> <ul class="nav"> <li class="nav1 "> <a href="/docs/mapping/type">Types</a> </li> <li class="nav1 "> <a href="/docs/mapping/jpa">JPA</a> </li> <li class="nav1 active"> <a class="active" href="/docs/mapping/extensions">Extensions</a> <ul class="nav"> <li > <a href="/docs/mapping/extensions/identity">@Identity</a> </li> <li class="active"> <a class="active" href="/docs/mapping/extensions/dbindex">@Index</a> </li> <li > <a href="/docs/mapping/extensions/dbenumvalue">@DbEnumValue</a> </li> <li > <a href="/docs/mapping/extensions/dbjson">@DbJson</a> </li> <li > <a href="/docs/mapping/extensions/dbarray">@DbArray</a> </li> <li > <a href="/docs/mapping/extensions/dbmap">@DbMap</a> </li> <li > <a href="/docs/mapping/extensions/dbforeignkey">@DbForeignKey</a> </li> <li > <a href="/docs/mapping/extensions/dbcomment">@DbComment</a> </li> <li > <a href="/docs/mapping/extensions/dbpartition">@DbPartition</a> </li> <li > <a href="/docs/mapping/extensions/when-created">@WhenCreated</a> </li> <li > <a href="/docs/mapping/extensions/when-modified">@WhenModified</a> </li> <li > <a href="/docs/mapping/extensions/who-created">@WhoCreated</a> </li> <li > <a href="/docs/mapping/extensions/who-modified">@WhoModified</a> </li> <li > <a href="/docs/mapping/extensions/soft-delete">@SoftDelete</a> </li> <li > <a href="/docs/mapping/extensions/encrypted">@Encrypted</a> </li> <li > <a href="/docs/mapping/extensions/formula">@Formula</a> </li> <li > <a href="/docs/mapping/extensions/view">@View</a> </li> <li > <a href="/docs/mapping/extensions/history">@History</a> </li> <li > <a href="/docs/mapping/extensions/change-log">@ChangeLog</a> </li> <li > <a href="/docs/mapping/extensions/read-audit">@ReadAudit</a> </li> </ul> </li> </ul> </li> <li class="nav1 "> <a href="/docs/ddl-generation">DDL & Migrations</a> </li> <li class="nav1 "> <a href="/docs/logging">Logging</a> </li> <li class="nav1 "> <a href="/docs/testing">Testing</a> </li> <li class="nav1 "> <a href="/docs/read-replicas">Read Replicas</a> </li> <li class="nav1 "> <a href="/docs/database">Database platforms</a> </li> <li class="nav1 "> <a href="/docs/multi-database">Multiple databases</a> </li> <li class="nav1 "> <a href="/docs/kotlin">Kotlin</a> </li> <li><a href="/docs/tuning">Tuning</a></li> <li class="nav1 "> <a href="/docs/features">Features</a> </li> </ul> </li> <li class="nav0 "> <a href="/support">Getting help</a> </li> <li class="nav0 "> <a target="_blank" href="/apidoc/13">API Javadoc</a> </li> <li class="nav0 "> <a href="/videos">Videos</a> </li> <li class="nav0 "> <a href="/docs/upgrading">Upgrading</a> </li> <li class="nav0 "> <a href="/releases">Releases</a> </li> </ul> </nav> </aside> <article> <form action="https://www.google.com/search" method="get" class="inline-form"> <input type="hidden" name="as_sitesearch" value="ebean.io"> <div id="page-search"> <div class="input-group"> <input class="frm" name="q" id="searchinput" type="text" placeholder="Search... (press 's' to focus)" data-placeholder-focus="Search... (use '↑', '↓' and '⏎' to select results)" data-placeholder-blur="Search... (press 's' to focus)" autocomplete="off"> <div class="input-group-btn"> <button class="frm" type="submit"><i class="fas fa-search"></i></button> </div> </div> <div id="page-search-results" style="display: none;"> <ul id="search-results-container" class="search-results"><li class=" active"><a href="/docs" title="Docs"><span style="color:#777;">Docs</span> Documentation </a></li><li class=""><small style="color:#999;">And 101 more...</small></li></ul> </div> </div> </form> <h2 id="index">@Index</h2> <p> With <em>@Index</em> we can specify additional indexes. We can use this to specify uniqueness, function based indexes, compound indexes, platform specific indexes and if the indexes should be created concurrently (Postgres only at this stage). </p> <h2 id="unique">@Index(unique=true)</h2> <p> This defines if the index is <em>unique</em>. This supports simple unique indexes but also function based unique indexes (e.g. unique lower name), compound uniqueness (multiple columns) and platform specific syntax. </p> <p> For simple cases we can alternatively use <code>@Column(unique=true)</code> or <code>@UniqueConstraint</code>. </p> <h2 id="platforms">Platforms</h2> <p> The platforms attribute limits the database platforms that the index should apply to. It is used so that we can define platform specific index definitions. </p> <p> We can alternatively specify platform specific DDL for indexes via extra-dll.xml. </p> <div class="syntax java"><div class="highlight"><pre><span></span><span class="nd">@Index</span><span class="o">(</span><span class="nx">platforms</span> <span class="o">=</span> <span class="nx">Platform</span><span class="o">.</span><span class="na">H2</span><span class="o">,</span> <span class="nx">unique</span> <span class="o">=</span> <span class="kc">true</span><span class="o">,</span> <span class="nx">columnNames</span> <span class="o">=</span> <span class="o">{</span><span class="s">"name"</span><span class="o">})</span> <span class="nd">@Index</span><span class="o">(</span><span class="nx">platforms</span> <span class="o">=</span> <span class="nx">Platform</span><span class="o">.</span><span class="na">POSTGRES</span><span class="o">,</span> <span class="nx">unique</span> <span class="o">=</span> <span class="kc">true</span><span class="o">,</span> <span class="nx">name</span> <span class="o">=</span> <span class="s">"uq_printer_name"</span><span class="o">,</span> <span class="nx">columnNames</span> <span class="o">=</span> <span class="o">{</span><span class="s">"lower(name)"</span><span class="o">})</span> <span class="nd">@Entity</span> <span class="nd">@Table</span><span class="o">(</span><span class="nx">name</span> <span class="o">=</span> <span class="s">"printers"</span><span class="o">)</span> <span class="kd">public</span> <span class="kd">class</span> <span class="nc">DPrinter</span> <span class="kd">extends</span> <span class="n">Model</span> <span class="o">{</span> <span class="o">...</span> </pre></div> </div> <p> In the above example we specify multiple <code>@Index</code> with the Postgres specific one using a function based index <code>lower(name)</code>. </p> <h2 id="concurrent">Concurrent</h2> <p> For Postgres this indicates the index should be created concurrently. For all other platforms this attribute is ignored. </p> <h2 id="definition">Definition</h2> <p> The <em>definition</em> attribute allows use to specify the full DDL to create the index. When we do this we should specify the index name in order to support dropping the index. </p> <div class="syntax java"><div class="highlight"><pre><span></span><span class="nd">@Index</span><span class="o">(</span> <span class="n">platforms</span> <span class="o">=</span> <span class="n">Platform</span><span class="o">.</span><span class="na">POSTGRES</span><span class="o">,</span> <span class="n">name</span> <span class="o">=</span> <span class="s">"ix_detail_lowername"</span><span class="o">,</span> <span class="n">definition</span> <span class="o">=</span> <span class="s">"create index ix_detail_lowername on t_detail using hash (lower(name)) where lower(name) like 'r%'"</span><span class="o">)</span> </pre></div> </div> <p> Often this index DDL will be platform specific and if we are testing against multiple database plaforms we specify the platforms that the index DDL applies to. </p> </article> </div> </div> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script> <script src="/js/site3.js"></script> <script src="/js/search3.js"></script> <script async src="https://www.googletagmanager.com/gtag/js?id=UA-75181644-1"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-75181644-1'); </script> </body> </html>