File tree Expand file tree Collapse file tree 4 files changed +214
-0
lines changed
ebean-querybean/src/test/java/org Expand file tree Collapse file tree 4 files changed +214
-0
lines changed Original file line number Diff line number Diff line change 1+ package org .example .domain ;
2+
3+ import io .ebean .Model ;
4+ import io .ebean .annotation .WhenCreated ;
5+ import io .ebean .annotation .WhenModified ;
6+
7+ import jakarta .persistence .Id ;
8+ import jakarta .persistence .MappedSuperclass ;
9+ import jakarta .persistence .Version ;
10+ import java .sql .Timestamp ;
11+
12+ /**
13+ * Base domain object with Id, version, whenCreated and whenUpdated.
14+ *
15+ * <p>
16+ * Extending Model to enable the 'active record' style.
17+ *
18+ * <p>
19+ * whenCreated and whenUpdated are generally useful for maintaining external search services (like
20+ * elasticsearch) and audit.
21+ */
22+ @ MappedSuperclass
23+ public abstract class GenericBaseModel <T > extends Model {
24+
25+ @ Id
26+ T id ;
27+
28+ @ Version
29+ Long version ;
30+
31+ @ WhenCreated
32+ Timestamp whenCreated ;
33+
34+ @ WhenModified
35+ Timestamp whenUpdated ;
36+
37+ public T getId () {
38+ return id ;
39+ }
40+
41+ public void setId (T id ) {
42+ this .id = id ;
43+ }
44+
45+ public Long getVersion () {
46+ return version ;
47+ }
48+
49+ public void setVersion (Long version ) {
50+ this .version = version ;
51+ }
52+
53+ public Timestamp getWhenCreated () {
54+ return whenCreated ;
55+ }
56+
57+ public void setWhenCreated (Timestamp whenCreated ) {
58+ this .whenCreated = whenCreated ;
59+ }
60+
61+ public Timestamp getWhenUpdated () {
62+ return whenUpdated ;
63+ }
64+
65+ public void setWhenUpdated (Timestamp whenUpdated ) {
66+ this .whenUpdated = whenUpdated ;
67+ }
68+
69+ }
Original file line number Diff line number Diff line change 1+ package org .example .domain ;
2+
3+ import org .example .domain .ProductWithGenericLong ;
4+ import jakarta .persistence .Entity ;
5+ import jakarta .persistence .Table ;
6+ import javax .validation .constraints .Size ;
7+
8+ /**
9+ * Product entity bean.
10+ */
11+ @ Entity
12+ @ Table (name = "o_product" , schema = "foo" )
13+ public class ProductWithGenericLong extends GenericBaseModel <Long > {
14+
15+ @ Size (max = 20 )
16+ String sku ;
17+
18+ String name ;
19+
20+ /**
21+ * Return sku.
22+ */
23+ public String getSku () {
24+ return sku ;
25+ }
26+
27+ /**
28+ * Set sku.
29+ */
30+ public void setSku (String sku ) {
31+ this .sku = sku ;
32+ }
33+
34+ /**
35+ * Return name.
36+ */
37+ public String getName () {
38+ return name ;
39+ }
40+
41+ /**
42+ * Set name.
43+ */
44+ public void setName (String name ) {
45+ this .name = name ;
46+ }
47+
48+ }
Original file line number Diff line number Diff line change 1+ package org .example .domain ;
2+
3+ import org .example .domain .ProductWithGenericLong ;
4+ import jakarta .persistence .Entity ;
5+ import jakarta .persistence .Table ;
6+ import javax .validation .constraints .Size ;
7+
8+ /**
9+ * Product entity bean.
10+ */
11+ @ Entity
12+ @ Table (name = "o_product" , schema = "foo" )
13+ public class ProductWithGenericString extends GenericBaseModel <String > {
14+
15+ @ Size (max = 20 )
16+ String sku ;
17+
18+ String name ;
19+
20+ /**
21+ * Return sku.
22+ */
23+ public String getSku () {
24+ return sku ;
25+ }
26+
27+ /**
28+ * Set sku.
29+ */
30+ public void setSku (String sku ) {
31+ this .sku = sku ;
32+ }
33+
34+ /**
35+ * Return name.
36+ */
37+ public String getName () {
38+ return name ;
39+ }
40+
41+ /**
42+ * Set name.
43+ */
44+ public void setName (String name ) {
45+ this .name = name ;
46+ }
47+
48+ }
Original file line number Diff line number Diff line change 1+ package org .querytest ;
2+
3+ import io .ebean .InTuples ;
4+
5+ import org .example .domain .ProductWithGenericLong ;
6+ import org .example .domain .ProductWithGenericString ;
7+ import org .example .domain .query .QContact ;
8+ import org .example .domain .query .QProductWithGenericLong ;
9+ import org .example .domain .query .QProductWithGenericString ;
10+ import org .junit .jupiter .api .Test ;
11+
12+ import java .time .ZonedDateTime ;
13+
14+ import static org .assertj .core .api .Assertions .assertThat ;
15+
16+ public class QProductWithGenericTest {
17+
18+ @ Test
19+ void findByLongId () {
20+
21+ var entity = new ProductWithGenericLong ();
22+ entity .setId (42L );
23+ entity .setName ("Gadget" );
24+ entity .save ();
25+
26+ var result = new QProductWithGenericLong ()
27+ .id .eq (42L )
28+ .findOne ();
29+
30+ assertThat (result ).isNotNull ();
31+ assertThat (result .getName ()).isEqualTo ("Gadget" );
32+ }
33+
34+ @ Test
35+ void findByStringId () {
36+
37+ var entity = new ProductWithGenericString ();
38+ entity .setId ("1234" );
39+ entity .setName ("Gadget" );
40+ entity .save ();
41+
42+ var result = new QProductWithGenericString ()
43+ .id .eq ("1234" )
44+ .findOne ();
45+
46+ assertThat (result ).isNotNull ();
47+ assertThat (result .getName ()).isEqualTo ("Gadget" );
48+ }
49+ }
You can’t perform that action at this time.
0 commit comments