Skip to content

Annotation

Yong Zhu edited this page Jan 20, 2018 · 1 revision

Annotation configuration

jDialects support below Annotations with same name and meaning of JPA:
@Entity, @Transient, @UniqueConstraint, @GenerationType, @Id, @Index, @SequenceGenerator, @GeneratedValue, @Table, @Column, @TableGenerator

And also below annotation only belong to jDialects: @FKey Define a foreign key @SingleIndex Shortcut Annotation to define a index for single column
@SingleUnique Shortcut Annotation to define a unique constraint for single column
@SingleFKey Shortcut Annotation to define a foreign key for single column

jDialects does not depends on JPA but it support JPA's Annotation by run-time reflection mechanism.

Example of using Annotation configuration:

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); 
	}
}

Full source code see AnnotationTest

Result:

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)