-
Notifications
You must be signed in to change notification settings - Fork 12
实体注解方式配置
jDialect支持11个主要的JPA注解(JPA注解有150多个,个人认为是设计过度,导致过分复杂,对开发无益),可以用来将JPA注解标注的POJO转化为jDialects的Java内存模型,从而输出为DDL或交由ORM程序做处理,因此它也可以充当一个迷你型的JPA解析工具。得益于Java虚拟表模型TableModel的可修改性,对于建立在jDialects基础上的ORM工具如jSqlBox来说,对于一个POJO类,jSqlBox既具有支持固定的JPA注解的功能,也具有在运行期动态修改内存模型的能力,从而实现在运行期动态修改、添加数据库字段、改变关联映射关系,这是Hibernate之类的ORM工具很难做到的。
目前jDialects支持以下JPA注解:
@Entity, @Transient, @UniqueConstraint, @GenerationType, @Id, @Index, @SequenceGenerator, @GeneratedValue, @Table, @Column, @TableGenerator
另外jDialect添加了以下独有的注解,用于更方便DDL生成,因为jDialects的重点和ORM工具不一样,jDialects更偏向于DDL操纵,不太关心实体的关联映射:
@FKey 用于定义外键,其中的ddl参数用于指明这个FKey外键是否参与DDL输出,默认值为true
@SingleIndex 用于定义只针对单例有效的索引,相比于标准的@Index标签,在单个例上定义索引用起来更方便。
@SingleUnique 用于定义只针对单例有效的唯一约束
@SingleFKey 用于定义只针对单例有效的外键约束
以上所有注解jDialects内部已包含,包含与JPA重名的11个注解。jDialects没有对JPA的库依赖,但是它也支持JPA注解,它是在运行期通过反射来判断POJO类使用了JPA还是jDialects自带的注解。
以下示例将一个用注解标注的POJO类转为DDL输出:
public class AnnotationTest extends TestBase {
@Entity
@Table(name = "testpo", //
uniqueConstraints = { @UniqueConstraint(columnNames = { "field1" }),
@UniqueConstraint(name = "unique_cons2", columnNames = { "field1", "field2" }) }, //
indexes = { @Index(columnList = "field1,field2", unique = true),
@Index(name = "index_cons2", columnList = "field1,field2", unique = false) }//
)
@SequenceGenerator(name = "seqID1", sequenceName = "seqName1", initialValue = 1, allocationSize = 10)
@TableGenerator(name = "tableID1", table = "table1", pkColumnName = "pkCol1", valueColumnName = "vcol1", pkColumnValue = "pkcolval1", initialValue = 2, allocationSize = 20)
@FKey(name = "fkey1", ddl=true, columns = { "field1", "field2" }, refs = { "Entity1", "field1", "field2" })
@FKey1(columns = { "field2", "field3" }, refs = { "Entity1", "field1", "field2" })
public static class Entity2 {
@SequenceGenerator(name = "seqID2", sequenceName = "seqName2", initialValue = 2, allocationSize = 20)
@TableGenerator(name = "tableID2", table = "table2", pkColumnName = "pkCol1", valueColumnName = "vcol1", pkColumnValue = "pkcolval1", initialValue = 2, allocationSize = 20)
@Id
@Column(columnDefinition = TypeUtils.VARCHAR, length = 20)
public String field1;
@Column(name = "field2", nullable = false, columnDefinition = TypeUtils.BIGINT)
public String field2;
@GeneratedValue(strategy = GenerationType.TABLE, generator = "CUST_GEN")
@Column(name = "field3", nullable = false, columnDefinition = TypeUtils.BIGINT)
@SingleFKey(name = "singleFkey1", ddl=true, refs = { "Entity1", "field1" })
@SingleIndex
@SingleUnique
public Integer field3;
@Transient
public Integer field4;
@UUID36
public String field5;
public static void config(TableModel tableModel) {
tableModel.getColumn("field7").setColumnName("changedfield7");
tableModel.column("newField9").STRING(10);
}
//getter & Setter
}
@Test
public void ddlOutTest() {
String[] dropAndCreateDDL = Dialect.H2Dialect.toDropAndCreateDDL(ModelUtils.entity2Model(Entity1.class, Entity2.class));
for (String ddl : dropAndCreateDDL)
System.out.println(ddl);
}
}
完整代码请见AnnotationTest
上例的运行结果为:
alter table testpo drop constraint singleFkey1
alter table testpo drop constraint fkey1
alter table testpo drop constraint fk_testpo_field2_field3
drop table table2 if exists
drop table table1 if exists
drop sequence if exists seqName2
drop sequence if exists seqName1
drop table Entity1 if exists
drop table testpo if exists
create table Entity1 ( field1 varchar(255),field2 varchar(255))
create table testpo ( field1 varchar(20),field2 bigint not null,field3 bigint not null,field5 varchar(255),field6 float,changedfield7 double,newField9 varchar(10), primary key (field1))
create unique index IX_testpo_field1_field2 on testpo (field1,field2)
create index index_cons2 on testpo (field1,field2)
create index IX_testpo_field3 on testpo (field3)
alter table testpo add constraint UK_testpo_field1 unique (field1)
alter table testpo add constraint unique_cons2 unique (field1,field2)
alter table testpo add constraint UK_testpo_field3 unique (field3)
create sequence seqName1 start with 1 increment by 10
create sequence seqName2 start with 2 increment by 20
create table table1 (pkCol1 varchar(100),vcol1 bigint )
create table table2 (pkCol1 varchar(100),vcol1 bigint )
alter table testpo add constraint fk_testpo_field2_field3 foreign key (field2,field3) references Entity1 (field1,field2)
alter table testpo add constraint fkey1 foreign key (field1,field2) references Entity1 (field1,field2)
alter table testpo add constraint singleFkey1 foreign key (field3) references Entity1 (field1)