java - H2 index name uniqueness -
i have small problem uniqueness of index names in h2 database. mysql/mariadb possible define index named "x" table , table b @ same time. h2 database not possible, since name of index should unique per database.
it problem me, since have base jpa entity class following property defined:
@org.hibernate.annotations.index(name = "x") protected string x;
it inherited class , b , index creation fails class b following error:
error [main] o.h.tool.hbm2ddl.schemaupdate - hhh000388: unsuccessful: create index x on b(x) error [main] o.h.tool.hbm2ddl.schemaupdate - index "x" exists
is possible tell hibernate automatically create index name or somehow create adapter h2 prefix such index names table names?
although should have database drive schema evolution , use flywaydb migrate schema versions, can override @index want.
i added test on github prove this.
the classes this:
@entity(name = "base") @table(name="base") @inheritance(strategy = inheritancetype.joined) public static abstract class base { @id @generatedvalue(strategy = generationtype.auto) private long id; @transient protected string x; public long getid() { return id; } public string getx() { return x; } public void setx(string x) { this.x = x; } } @entity(name = "childy") @table(name="childy") @discriminatorvalue("y") public static class childy extends base { private string y; @override @org.hibernate.annotations.index(name = "xy") @access(accesstype.property) public string getx() { return x; } } @entity(name = "childz") @table(name="childz") @inheritance(strategy = inheritancetype.joined) @discriminatorvalue("z") public static class childz extends base { private string z; @override @org.hibernate.annotations.index(name = "xz") @access(accesstype.property) public string getx() { return x; } }
the schema generated this:
create table base (id bigint generated default identity (start 1), primary key (id)) create table childy (x varchar(255), y varchar(255), id bigint not null, primary key (id)) create table childz (x varchar(255), z varchar(255), id bigint not null, primary key (id)) create index xy on childy (x) create index xz on childz (x)
this way:
- you retain base class property in domain model (field, getter , setter)
- each table gets own
x
column associated index
the problem can't have field in base class, hibernate attempt create twice. can file jira issue on hibernate , mention hbm schema generation should skip index if it's created.
the elegant solution use proper database schema , remove hbm-ddl schema generation.
Comments
Post a Comment