Remove reading mapping annotations from "Getters" with associated refactoring for performance (#1971)

* #1970 - Change test code only moving mapping annotations to fields

First step in remove support for mapping annotations on "Getters" (so field only target for mapping annotations)

* #1970 - Remove reading mapping annotations from "Getters"

* #1970 - Refactor to use simple annotation lookup where possible

* #1970 - Remove GenerationType.TABLE and SequenceGenerator from type/class

* #1970 - Refactor meta annotation reading

* #1970 - Read @Aggregation as a meta annotation
This commit is contained in:
Rob Bygrave
2020-03-09 16:09:41 +13:00
committed by GitHub
parent 7692d9782a
commit a861e8bdcc
33 changed files with 590 additions and 680 deletions
@@ -1,15 +1,18 @@
package org.tests.basic;
import io.ebean.BaseTestCase;
import io.ebean.annotation.Index;
import io.ebean.annotation.Platform;
import io.ebean.annotation.Where;
import io.ebean.util.AnnotationUtil;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.IndexDefinition;
import io.ebeaninternal.server.deploy.meta.DeployBeanProperty;
import org.junit.Test;
import org.tests.model.basic.ValidationGroupSomething;
import javax.persistence.Entity;
import javax.persistence.MappedSuperclass;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.lang.annotation.ElementType;
@@ -17,6 +20,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -25,6 +30,13 @@ import static org.junit.Assert.assertTrue;
public class TestAnnotationBase extends BaseTestCase {
private final Set<Class<?>> metaAnnotationsFilter = new HashSet<>();
public TestAnnotationBase() {
metaAnnotationsFilter.add(Where.class);
metaAnnotationsFilter.add(Where.List.class);
}
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Where(clause = "SELECT 'mysql' from 1", platforms = Platform.MYSQL)
@@ -34,8 +46,16 @@ public class TestAnnotationBase extends BaseTestCase {
}
@Index(name = "ano_1", columnNames = "direct")
@Index(name = "ano_2", columnNames = "direct")
@MappedSuperclass
public static class MappedBaseEntity {
}
@Index(name = "ano_3", columnNames = "direct")
@Entity
public static class TestAnnotationBaseEntity {
public static class TestAnnotationBaseEntity extends MappedBaseEntity {
@Where(clause = "SELECT 'mysql' from 1", platforms = Platform.MYSQL)
@Where(clause = "SELECT 'h2' from 1", platforms = Platform.H2)
@@ -45,7 +65,6 @@ public class TestAnnotationBase extends BaseTestCase {
@MetaTest
private String meta;
@MetaTest
@Where(clause = "SELECT 'oracle' from 1", platforms = Platform.ORACLE)
private String mixed;
@@ -131,6 +150,13 @@ public class TestAnnotationBase extends BaseTestCase {
assertEquals(40, bp.getDbLength());
}
@Test
public void annotationClassIndexes() throws SecurityException {
BeanDescriptor<TestAnnotationBaseEntity> descriptor = spiEbeanServer().getBeanDescriptor(TestAnnotationBaseEntity.class);
final IndexDefinition[] indexDefinitions = descriptor.getIndexDefinitions();
assertEquals(3, indexDefinitions.length);
}
@Test
public void testNotNullWithGroup() throws SecurityException {
BeanDescriptor<TestAnnotationBaseEntity> descriptor = spiEbeanServer().getBeanDescriptor(TestAnnotationBaseEntity.class);
@@ -147,45 +173,41 @@ public class TestAnnotationBase extends BaseTestCase {
@Test
public void testFindAnnotation() throws NoSuchFieldException, SecurityException {
Field fld = TestAnnotationBaseEntity.class.getDeclaredField("direct");
String s;
s = AnnotationUtil.findAnnotation(fld, Where.class, Platform.MYSQL).clause();
assertEquals("SELECT 'mysql' from 1", s);
Field directFld = TestAnnotationBaseEntity.class.getDeclaredField("direct");
final DeployBeanProperty direct = createProperty(directFld);
s = AnnotationUtil.findAnnotation(fld, Where.class, Platform.H2).clause();
assertEquals("SELECT 'h2' from 1", s);
s = AnnotationUtil.findAnnotation(fld, Where.class, Platform.POSTGRES).clause();
assertEquals("SELECT 'other' from 1", s);
assertEquals("SELECT 'mysql' from 1", where(direct, Platform.MYSQL));
assertEquals("SELECT 'h2' from 1", where(direct, Platform.H2));
assertEquals("SELECT 'other' from 1", where(direct, Platform.POSTGRES));
// meta
fld = TestAnnotationBaseEntity.class.getDeclaredField("meta");
s = AnnotationUtil.findAnnotation(fld, Where.class, Platform.MYSQL).clause();
assertEquals("SELECT 'mysql' from 1", s);
s = AnnotationUtil.findAnnotation(fld, Where.class, Platform.H2).clause();
assertEquals("SELECT 'h2' from 1", s);
s = AnnotationUtil.findAnnotation(fld, Where.class, Platform.POSTGRES).clause();
assertEquals("SELECT 'other' from 1", s);
Field metaFld = TestAnnotationBaseEntity.class.getDeclaredField("meta");
final DeployBeanProperty meta = createProperty(metaFld);
assertEquals("SELECT 'mysql' from 1", where(meta, Platform.MYSQL));
assertEquals("SELECT 'h2' from 1", where(meta, Platform.H2));
assertEquals("SELECT 'other' from 1", where(meta, Platform.POSTGRES));
// mixed
fld = TestAnnotationBaseEntity.class.getDeclaredField("mixed");
Field mixedFld = TestAnnotationBaseEntity.class.getDeclaredField("mixed");
final DeployBeanProperty mixed = createProperty(mixedFld);
s = AnnotationUtil.findAnnotation(fld, Where.class, Platform.MYSQL).clause();
assertEquals("SELECT 'mysql' from 1", s);
assertEquals("SELECT 'mysql' from 1", where(mixed, Platform.MYSQL));
assertEquals("SELECT 'h2' from 1", where(mixed, Platform.H2));
assertEquals("SELECT 'other' from 1", where(mixed, Platform.POSTGRES));
assertEquals("SELECT 'oracle' from 1", where(mixed, Platform.ORACLE));
}
s = AnnotationUtil.findAnnotation(fld, Where.class, Platform.H2).clause();
assertEquals("SELECT 'h2' from 1", s);
private String where(DeployBeanProperty property, Platform platform) {
return property.getMetaAnnotationWhere(platform).clause();
}
s = AnnotationUtil.findAnnotation(fld, Where.class, Platform.POSTGRES).clause();
assertEquals("SELECT 'other' from 1", s);
s = AnnotationUtil.findAnnotation(fld, Where.class, Platform.ORACLE).clause();
assertEquals("SELECT 'oracle' from 1", s);
private DeployBeanProperty createProperty(Field fld) {
DeployBeanProperty directProperty = new DeployBeanProperty(null, null, null);
directProperty.setField(fld);
directProperty.initMetaAnnotations(metaAnnotationsFilter);
return directProperty;
}
}
@@ -12,6 +12,8 @@ import org.junit.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class TestWhereAnnotation extends TransactionalTestCase {
@Test
@@ -29,6 +31,6 @@ public class TestWhereAnnotation extends TransactionalTestCase {
q1.findOne();
String s1 = q1.getGeneratedSql();
Assert.assertTrue(s1.contains("t1.order_date is not null"));
assertThat(s1).contains("t1.order_date is not null");
}
}
@@ -95,8 +95,10 @@ public class TestOnCascadeDeleteChildrenWithCompositeKeys extends BaseTestCase {
@Entity
@Table(name = "em_user")
public static class User {
@Id
private Long id;
private String name;
@OneToMany(cascade = CascadeType.REMOVE)
private Set<UserRole> userRoles;
public User() {
@@ -107,7 +109,6 @@ public class TestOnCascadeDeleteChildrenWithCompositeKeys extends BaseTestCase {
this.name = name;
}
@Id
public Long getId() {
return id;
}
@@ -124,7 +125,6 @@ public class TestOnCascadeDeleteChildrenWithCompositeKeys extends BaseTestCase {
this.name = name;
}
@OneToMany(cascade = CascadeType.REMOVE)
public Set<UserRole> getUserRoles() {
return userRoles;
}
@@ -138,11 +138,18 @@ public class TestOnCascadeDeleteChildrenWithCompositeKeys extends BaseTestCase {
@Table(name = "em_user_role")
public static class UserRole implements Serializable {
private static final long serialVersionUID = 1L;
private UserRolePK pk;
private User user;
private Role role;
@EmbeddedId
private UserRolePK pk;
@ManyToOne
@JoinColumn(name = "user_id", nullable = false, insertable = false, updatable = false)
private User user;
@ManyToOne
@JoinColumn(name = "role_id", nullable = false, insertable = false, updatable = false)
private Role role;
public UserRolePK getPk() {
return pk;
}
@@ -151,8 +158,6 @@ public class TestOnCascadeDeleteChildrenWithCompositeKeys extends BaseTestCase {
this.pk = pk;
}
@ManyToOne
@JoinColumn(name = "user_id", nullable = false, insertable = false, updatable = false)
public User getUser() {
return user;
}
@@ -161,8 +166,6 @@ public class TestOnCascadeDeleteChildrenWithCompositeKeys extends BaseTestCase {
this.user = user;
}
@ManyToOne
@JoinColumn(name = "role_id", nullable = false, insertable = false, updatable = false)
public Role getRole() {
return role;
}
@@ -213,11 +216,12 @@ public class TestOnCascadeDeleteChildrenWithCompositeKeys extends BaseTestCase {
@Entity
@Table(name = "em_role")
public static class Role {
@Id
private Long id;
private String name;
@OneToMany(cascade = CascadeType.REMOVE)
private Set<UserRole> userRoles;
@Id
public Long getId() {
return id;
}
@@ -234,7 +238,6 @@ public class TestOnCascadeDeleteChildrenWithCompositeKeys extends BaseTestCase {
this.name = name;
}
@OneToMany(cascade = CascadeType.REMOVE)
public Set<UserRole> getUserRoles() {
return userRoles;
}
@@ -4,10 +4,8 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
@Entity
@SequenceGenerator(name = "SEQ_NAME", sequenceName = GenKeySequence.SEQUENCE_NAME)
public class GenKeySequence {
public final static String SEQUENCE_NAME = "SEQ";
@@ -1,31 +0,0 @@
package org.tests.idkeys.db;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class GenKeyTable {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
private String description;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
@@ -28,6 +28,7 @@ public class DMachineUse extends Model {
private long timeSecs;
@Decimal93
private BigDecimal fuel;
@Version
@@ -0,0 +1,16 @@
package org.tests.model.aggregation;
import javax.persistence.Column;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Example meta annotation for <code>@Column</code>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Column(precision = 9, scale = 3)
public @interface Decimal93 {
}
@@ -18,18 +18,24 @@ public class Person implements Serializable {
private static final long serialVersionUID = 495045977245770183L;
@Id
@GeneratedValue(strategy = javax.persistence.GenerationType.AUTO)
@SequenceGenerator(name = "PERSONS_SEQ", initialValue = 1000, allocationSize = 40)
@Column(name = "ID", unique = true, nullable = false)
private Long id;
@Column(name = "SURNAME", nullable = false, unique = false, columnDefinition = "varchar(64)")
private String surname;
@Column(name = "NAME", nullable = false, unique = false, columnDefinition = "varchar(64)")
private String name;
@OneToMany(targetEntity = Phone.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "person")
private List<Phone> phones;
public Person() {
}
@Id
@GeneratedValue(strategy = javax.persistence.GenerationType.AUTO)
@SequenceGenerator(name = "PERSONS_SEQ", initialValue = 1000, allocationSize = 40)
@Column(name = "ID", unique = true, nullable = false)
public Long getId() {
return id;
}
@@ -38,7 +44,6 @@ public class Person implements Serializable {
this.id = id;
}
@Column(name = "SURNAME", nullable = false, unique = false, columnDefinition = "varchar(64)")
public String getSurname() {
return surname;
}
@@ -47,7 +52,6 @@ public class Person implements Serializable {
this.surname = surname;
}
@Column(name = "NAME", nullable = false, unique = false, columnDefinition = "varchar(64)")
public String getName() {
return name;
}
@@ -56,7 +60,6 @@ public class Person implements Serializable {
this.name = name;
}
@OneToMany(targetEntity = Phone.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "person")
public List<Phone> getPhones() {
return phones;
}
@@ -18,16 +18,21 @@ public class Phone implements Serializable {
private static final long serialVersionUID = -326610269092956952L;
@Id
@GeneratedValue(strategy = javax.persistence.GenerationType.AUTO)
@Column(name = "id", unique = true, nullable = false)
private Long id;
@Column(name = "phone_number", nullable = false, unique = true, columnDefinition = "varchar(7)")
private String phoneNumber;
@ManyToOne(targetEntity = Person.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "person_id", nullable = false)
private Person person;
public Phone() {
}
@Id
@GeneratedValue(strategy = javax.persistence.GenerationType.AUTO)
@Column(name = "id", unique = true, nullable = false)
public Long getId() {
return id;
}
@@ -36,7 +41,6 @@ public class Phone implements Serializable {
this.id = id;
}
@Column(name = "phone_number", nullable = false, unique = true, columnDefinition = "varchar(7)")
public String getPhoneNumber() {
return phoneNumber;
}
@@ -46,8 +50,6 @@ public class Phone implements Serializable {
}
@NotNull
@ManyToOne(targetEntity = Person.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "person_id", nullable = false)
public Person getPerson() {
return person;
}
@@ -1,6 +1,5 @@
package org.tests.model.embedded;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
@@ -18,7 +17,6 @@ public class RevisionId {
this.primaryId = primaryId;
}
@Column(name = "revision")
public Integer getRevision() {
return revision;
}