├── e-groovy ├── .gitignore └── src │ ├── main │ ├── resources │ │ ├── META-INF │ │ │ └── services │ │ │ │ └── org.codehaus.groovy.runtime.ExtensionModule │ │ ├── ebean.properties │ │ └── logback.xml │ └── groovy │ │ └── org │ │ ├── example │ │ ├── domain │ │ │ ├── Product.groovy │ │ │ ├── OrderDetail.groovy │ │ │ ├── Country.groovy │ │ │ ├── Contact.groovy │ │ │ ├── BaseModel.groovy │ │ │ ├── Address.groovy │ │ │ ├── Order.groovy │ │ │ └── Customer.groovy │ │ └── service │ │ │ ├── LoadAgentAtRuntime.groovy │ │ │ └── LoadCustomerService.groovy │ │ └── avaje │ │ └── ebean │ │ └── groovy │ │ ├── StaticEbeanExtensionModule.groovy │ │ └── EbeanExtensionModule.groovy │ └── test │ └── groovy │ └── org │ └── example │ └── service │ ├── LoadCustomerServiceTest.groovy │ ├── ExampleUseExtensionMethod.groovy │ ├── ExampleBaseTestCase.groovy │ ├── ExampleGroovyNullSafeTraversal.groovy │ ├── ExampleUsingPathProperties.groovy │ ├── LargeQueryWithFindIterateTest.groovy │ └── InsertCustomerTest.groovy ├── a-basic ├── src │ ├── main │ │ ├── resources │ │ │ ├── META-INF │ │ │ │ ├── ebean.mf │ │ │ │ └── ebean-typequery.mf │ │ │ ├── ebean.properties │ │ │ └── logback.xml │ │ └── java │ │ │ └── org │ │ │ └── example │ │ │ ├── domain │ │ │ ├── SomePojo.java │ │ │ ├── finder │ │ │ │ ├── ProductFinder.java │ │ │ │ ├── ContactNoteFinder.java │ │ │ │ ├── OrderDetailFinder.java │ │ │ │ ├── CustomerFinder.java │ │ │ │ ├── AddressFinder.java │ │ │ │ ├── CountryFinder.java │ │ │ │ ├── ContactFinder.java │ │ │ │ └── OrderFinder.java │ │ │ ├── ContactNote.java │ │ │ ├── Product.java │ │ │ ├── Country.java │ │ │ ├── BaseModel.java │ │ │ ├── Contact.java │ │ │ ├── OrderDetail.java │ │ │ ├── Address.java │ │ │ └── Customer.java │ │ │ └── service │ │ │ ├── LoadAgentAtRuntime.java │ │ │ └── ExampleUsingPathProperties.java │ └── test │ │ ├── resources │ │ ├── test.init.sql │ │ ├── test.seed.sql │ │ └── test-ebean.properties │ │ └── java │ │ ├── org │ │ └── example │ │ │ ├── GenerateMigrationTest.java │ │ │ ├── domain │ │ │ ├── ExQueryJoins.java │ │ │ ├── ExPartialObjects.java │ │ │ ├── LargeQueryWithFindIterateTest.java │ │ │ ├── ExFindIterate.java │ │ │ ├── InsertCustomerTest.java │ │ │ └── finder │ │ │ │ └── OrderFinderTest.java │ │ │ └── ExampleBaseTestCase.java │ │ └── main │ │ └── MainQueryBeanGenerator.java ├── .gitignore ├── h2-drop.sql ├── pg-drop-all.sql ├── h2-drop-all.sql └── ebean-autotune.xml ├── x-dbmigration ├── h2-drop-all.sql ├── h2-create-all.sql ├── src │ ├── test │ │ ├── java │ │ │ ├── main │ │ │ │ └── MainDbMigration.java │ │ │ └── org │ │ │ │ └── example │ │ │ │ └── domain │ │ │ │ └── CustomerTest.java │ │ └── resources │ │ │ ├── ebean-test.properties │ │ │ └── logback-test.xml │ └── main │ │ ├── resources │ │ └── ebean.properties │ │ └── java │ │ └── org │ │ └── example │ │ └── domain │ │ ├── Customer.java │ │ └── BaseModel.java └── pom.xml ├── x-postgres-features ├── src │ ├── main │ │ ├── resources │ │ │ ├── META-INF │ │ │ │ └── ebean-typequery.mf │ │ │ └── ebean.properties │ │ └── java │ │ │ └── org │ │ │ └── example │ │ │ └── domain │ │ │ ├── User.java │ │ │ ├── SimpleDoc.java │ │ │ ├── SimpleDocUsingJsonNode.java │ │ │ ├── BaseModel.java │ │ │ └── Customer.java │ └── test │ │ ├── java │ │ ├── main │ │ │ └── MainDbMigration.java │ │ └── org │ │ │ └── example │ │ │ ├── RawJdbcIsValidTest.java │ │ │ ├── HstoreJsonTest.java │ │ │ ├── AutoCommitTest.java │ │ │ ├── ExampleBaseTestCase.java │ │ │ ├── BatchInsertTest.java │ │ │ ├── ListTableNamesTest.java │ │ │ ├── RawJdbcInsertWithGeneratedKeyTest.java │ │ │ ├── SlashInLikeTest.java │ │ │ ├── domain │ │ │ └── type │ │ │ │ └── ScalarTypeJsonNodeTextTest.java │ │ │ ├── RawJdbcHstoreTest.java │ │ │ ├── storedprocedure │ │ │ └── CallableSqlTest.java │ │ │ ├── SimpleDocWithJsonNodeTest.java │ │ │ └── BasicInsertTest.java │ │ └── resources │ │ ├── createStoredProcedure.sql │ │ └── logback.xml ├── mysql-drop.sql ├── pg-drop.sql ├── pg-drop-all.sql ├── ora-drop-all.sql ├── mysql-create.sql ├── pg-create-backup.sql ├── pg-create.sql ├── pg-create-all.sql └── ora-create-all.sql ├── e-eventadapters ├── h2-drop.sql ├── h2-create.sql └── src │ ├── main │ ├── resources │ │ ├── ebean.properties │ │ └── logback-test.xml │ └── java │ │ └── org │ │ └── example │ │ └── domain │ │ ├── User.java │ │ ├── BusinessBeanPersistAdapter.java │ │ └── AbstractModel.java │ └── test │ ├── java │ └── org │ │ └── example │ │ └── domain │ │ ├── ExampleBaseTestCase.java │ │ └── UserTest.java │ └── resources │ └── logback-test.xml ├── .gitignore ├── e-kotlin-maven └── src │ ├── main │ ├── java │ │ └── org │ │ │ └── example │ │ │ ├── extension │ │ │ └── Extension.kt │ │ │ ├── service │ │ │ ├── PlayWithAddress.kt │ │ │ ├── LoadAgentAtRuntime.java │ │ │ ├── WithTransactional.kt │ │ │ └── OrderService.kt │ │ │ ├── module │ │ │ ├── DbModule.kt │ │ │ └── EbeanServerProvider.kt │ │ │ └── domain │ │ │ ├── Product.kt │ │ │ ├── Address.kt │ │ │ ├── Country.kt │ │ │ ├── Order.kt │ │ │ ├── BaseModel.kt │ │ │ ├── Contact.kt │ │ │ ├── OrderDetail.kt │ │ │ └── Customer.kt │ └── resources │ │ ├── ebean.properties │ │ └── logback.xml │ └── test │ └── java │ └── org │ └── example │ ├── service │ ├── LoadExampleDataTest.kt │ ├── PlayWithAddressTest.kt │ ├── OrderServiceTest.kt │ └── WithTransactionalTest.kt │ ├── domain │ ├── SimpleTest.kt │ ├── ExamplePartialObjectQueryTest.kt │ ├── CreateCountryTest.kt │ ├── ExampleUsingPathProperties.kt │ ├── LargeQueryWithFindIterateTest.kt │ └── InsertCustomerTest.kt │ └── ExampleBaseTestCase.java ├── x-postgres-history ├── pg-drop.sql └── src │ ├── test │ ├── java │ │ ├── main │ │ │ └── MainDbMigration.java │ │ └── org │ │ │ └── example │ │ │ └── domain │ │ │ └── CustomerTest.java │ └── resources │ │ └── logback-test.xml │ ├── main │ ├── java │ │ └── org │ │ │ └── example │ │ │ └── domain │ │ │ ├── finder │ │ │ ├── ProductFinder.java │ │ │ ├── CustomerFinder.java │ │ │ ├── AddressFinder.java │ │ │ ├── CountryFinder.java │ │ │ └── ContactFinder.java │ │ │ ├── Product.java │ │ │ ├── excludem2m │ │ │ ├── FeatureExcludeM2M.java │ │ │ ├── ContactExcludeM2M.java │ │ │ └── CustomerExcludeM2M.java │ │ │ ├── Feature.java │ │ │ ├── Country.java │ │ │ ├── BaseModel.java │ │ │ ├── Contact.java │ │ │ ├── Address.java │ │ │ └── Customer.java │ └── resources │ │ └── ebean.properties │ ├── extra-ddl-address.sql │ ├── extra-ddl-customer.sql │ ├── extra-ddl-customer_feature.sql │ └── extra-ddl.sql ├── README.md ├── x-json-features ├── src │ ├── test │ │ └── java │ │ │ └── org │ │ │ └── example │ │ │ └── PlainJsonParseTest.java │ └── main │ │ └── java │ │ └── org │ │ └── example │ │ └── EJson.java └── pom.xml └── e-spring └── pom.xml /e-groovy/.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | h2-create.sql 3 | h2-drop.sql 4 | 5 | -------------------------------------------------------------------------------- /a-basic/src/main/resources/META-INF/ebean.mf: -------------------------------------------------------------------------------- 1 | packages: org.example.domain 2 | 3 | -------------------------------------------------------------------------------- /a-basic/src/main/resources/META-INF/ebean-typequery.mf: -------------------------------------------------------------------------------- 1 | packages: org.example.domain.query 2 | 3 | -------------------------------------------------------------------------------- /x-dbmigration/h2-drop-all.sql: -------------------------------------------------------------------------------- 1 | drop table if exists customer; 2 | drop sequence if exists customer_seq; 3 | 4 | -------------------------------------------------------------------------------- /x-postgres-features/src/main/resources/META-INF/ebean-typequery.mf: -------------------------------------------------------------------------------- 1 | packages: org.example.domain.query 2 | 3 | -------------------------------------------------------------------------------- /x-postgres-features/mysql-drop.sql: -------------------------------------------------------------------------------- 1 | SET FOREIGN_KEY_CHECKS=0; 2 | 3 | drop table p_customer; 4 | 5 | SET FOREIGN_KEY_CHECKS=1; 6 | 7 | -------------------------------------------------------------------------------- /a-basic/src/test/resources/test.init.sql: -------------------------------------------------------------------------------- 1 | -- Do nothing 2 | create table junk (acol varchar(10)); 3 | create table junk_two (acol varchar(10)); 4 | -------------------------------------------------------------------------------- /e-eventadapters/h2-drop.sql: -------------------------------------------------------------------------------- 1 | SET REFERENTIAL_INTEGRITY FALSE; 2 | 3 | drop table if exists e_user; 4 | 5 | SET REFERENTIAL_INTEGRITY TRUE; 6 | 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | #Intellij project files 2 | *.iml 3 | *.ipr 4 | *.iws 5 | .idea/ 6 | 7 | .classpath 8 | .project 9 | .settings/ 10 | 11 | target/ 12 | log/ 13 | logs/ 14 | 15 | -------------------------------------------------------------------------------- /a-basic/.gitignore: -------------------------------------------------------------------------------- 1 | #Intellij project files 2 | *.iml 3 | *.ipr 4 | *.iws 5 | .idea/ 6 | 7 | .classpath 8 | .project 9 | .settings/ 10 | 11 | target/ 12 | log/ 13 | logs/ 14 | 15 | -------------------------------------------------------------------------------- /a-basic/src/test/resources/test.seed.sql: -------------------------------------------------------------------------------- 1 | insert into o_country (code,name) values ('US','United States'); 2 | insert into o_country (code,name) values ('CA','Canada'); 3 | 4 | insert into junk (acol) values ('a'); 5 | 6 | -------------------------------------------------------------------------------- /x-postgres-features/pg-drop.sql: -------------------------------------------------------------------------------- 1 | drop table if exists p_customer cascade; 2 | 3 | drop table if exists p_doc cascade; 4 | 5 | drop table if exists p_doc_jsonnode cascade; 6 | 7 | drop table if exists p_user cascade; 8 | 9 | -------------------------------------------------------------------------------- /x-postgres-features/pg-drop-all.sql: -------------------------------------------------------------------------------- 1 | drop table if exists p_customer cascade; 2 | 3 | drop table if exists p_doc cascade; 4 | 5 | drop table if exists p_doc_jsonnode cascade; 6 | 7 | drop table if exists p_user cascade; 8 | 9 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/main/java/org/example/extension/Extension.kt: -------------------------------------------------------------------------------- 1 | package org.example.extension 2 | 3 | import org.slf4j.LoggerFactory 4 | 5 | /** 6 | * Helper function to create a Logger. 7 | */ 8 | fun loggerFor(clazz: Class) = LoggerFactory.getLogger(clazz) 9 | -------------------------------------------------------------------------------- /e-groovy/src/main/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule: -------------------------------------------------------------------------------- 1 | moduleName = GroovyEbeanExtensions 2 | moduleVersion = 1.1 3 | extensionClasses = org.avaje.ebean.groovy.EbeanExtensionModule 4 | staticExtensionClasses = org.avaje.ebean.groovy.StaticEbeanExtensionModule 5 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/main/java/org/example/service/PlayWithAddress.kt: -------------------------------------------------------------------------------- 1 | package org.example.service 2 | 3 | import org.example.domain.Address 4 | 5 | public class PlayWithAddress { 6 | 7 | fun findAll(): MutableList
? { 8 | return Address.findList(); 9 | } 10 | 11 | } -------------------------------------------------------------------------------- /x-postgres-history/pg-drop.sql: -------------------------------------------------------------------------------- 1 | drop table if exists o_address cascade; 2 | 3 | drop table if exists contact cascade; 4 | 5 | drop table if exists country cascade; 6 | 7 | drop table if exists customer cascade; 8 | 9 | drop table if exists customer_feature cascade; 10 | 11 | drop table if exists feature cascade; 12 | 13 | drop table if exists product cascade; 14 | 15 | -------------------------------------------------------------------------------- /x-postgres-features/ora-drop-all.sql: -------------------------------------------------------------------------------- 1 | drop table p_customer cascade constraints purge; 2 | drop sequence p_customer_seq; 3 | 4 | drop table p_doc cascade constraints purge; 5 | drop sequence p_doc_seq; 6 | 7 | drop table p_doc_jsonnode cascade constraints purge; 8 | drop sequence p_doc_jsonnode_seq; 9 | 10 | drop table p_user cascade constraints purge; 11 | drop sequence p_user_seq; 12 | 13 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/test/java/org/example/service/LoadExampleDataTest.kt: -------------------------------------------------------------------------------- 1 | package org.example.service 2 | 3 | import org.junit.Test 4 | import org.example.ExampleBaseTestCase 5 | 6 | /** 7 | * Created by rob on 23/10/14. 8 | */ 9 | class LoadExampleDataTest : ExampleBaseTestCase() { 10 | 11 | @Test fun just_run_it() { 12 | 13 | val load = LoadExampleData() 14 | load.load() 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /e-kotlin-maven/src/test/java/org/example/service/PlayWithAddressTest.kt: -------------------------------------------------------------------------------- 1 | package org.example.service 2 | 3 | import org.junit.Test 4 | import kotlin.test.assertEquals 5 | 6 | class PlayWithAddressTest { 7 | 8 | @Test 9 | fun testFindAll() { 10 | 11 | val play = PlayWithAddress() 12 | val addresses = play.findAll(); 13 | 14 | assertEquals(0, addresses?.size) 15 | } 16 | } -------------------------------------------------------------------------------- /e-groovy/src/test/groovy/org/example/service/LoadCustomerServiceTest.groovy: -------------------------------------------------------------------------------- 1 | package org.example.service 2 | 3 | import org.junit.Test 4 | 5 | /** 6 | */ 7 | class LoadCustomerServiceTest extends ExampleBaseTestCase { 8 | 9 | @Test 10 | void test() { 11 | 12 | LoadCustomerService loadCustomerService = new LoadCustomerService(); 13 | loadCustomerService.loadSome(); 14 | 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /x-postgres-features/src/test/java/main/MainDbMigration.java: -------------------------------------------------------------------------------- 1 | package main; 2 | 3 | import com.avaje.ebean.dbmigration.DbMigration; 4 | 5 | import java.io.IOException; 6 | 7 | /** 8 | * 9 | */ 10 | public class MainDbMigration { 11 | 12 | public static void main(String[] args) throws IOException { 13 | 14 | DbMigration dbMigration = new DbMigration(); 15 | dbMigration.generateMigration(); 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /x-dbmigration/h2-create-all.sql: -------------------------------------------------------------------------------- 1 | create table customer ( 2 | id bigint not null, 3 | name varchar(50) not null, 4 | short_desc varchar(255), 5 | when_created timestamp not null, 6 | when_modified timestamp not null, 7 | version bigint not null, 8 | constraint pk_customer primary key (id) 9 | ); 10 | create sequence customer_seq; 11 | 12 | -------------------------------------------------------------------------------- /x-postgres-history/src/test/java/main/MainDbMigration.java: -------------------------------------------------------------------------------- 1 | package main; 2 | 3 | import com.avaje.ebean.dbmigration.DbMigration; 4 | 5 | import java.io.IOException; 6 | 7 | public class MainDbMigration { 8 | 9 | public static void main(String[] args) throws IOException { 10 | 11 | DbMigration dbMigration = new DbMigration(); 12 | //dbMigration.setPathToResources("src/main/resources"); 13 | 14 | //dbMigration.setPlatform(DbPlatformName.POSTGRES); 15 | dbMigration.generateMigration(); 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /x-postgres-features/mysql-create.sql: -------------------------------------------------------------------------------- 1 | create table p_customer ( 2 | id bigint auto_increment not null, 3 | inactive tinyint(1) default 0, 4 | name varchar(100), 5 | registered datetime, 6 | comments varchar(1000), 7 | version bigint not null, 8 | when_created datetime not null, 9 | when_updated datetime not null, 10 | constraint pk_p_customer primary key (id)) 11 | ; 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /a-basic/src/test/resources/test-ebean.properties: -------------------------------------------------------------------------------- 1 | 2 | 3 | ebean.ddl.generate=true 4 | ebean.ddl.run=true 5 | ebean.ddl.createOnly=true 6 | ebean.ddl.initsql=test.init.sql 7 | ebean.ddl.seedsql=test.seed.sql 8 | 9 | #migration.platform=postgres 10 | #migration.generate=true 11 | #migration.version=1.2.1_1 12 | #migration.name=some comment 13 | 14 | #datasource.default=h2 15 | 16 | datasource.db.username=sa 17 | datasource.db.password= 18 | datasource.db.databaseUrl=jdbc:h2:mem:tests 19 | datasource.db.databaseDriver=org.h2.Driver 20 | 21 | 22 | -------------------------------------------------------------------------------- /e-eventadapters/h2-create.sql: -------------------------------------------------------------------------------- 1 | create table e_user ( 2 | id varchar(40) not null, 3 | creator_id varchar(255), 4 | creator_name varchar(255), 5 | modifier_id varchar(255), 6 | modifier_name varchar(255), 7 | name varchar(255), 8 | age integer, 9 | create_time timestamp not null, 10 | modify_time timestamp not null, 11 | constraint pk_e_user primary key (id)) 12 | ; 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /x-postgres-history/src/main/java/org/example/domain/finder/ProductFinder.java: -------------------------------------------------------------------------------- 1 | package org.example.domain.finder; 2 | 3 | import com.avaje.ebean.Model; 4 | import org.example.domain.Product; 5 | 6 | /** 7 | * Add finder methods here. 8 | */ 9 | public class ProductFinder extends Model.Finder { 10 | 11 | public ProductFinder() { 12 | super(Product.class); 13 | } 14 | 15 | /** 16 | * Find by SKU. 17 | */ 18 | public Product bySku(String sku) { 19 | return where().eq("sku", sku).findUnique(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /e-groovy/src/main/groovy/org/example/domain/Product.groovy: -------------------------------------------------------------------------------- 1 | package org.example.domain 2 | 3 | import groovy.transform.CompileStatic 4 | 5 | import javax.persistence.Entity 6 | import javax.persistence.Table 7 | import javax.validation.constraints.Size 8 | 9 | /** 10 | * 11 | * @author: Richard Vowles - https://plus.google.com/+RichardVowles 12 | * Product entity bean. 13 | */ 14 | @Entity 15 | @Table(name = "o_product") 16 | @CompileStatic 17 | class Product extends BaseModel { 18 | @Size(max = 20) 19 | String sku 20 | 21 | String name 22 | } 23 | -------------------------------------------------------------------------------- /a-basic/src/main/resources/ebean.properties: -------------------------------------------------------------------------------- 1 | 2 | #load.properties.override=${home}/config/myapp.ebean.properties 3 | 4 | migration.platform=postgres 5 | #migration.generate=true 6 | #migration.version=1.2.1_1 7 | #migration.name=some comment 8 | 9 | datasource.default=db 10 | 11 | #ebean.autotune.profiling=true 12 | ebean.autotune.queryTuning=true 13 | 14 | datasource.db.username=test 15 | datasource.db.password=test 16 | datasource.db.databaseUrl=jdbc:postgresql://127.0.0.1:5432/test 17 | datasource.db.databaseDriver=org.postgresql.Driver 18 | 19 | 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | avaje-ebeanorm-examples 2 | ======================= 3 | 4 | Multiple example projects demonstrating use of ebean features 5 | 6 | * a-basic - This project is a simple project 7 | * a-bootup-programmatic - This project uses ServerConfig and EbeanServerFactory to programmatically create an EbeanServer 8 | * a-bootup-spring-simple - A simple Spring FactoryBean implementation, use @Inject and Ebean singleton 9 | * a-bootup-spring-xml - Use avaje-ebeanorm-spring to create a EbeanServer to @Inject 10 | * b-batch-insert - Example of batch insert processing techniques 11 | -------------------------------------------------------------------------------- /a-basic/src/main/java/org/example/domain/SomePojo.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import java.util.ArrayList; 4 | 5 | import javax.validation.constraints.AssertTrue; 6 | import javax.xml.ws.Action; 7 | import javax.xml.ws.soap.Addressing; 8 | 9 | @Addressing 10 | public class SomePojo { 11 | 12 | @AssertTrue 13 | String name; 14 | 15 | ArrayList foos = new ArrayList<>(); 16 | 17 | @Action 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public void setName(String name) { 23 | this.name = name; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /a-basic/src/test/java/org/example/GenerateMigrationTest.java: -------------------------------------------------------------------------------- 1 | package org.example; 2 | 3 | import com.avaje.ebean.Ebean; 4 | import org.junit.Test; 5 | 6 | public class GenerateMigrationTest extends ExampleBaseTestCase { 7 | 8 | @Test 9 | public void generate() { 10 | 11 | System.setProperty("ddl.migration.generate", "true"); 12 | System.setProperty("ddl.migration.version", "1.2.2_4"); 13 | System.setProperty("ddl.migration.name", "foo"); 14 | 15 | // migration will be run on EbeanServer instance start 16 | Ebean.getDefaultServer(); 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /x-postgres-history/src/main/java/org/example/domain/finder/CustomerFinder.java: -------------------------------------------------------------------------------- 1 | package org.example.domain.finder; 2 | 3 | import com.avaje.ebean.Model; 4 | import org.example.domain.Customer; 5 | 6 | /** 7 | * Add finder methods here. 8 | */ 9 | public class CustomerFinder extends Model.Finder { 10 | 11 | public CustomerFinder() { 12 | super(Customer.class); 13 | } 14 | 15 | /** 16 | * Find by name equal (case insensitive). 17 | */ 18 | public Customer byName(String name) { 19 | return where().ieq("name", name).findUnique(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /a-basic/src/main/java/org/example/service/LoadAgentAtRuntime.java: -------------------------------------------------------------------------------- 1 | package org.example.service; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | public class LoadAgentAtRuntime { 7 | 8 | protected static Logger logger = LoggerFactory.getLogger(LoadAgentAtRuntime.class); 9 | 10 | // static { 11 | // logger.debug("... preStart"); 12 | // if (!AgentLoader.loadAgentFromClasspath("avaje-ebeanorm-agent","debug=1;packages=org.example.**")) { 13 | // logger.info("avaje-ebeanorm-agent not found in classpath - not dynamically loaded"); 14 | // } 15 | // } 16 | } 17 | -------------------------------------------------------------------------------- /x-postgres-history/src/main/resources/ebean.properties: -------------------------------------------------------------------------------- 1 | #load.properties.override=${home}/config/myapp.ebean.properties 2 | 3 | #ebean.ddl.generate=true 4 | #ebean.ddl.run=true 5 | 6 | datasource.default=pg 7 | 8 | datasource.pg.username=hist 9 | datasource.pg.password=hist 10 | datasource.pg.databaseUrl=jdbc:postgresql://127.0.0.1:5432/hist 11 | datasource.pg.databaseDriver=org.postgresql.Driver 12 | 13 | datasource.ora.username=mig 14 | datasource.ora.password=mig 15 | datasource.ora.databaseUrl=jdbc:oracle:thin:@//127.0.0.1:1521/orcl 16 | datasource.ora.databaseDriver=oracle.jdbc.driver.OracleDriver -------------------------------------------------------------------------------- /x-postgres-history/src/main/java/org/example/domain/finder/AddressFinder.java: -------------------------------------------------------------------------------- 1 | package org.example.domain.finder; 2 | 3 | import com.avaje.ebean.Model; 4 | import org.example.domain.Address; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * Add finder methods here. 10 | */ 11 | public class AddressFinder extends Model.Finder { 12 | 13 | public AddressFinder() { 14 | super(Address.class); 15 | } 16 | 17 | /** 18 | * Find all addresses with city like. 19 | */ 20 | public List
byCityLike(String city) { 21 | return where().like("city", city).findList(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/main/java/org/example/module/DbModule.kt: -------------------------------------------------------------------------------- 1 | package org.example.module 2 | 3 | import com.avaje.ebean.EbeanServer 4 | import com.google.inject.AbstractModule 5 | import org.example.extension.loggerFor 6 | import org.example.service.OrderService 7 | 8 | /** 9 | * 10 | */ 11 | public class DbModule : AbstractModule() { 12 | 13 | private val log = loggerFor(javaClass) 14 | 15 | override fun configure() { 16 | 17 | log.debug("configure ...") 18 | 19 | bind(OrderService::class.java) 20 | bind(EbeanServer::class.java) 21 | .toProvider(EbeanServerProvider::class.java).asEagerSingleton() 22 | } 23 | } -------------------------------------------------------------------------------- /x-postgres-history/src/main/java/org/example/domain/finder/CountryFinder.java: -------------------------------------------------------------------------------- 1 | package org.example.domain.finder; 2 | 3 | import com.avaje.ebean.Model; 4 | import org.example.domain.Country; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * Add finder methods here. 10 | */ 11 | public class CountryFinder extends Model.Finder { 12 | 13 | public CountryFinder() { 14 | super(Country.class); 15 | } 16 | 17 | /** 18 | * Return countries with name like (case insensitive). 19 | */ 20 | public List byNameLike(String name) { 21 | return where().ilike("name", name).findList(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/main/java/org/example/service/LoadAgentAtRuntime.java: -------------------------------------------------------------------------------- 1 | package org.example.service; 2 | 3 | import org.avaje.agentloader.AgentLoader; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | 7 | public class LoadAgentAtRuntime { 8 | 9 | protected static Logger logger = LoggerFactory.getLogger(LoadAgentAtRuntime.class); 10 | 11 | static { 12 | logger.debug("... preStart"); 13 | if (!AgentLoader.loadAgentFromClasspath("avaje-ebeanorm-agent","debug=1;packages=org.example.**")) { 14 | logger.info("avaje-ebeanorm-agent not found in classpath - not dynamically loaded"); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /e-groovy/src/main/groovy/org/example/domain/OrderDetail.groovy: -------------------------------------------------------------------------------- 1 | package org.example.domain 2 | 3 | import groovy.transform.CompileStatic 4 | 5 | import javax.persistence.Entity 6 | import javax.persistence.ManyToOne 7 | import javax.persistence.Table 8 | 9 | /** 10 | * 11 | * @author: Richard Vowles - https://plus.google.com/+RichardVowles 12 | * Order Detail entity bean. 13 | */ 14 | @Entity 15 | @Table(name = "o_order_detail") 16 | @CompileStatic 17 | class OrderDetail extends BaseModel { 18 | @ManyToOne 19 | Order order 20 | 21 | Integer orderQty 22 | 23 | Integer shipQty 24 | 25 | Double unitPrice 26 | 27 | @ManyToOne 28 | Product product 29 | } 30 | -------------------------------------------------------------------------------- /e-groovy/src/test/groovy/org/example/service/ExampleUseExtensionMethod.groovy: -------------------------------------------------------------------------------- 1 | package org.example.service 2 | 3 | import com.avaje.ebean.Ebean 4 | import org.example.domain.Customer 5 | import org.junit.Test 6 | 7 | /** 8 | * Created by rob on 22/10/14. 9 | */ 10 | class ExampleUseExtensionMethod extends ExampleBaseTestCase { 11 | 12 | @Test 13 | void test_run() { 14 | 15 | // e.g. run a closure wrapped in a transaction scope 16 | Ebean.run { 17 | 18 | Customer one = new Customer(name:'one') 19 | one.save(); 20 | 21 | Customer two = new Customer(name:'two', registered: new Date()) 22 | two.save() 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/main/java/org/example/service/WithTransactional.kt: -------------------------------------------------------------------------------- 1 | package org.example.service 2 | 3 | import com.avaje.ebean.annotation.Transactional 4 | import org.example.domain.Customer 5 | import org.example.domain.Product 6 | 7 | /** 8 | * Uses @Transactional and byte code enhancement. 9 | */ 10 | public class WithTransactional { 11 | 12 | @Transactional 13 | fun performInTransaction() { 14 | 15 | val custJim = Customer() 16 | custJim.name = "ShouldNotFindMe" 17 | custJim.save() 18 | 19 | val prod = Product() 20 | prod.sku = "78G" 21 | prod.name = "Bulldozer" 22 | prod.save() 23 | 24 | throw RuntimeException("Intentional") 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /x-postgres-features/src/main/java/org/example/domain/User.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.Id; 6 | import javax.persistence.Table; 7 | 8 | @Entity 9 | @Table(name="p_user") 10 | public class User { 11 | 12 | @Id 13 | @Column(name="user_id") 14 | Long id; 15 | 16 | String name; 17 | 18 | public Long getId() { 19 | return id; 20 | } 21 | 22 | public void setId(Long id) { 23 | this.id = id; 24 | } 25 | 26 | public String getName() { 27 | return name; 28 | } 29 | 30 | public void setName(String name) { 31 | this.name = name; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /e-groovy/src/main/groovy/org/example/service/LoadAgentAtRuntime.groovy: -------------------------------------------------------------------------------- 1 | package org.example.service 2 | 3 | import org.avaje.agentloader.AgentLoader 4 | import org.slf4j.Logger 5 | import org.slf4j.LoggerFactory 6 | 7 | /** 8 | * 9 | * @author: Richard Vowles - https://plus.google.com/+RichardVowles 10 | */ 11 | class LoadAgentAtRuntime { 12 | protected static Logger logger = LoggerFactory.getLogger(LoadAgentAtRuntime.class); 13 | 14 | static { 15 | logger.debug("... preStart"); 16 | if (!AgentLoader.loadAgentFromClasspath("avaje-ebeanorm-agent","debug=1;packages=org.example.**")) { 17 | logger.info("avaje-ebeanorm-agent not found in classpath - not dynamically loaded"); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/test/java/org/example/service/OrderServiceTest.kt: -------------------------------------------------------------------------------- 1 | package org.example.service 2 | 3 | import com.avaje.ebean.annotation.Transactional 4 | import com.google.inject.Guice 5 | import org.example.domain.Order 6 | import org.example.module.DbModule 7 | import org.junit.Test 8 | import sun.security.pkcs11.Secmod 9 | 10 | /** 11 | * 12 | */ 13 | public class OrderServiceTest { 14 | 15 | @Transactional 16 | @Test 17 | fun save() { 18 | 19 | val injector = Guice.createInjector(DbModule()) 20 | 21 | val orderService = injector.getInstance(OrderService::class.java) 22 | 23 | val order = Order() 24 | order.status = Order.Status.APPROVED 25 | orderService.save(order); 26 | } 27 | } -------------------------------------------------------------------------------- /e-kotlin-maven/src/main/java/org/example/domain/Product.kt: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.Model 4 | import javax.persistence.Entity; 5 | import javax.persistence.Table; 6 | import javax.validation.constraints.NotNull 7 | import javax.validation.constraints.Size; 8 | 9 | 10 | /** 11 | * Product entity bean. 12 | */ 13 | @Entity 14 | @Table(name = "o_product") 15 | public class Product ( 16 | 17 | @NotNull @Size(max = 20) 18 | public var sku: String = "", 19 | 20 | @NotNull @Size(max = 100) 21 | public var name: String = "" 22 | 23 | ) : BaseModel() { 24 | 25 | /** 26 | * Finder convenience singleton. 27 | */ 28 | companion object : Model.Find() {} 29 | 30 | } 31 | -------------------------------------------------------------------------------- /x-postgres-features/src/main/resources/ebean.properties: -------------------------------------------------------------------------------- 1 | 2 | #load.properties.override=${home}/config/myapp.ebean.properties 3 | 4 | #ebean.ddl.generate=true 5 | #ebean.ddl.run=true 6 | 7 | datasource.default=pg 8 | 9 | datasource.pg.username=feat 10 | datasource.pg.password=feat 11 | datasource.pg.databaseUrl=jdbc:postgresql://127.0.0.1:5432/feat 12 | datasource.pg.databaseDriver=org.postgresql.Driver 13 | #datasource.pg.minConnections=1 14 | #datasource.pg.maxConnections=25 15 | #datasource.pg.capturestacktrace=true 16 | 17 | 18 | datasource.ora.username=feat 19 | datasource.ora.password=feat 20 | datasource.ora.databaseUrl=jdbc:oracle:thin:@//127.0.0.1:1521/orcl 21 | datasource.ora.databaseDriver=oracle.jdbc.driver.OracleDriver -------------------------------------------------------------------------------- /a-basic/src/main/java/org/example/domain/finder/ProductFinder.java: -------------------------------------------------------------------------------- 1 | package org.example.domain.finder; 2 | 3 | import com.avaje.ebean.Finder; 4 | import org.example.domain.Product; 5 | import org.example.domain.query.QProduct; 6 | 7 | public class ProductFinder extends Finder { 8 | 9 | /** 10 | * Construct using the default EbeanServer. 11 | */ 12 | public ProductFinder() { 13 | super(Product.class); 14 | } 15 | 16 | /** 17 | * Construct with a given EbeanServer. 18 | */ 19 | public ProductFinder(String serverName) { 20 | super(Product.class, serverName); 21 | } 22 | 23 | /** 24 | * Start a new typed query. 25 | */ 26 | protected QProduct where() { 27 | return new QProduct(db()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /e-eventadapters/src/main/resources/ebean.properties: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ebean.ddl.generate=true 5 | ebean.ddl.run=true 6 | 7 | 8 | 9 | datasource.default=h2 10 | 11 | datasource.h2.username=sa 12 | datasource.h2.password= 13 | datasource.h2.databaseUrl=jdbc:h2:mem:tests 14 | datasource.h2.databaseDriver=org.h2.Driver 15 | 16 | #datasource.default=mysql 17 | 18 | #datasource.mysql.username=wjx 19 | #datasource.mysql.password=wjx971265 20 | #datasource.mysql.databaseUrl=jdbc:mysql://127.0.0.1:3306/test 21 | #datasource.mysql.databaseDriver=com.mysql.jdbc.Driver 22 | #datasource.mysql.minConnections=1 23 | #datasource.mysql.maxConnections=25 24 | #datasource.mysql.heartbeatsql=select count(*) from dual 25 | #datasource.mysql.isolationlevel=read_committed 26 | 27 | -------------------------------------------------------------------------------- /e-groovy/src/main/groovy/org/example/service/LoadCustomerService.groovy: -------------------------------------------------------------------------------- 1 | package org.example.service 2 | 3 | import groovy.transform.CompileStatic 4 | import org.example.domain.Customer 5 | 6 | /** 7 | * 8 | */ 9 | @CompileStatic 10 | class LoadCustomerService { 11 | 12 | 13 | void loadSome() { 14 | 15 | Customer rob = new Customer(name:"Rob") 16 | rob.save(); 17 | 18 | Customer richard = new Customer(name:"Richard", registered:new Date()) 19 | richard.save() 20 | 21 | richard.name = "was richard" 22 | richard.save(); 23 | 24 | def customers = Customer.find.order().asc("id").findList(); 25 | 26 | for (Customer customer: customers) { 27 | System.out.println("Hello $customer.name $customer.id") 28 | } 29 | 30 | 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/test/java/org/example/service/WithTransactionalTest.kt: -------------------------------------------------------------------------------- 1 | package org.example.service 2 | 3 | import org.example.ExampleBaseTestCase 4 | import org.junit.Test 5 | import org.example.domain.Customer 6 | import org.junit.Assert 7 | import kotlin.test.assertFalse 8 | import kotlin.test.assertNull 9 | 10 | /** 11 | * Created by rob on 30/10/14. 12 | */ 13 | class WithTransactionalTest : ExampleBaseTestCase() { 14 | 15 | @Test fun just_run_it() { 16 | 17 | val load = WithTransactional() 18 | try { 19 | load.performInTransaction() 20 | assertFalse(true) 21 | 22 | } catch (e: RuntimeException) { 23 | val customer : Customer? = Customer.where().eq("name", "ShouldNotFindMe").findUnique() 24 | assertNull(customer) 25 | } 26 | 27 | } 28 | } -------------------------------------------------------------------------------- /e-kotlin-maven/src/main/java/org/example/domain/Address.kt: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.Model 4 | import javax.persistence.Entity; 5 | import javax.persistence.ManyToOne; 6 | import javax.persistence.Table; 7 | import javax.validation.constraints.Size; 8 | 9 | /** 10 | * Address entity bean. 11 | */ 12 | @Entity 13 | @Table(name = "o_address") 14 | public class Address : BaseModel() { 15 | 16 | @Size(max = 100) 17 | public var line1: String? = null; 18 | 19 | @Size(max = 100) 20 | public var line2: String? = null; 21 | 22 | @Size(max = 100) 23 | public var city: String? = null; 24 | 25 | @ManyToOne(optional = false) 26 | public var country: Country? = null; 27 | 28 | companion object : Model.Find() {} 29 | 30 | } 31 | 32 | 33 | -------------------------------------------------------------------------------- /a-basic/src/main/java/org/example/domain/finder/ContactNoteFinder.java: -------------------------------------------------------------------------------- 1 | package org.example.domain.finder; 2 | 3 | import com.avaje.ebean.Finder; 4 | import org.example.domain.ContactNote; 5 | import org.example.domain.query.QContactNote; 6 | 7 | public class ContactNoteFinder extends Finder { 8 | 9 | /** 10 | * Construct using the default EbeanServer. 11 | */ 12 | public ContactNoteFinder() { 13 | super(ContactNote.class); 14 | } 15 | 16 | /** 17 | * Construct with a given EbeanServer. 18 | */ 19 | public ContactNoteFinder(String serverName) { 20 | super(ContactNote.class, serverName); 21 | } 22 | 23 | /** 24 | * Start a new typed query. 25 | */ 26 | protected QContactNote where() { 27 | return new QContactNote(db()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /a-basic/src/main/java/org/example/domain/finder/OrderDetailFinder.java: -------------------------------------------------------------------------------- 1 | package org.example.domain.finder; 2 | 3 | import com.avaje.ebean.Finder; 4 | import org.example.domain.OrderDetail; 5 | import org.example.domain.query.QOrderDetail; 6 | 7 | public class OrderDetailFinder extends Finder { 8 | 9 | /** 10 | * Construct using the default EbeanServer. 11 | */ 12 | public OrderDetailFinder() { 13 | super(OrderDetail.class); 14 | } 15 | 16 | /** 17 | * Construct with a given EbeanServer. 18 | */ 19 | public OrderDetailFinder(String serverName) { 20 | super(OrderDetail.class, serverName); 21 | } 22 | 23 | /** 24 | * Start a new typed query. 25 | */ 26 | protected QOrderDetail where() { 27 | return new QOrderDetail(db()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/main/resources/ebean.properties: -------------------------------------------------------------------------------- 1 | 2 | #load.properties.override=${home}/config/myapp.ebean.properties 3 | 4 | ebean.ddl.generate=true 5 | ebean.ddl.run=true 6 | 7 | ebean.databaseSequenceBatchSize=1 8 | 9 | datasource.default=h2 10 | 11 | datasource.h2.username=sa 12 | datasource.h2.password= 13 | datasource.h2.databaseUrl=jdbc:h2:mem:tests 14 | datasource.h2.databaseDriver=org.h2.Driver 15 | #datasource.h2.minConnections=1 16 | #datasource.h2.maxConnections=25 17 | #datasource.h2.capturestacktrace=true 18 | #datasource.h2.isolationlevel=read_committed 19 | #datasource.h2.maxStackTraceSize=20 20 | 21 | datasource.pg.username=test 22 | datasource.pg.password=test 23 | datasource.pg.databaseUrl=jdbc:postgresql://127.0.0.1:5432/mydb 24 | datasource.pg.databaseDriver=org.postgresql.Driver 25 | 26 | 27 | -------------------------------------------------------------------------------- /a-basic/h2-drop.sql: -------------------------------------------------------------------------------- 1 | SET REFERENTIAL_INTEGRITY FALSE; 2 | 3 | drop table if exists o_address; 4 | 5 | drop table if exists be_contact; 6 | 7 | drop table if exists contact_note; 8 | 9 | drop table if exists o_country; 10 | 11 | drop table if exists be_customer; 12 | 13 | drop table if exists o_order; 14 | 15 | drop table if exists o_order_detail; 16 | 17 | drop table if exists o_product; 18 | 19 | SET REFERENTIAL_INTEGRITY TRUE; 20 | 21 | drop sequence if exists o_address_seq; 22 | 23 | drop sequence if exists be_contact_seq; 24 | 25 | drop sequence if exists contact_note_seq; 26 | 27 | drop sequence if exists o_country_seq; 28 | 29 | drop sequence if exists be_customer_seq; 30 | 31 | drop sequence if exists o_order_seq; 32 | 33 | drop sequence if exists o_order_detail_seq; 34 | 35 | drop sequence if exists o_product_seq; 36 | 37 | -------------------------------------------------------------------------------- /e-groovy/src/main/resources/ebean.properties: -------------------------------------------------------------------------------- 1 | 2 | #load.properties.override=${home}/config/myapp.ebean.properties 3 | 4 | ebean.ddl.generate=true 5 | ebean.ddl.run=true 6 | 7 | ebean.uuidStoreAsBinary=true 8 | ebean.databaseSequenceBatchSize=1 9 | 10 | datasource.default=h2 11 | 12 | datasource.h2.username=h2 13 | datasource.h2.password= 14 | datasource.h2.databaseUrl=jdbc:h2:mem:tests 15 | datasource.h2.databaseDriver=org.h2.Driver 16 | #datasource.h2.minConnections=1 17 | #datasource.h2.maxConnections=25 18 | #datasource.h2.capturestacktrace=true 19 | #datasource.h2.isolationlevel=read_committed 20 | #datasource.h2.maxStackTraceSize=20 21 | 22 | datasource.pg.username=test 23 | datasource.pg.password=test 24 | datasource.pg.databaseUrl=jdbc:postgresql://127.0.0.1:5432/mydb 25 | datasource.pg.databaseDriver=org.postgresql.Driver 26 | 27 | 28 | -------------------------------------------------------------------------------- /e-groovy/src/main/groovy/org/avaje/ebean/groovy/StaticEbeanExtensionModule.groovy: -------------------------------------------------------------------------------- 1 | package org.avaje.ebean.groovy 2 | 3 | import com.avaje.ebean.Ebean 4 | import com.avaje.ebean.TxRunnable 5 | import groovy.transform.CompileStatic 6 | 7 | /** 8 | * This is just to demonstrate extensions on static classes. You cannot use this in the classes in the same 9 | * project, separate it into a separate artifact if you are wanting to use these features (github.com/uoa-group-applications/common-ebean) 10 | * 11 | * @author: Richard Vowles - https://plus.google.com/+RichardVowles 12 | */ 13 | @CompileStatic 14 | class StaticEbeanExtensionModule { 15 | 16 | public static run(Ebean server, Closure txRunnable) { 17 | server.execute(new TxRunnable() { 18 | @Override 19 | void run() { 20 | txRunnable.call() 21 | } 22 | }) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /x-postgres-history/src/main/java/org/example/domain/finder/ContactFinder.java: -------------------------------------------------------------------------------- 1 | package org.example.domain.finder; 2 | 3 | import com.avaje.ebean.Model; 4 | import org.example.domain.Contact; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * Add finder methods here. 10 | */ 11 | public class ContactFinder extends Model.Finder { 12 | 13 | public ContactFinder() { 14 | super(Contact.class); 15 | } 16 | 17 | /** 18 | * Find the contact by exact email address. 19 | */ 20 | public Contact byEmail(String email) { 21 | return where().eq("email", email).findUnique(); 22 | } 23 | 24 | /** 25 | * Find by email address ending in (case insensitive). 26 | */ 27 | public List byEmailEndsWith(String emailEndsIn) { 28 | return where().iendsWith("email", emailEndsIn).findList(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /e-groovy/src/main/groovy/org/example/domain/Country.groovy: -------------------------------------------------------------------------------- 1 | package org.example.domain 2 | 3 | import com.avaje.ebean.annotation.CacheStrategy 4 | import com.avaje.ebean.annotation.CacheTuning 5 | import groovy.transform.CompileStatic 6 | 7 | import javax.persistence.Entity 8 | import javax.persistence.Id 9 | import javax.persistence.Table 10 | import javax.validation.constraints.Size 11 | 12 | /** 13 | * 14 | * @author: Richard Vowles - https://plus.google.com/+RichardVowles 15 | * Country entity bean. 16 | */ 17 | @CacheStrategy(readOnly=true,warmingQuery="order by name") 18 | @CacheTuning(maxSize=500) 19 | @Entity 20 | @Table(name="o_country") 21 | @CompileStatic 22 | class Country { 23 | @Id 24 | @Size(max=2) 25 | String code 26 | 27 | @Size(max=60) 28 | String name 29 | 30 | public String toString() { 31 | return code 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/test/java/org/example/domain/SimpleTest.kt: -------------------------------------------------------------------------------- 1 | package org.example.domain 2 | 3 | import org.junit.Test 4 | import java.util.ArrayList 5 | 6 | /** 7 | * Created by rob on 23/10/14. 8 | */ 9 | class SimpleTest { 10 | 11 | @Test 12 | fun simple() { 13 | 14 | var details: MutableList = ArrayList(); 15 | details.add(OrderDetail()) 16 | 17 | // Mutable ... subList, iterator etc 18 | val mutIterator = details.iterator() 19 | while (mutIterator.hasNext()) { 20 | mutIterator.next() 21 | mutIterator.remove() 22 | } 23 | 24 | 25 | // Immutable ... subList, iterator etc 26 | val someStrings = listOf("one", "two"); 27 | //val subList = someStrings.subList(0, 1); 28 | 29 | val it = someStrings.iterator(); 30 | while (it.hasNext()) { 31 | it.next(); 32 | //it.remove(); 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /x-postgres-features/src/test/java/org/example/RawJdbcIsValidTest.java: -------------------------------------------------------------------------------- 1 | package org.example; 2 | 3 | import com.avaje.ebean.Ebean; 4 | import com.avaje.ebean.Transaction; 5 | import org.junit.Test; 6 | 7 | import java.sql.Connection; 8 | import java.sql.PreparedStatement; 9 | import java.sql.ResultSet; 10 | import java.sql.SQLException; 11 | import java.util.HashMap; 12 | import java.util.Map; 13 | 14 | import static org.junit.Assert.assertTrue; 15 | 16 | public class RawJdbcIsValidTest extends ExampleBaseTestCase { 17 | 18 | @Test 19 | public void test() throws SQLException { 20 | 21 | Transaction txn = Ebean.beginTransaction(); 22 | try { 23 | Connection connection = txn.getConnection(); 24 | 25 | boolean valid = connection.isValid(1000); 26 | assertTrue(valid); 27 | 28 | } finally { 29 | txn.end(); 30 | } 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /x-postgres-features/src/test/resources/createStoredProcedure.sql: -------------------------------------------------------------------------------- 1 | create or replace function sp_insert (p_name VARCHAR, OUT p_id INTEGER) 2 | as $$ 3 | 4 | DECLARE 5 | 6 | BEGIN 7 | insert into p_customer (name, inactive, version, when_created, when_updated) values (p_name, true, 1, current_timestamp, current_timestamp); 8 | SELECT currval(pg_get_serial_sequence('p_customer','id')) into p_id; 9 | END; 10 | $$ 11 | LANGUAGE 'plpgsql' 12 | 13 | -- select sp_insert('asd'); 14 | 15 | select * from p_customer ORDER BY id desc; 16 | 17 | 18 | 19 | CREATE OR REPLACE FUNCTION sp_insert2 (INOUT p_name VARCHAR, OUT p_id INTEGER) 20 | AS $$ 21 | BEGIN 22 | insert into p_customer (name, inactive, version, when_created, when_updated) values (p_name, true, 1, current_timestamp, current_timestamp); 23 | SELECT currval(pg_get_serial_sequence('p_customer','id')) into p_id; 24 | END; 25 | $$ 26 | LANGUAGE 'plpgsql' -------------------------------------------------------------------------------- /e-kotlin-maven/src/test/java/org/example/ExampleBaseTestCase.java: -------------------------------------------------------------------------------- 1 | package org.example; 2 | 3 | import org.avaje.agentloader.AgentLoader; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | 7 | 8 | /** 9 | * Used to dynamically load the avaje-ebeanorm-agent. 10 | * 11 | * Alternatives: 12 | * - Use IntelliJ or Eclipse plugins, 13 | * - Rely on maven/ant enhancement (pain in dev, test cycle) 14 | * - Specify the java agent on the command line 15 | * 16 | */ 17 | public class ExampleBaseTestCase { 18 | 19 | protected static Logger logger = LoggerFactory.getLogger(ExampleBaseTestCase.class); 20 | 21 | static { 22 | logger.debug("... preStart"); 23 | if (!AgentLoader.loadAgentFromClasspath("avaje-ebeanorm-agent","debug=3;packages=org.example.**")) { 24 | logger.info("avaje-ebeanorm-agent not found in classpath - not dynamically loaded"); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /x-postgres-features/src/test/java/org/example/HstoreJsonTest.java: -------------------------------------------------------------------------------- 1 | package org.example; 2 | 3 | import java.io.IOException; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | import org.example.domain.Customer; 8 | import org.junit.Test; 9 | 10 | import com.avaje.ebean.text.json.JsonContext; 11 | 12 | public class HstoreJsonTest extends ExampleBaseTestCase { 13 | 14 | @Test 15 | public void test() throws IOException { 16 | 17 | 18 | Customer customer = new Customer(); 19 | customer.setName("SomeCustomer"); 20 | 21 | Map tags = new HashMap<>(); 22 | tags.put("height","100"); 23 | tags.put("length","200"); 24 | 25 | customer.setTags(tags); 26 | 27 | JsonContext jsonContext = Customer.db().json(); 28 | String jsonString = jsonContext.toJson(customer); 29 | System.out.println(jsonString); 30 | 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /x-postgres-features/src/test/java/org/example/AutoCommitTest.java: -------------------------------------------------------------------------------- 1 | package org.example; 2 | 3 | import org.example.domain.Customer; 4 | import org.junit.Test; 5 | 6 | import com.avaje.ebean.Ebean; 7 | import com.avaje.ebean.Transaction; 8 | 9 | public class AutoCommitTest extends ExampleBaseTestCase { 10 | 11 | @Test 12 | public void test() { 13 | 14 | Ebean.getServer(null); 15 | 16 | Customer customer = new Customer(); 17 | customer.setName("Rob"); 18 | customer.save(); 19 | 20 | Transaction txn = Customer.db().beginTransaction(); 21 | txn.setBatchMode(true); 22 | try { 23 | 24 | for (int i = 0; i < 2; i++) { 25 | Customer cust = new Customer(); 26 | cust.setName("Batch insert "+i); 27 | cust.save(); 28 | } 29 | 30 | txn.commit(); 31 | 32 | } finally { 33 | txn.end(); 34 | } 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /x-postgres-features/src/test/java/org/example/ExampleBaseTestCase.java: -------------------------------------------------------------------------------- 1 | package org.example; 2 | 3 | import org.avaje.agentloader.AgentLoader; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | 7 | 8 | /** 9 | * Used to dynamically load the avaje-ebeanorm-agent. 10 | * 11 | * Alternatives: 12 | * - Use IntelliJ or Eclipse plugins, 13 | * - Rely on maven/ant enhancement (pain in dev, test cycle) 14 | * - Specify the java agent on the command line 15 | * 16 | */ 17 | public class ExampleBaseTestCase { 18 | 19 | protected static Logger logger = LoggerFactory.getLogger(ExampleBaseTestCase.class); 20 | 21 | static { 22 | logger.debug("... preStart"); 23 | if (!AgentLoader.loadAgentFromClasspath("avaje-ebeanorm-agent","debug=1;packages=org.example.**")) { 24 | logger.info("avaje-ebeanorm-agent not found in classpath - not dynamically loaded"); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /e-eventadapters/src/test/java/org/example/domain/ExampleBaseTestCase.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import org.avaje.agentloader.AgentLoader; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | 7 | 8 | /** 9 | * Used to dynamically load the avaje-ebeanorm-agent. 10 | * 11 | * Alternatives: 12 | * - Use IntelliJ or Eclipse plugins, 13 | * - Rely on maven/ant enhancement (pain in dev, test cycle) 14 | * - Specify the java agent on the command line 15 | * 16 | */ 17 | public class ExampleBaseTestCase { 18 | 19 | protected static Logger logger = LoggerFactory.getLogger(ExampleBaseTestCase.class); 20 | 21 | static { 22 | logger.debug("... preStart"); 23 | if (!AgentLoader.loadAgentFromClasspath("avaje-ebeanorm-agent","debug=1;packages=org.example.**")) { 24 | logger.info("avaje-ebeanorm-agent not found in classpath - not dynamically loaded"); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /e-groovy/src/test/groovy/org/example/service/ExampleBaseTestCase.groovy: -------------------------------------------------------------------------------- 1 | package org.example.service; 2 | 3 | import org.avaje.agentloader.AgentLoader; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | 7 | 8 | /** 9 | * Used to dynamically load the avaje-ebeanorm-agent. 10 | * 11 | * Alternatives: 12 | * - Use IntelliJ or Eclipse plugins, 13 | * - Rely on maven/ant enhancement (pain in dev, test cycle) 14 | * - Specify the java agent on the command line 15 | * 16 | */ 17 | public class ExampleBaseTestCase { 18 | 19 | protected static Logger logger = LoggerFactory.getLogger(ExampleBaseTestCase.class); 20 | 21 | static { 22 | logger.debug("... preStart"); 23 | if (!AgentLoader.loadAgentFromClasspath("avaje-ebeanorm-agent","debug=1;packages=org.example.**")) { 24 | logger.info("avaje-ebeanorm-agent not found in classpath - not dynamically loaded"); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /e-groovy/src/test/groovy/org/example/service/ExampleGroovyNullSafeTraversal.groovy: -------------------------------------------------------------------------------- 1 | package org.example.service 2 | 3 | import groovy.transform.CompileStatic 4 | import org.example.domain.Customer 5 | import org.junit.Test 6 | 7 | @CompileStatic 8 | class ExampleGroovyNullSafeTraversal extends ExampleBaseTestCase { 9 | 10 | @Test 11 | void test_query_then_navigate() { 12 | 13 | LoadExampleData.load(); 14 | 15 | def customers = Customer.find 16 | .select("name") 17 | .fetch("billingAddress","city") 18 | .fetch("billingAddress.country") 19 | .findList(); 20 | 21 | for (Customer customer : customers) { 22 | // null safe traversal of properties 23 | def city = customer.billingAddress?.city; 24 | def country = customer.billingAddress?.country?.name; 25 | System.println("customer: $customer.name city: $city country: $country"); 26 | } 27 | 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /x-json-features/src/test/java/org/example/PlainJsonParseTest.java: -------------------------------------------------------------------------------- 1 | package org.example; 2 | 3 | import java.util.Map; 4 | 5 | import org.boon.json.JsonParser; 6 | import org.boon.json.JsonParserAndMapper; 7 | import org.boon.json.JsonParserFactory; 8 | import org.junit.Test; 9 | 10 | public class PlainJsonParseTest { 11 | 12 | @Test 13 | public void test() { 14 | 15 | final JsonParser parser = new JsonParserFactory().create (); 16 | 17 | JsonParserAndMapper json = new JsonParserFactory().create(); 18 | 19 | String jsonString = "{\"length\":\"100\", \"height\":200}"; 20 | 21 | Map object = json.parse(Map.class, jsonString); 22 | 23 | //Object object = parser.parse(json); 24 | 25 | Map map = EJson.parseObject(jsonString); 26 | System.out.println(map); 27 | 28 | String mapAsJson = EJson.write(map); 29 | System.out.println(mapAsJson); 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/main/java/org/example/service/OrderService.kt: -------------------------------------------------------------------------------- 1 | package org.example.service 2 | 3 | import com.avaje.ebean.EbeanServer 4 | import com.avaje.ebean.annotation.Transactional 5 | import org.example.domain.Order 6 | import org.example.extension.loggerFor 7 | import javax.inject.Inject 8 | import javax.inject.Singleton 9 | 10 | /** 11 | * 12 | */ 13 | @Singleton 14 | public class OrderService { 15 | 16 | private val log = loggerFor(javaClass) 17 | 18 | private val server : EbeanServer 19 | 20 | @Inject 21 | constructor(server : EbeanServer){ 22 | this.server = server 23 | } 24 | 25 | @Transactional 26 | fun save(order: Order) { 27 | 28 | val ebeanServer = Order.db() 29 | 30 | if (ebeanServer != server) { 31 | log.error("not the same server instance!!") 32 | } 33 | 34 | log.debug("saving order") 35 | order.save() 36 | 37 | log.debug("server ${ebeanServer.getName()}") 38 | } 39 | } -------------------------------------------------------------------------------- /x-dbmigration/src/test/java/main/MainDbMigration.java: -------------------------------------------------------------------------------- 1 | package main; 2 | 3 | import com.avaje.ebean.config.dbplatform.DbPlatformName; 4 | import com.avaje.ebean.dbmigration.DbMigration; 5 | 6 | import java.io.IOException; 7 | 8 | public class MainDbMigration { 9 | 10 | public static void main(String[] args) throws IOException { 11 | 12 | DbMigration migration = new DbMigration(); 13 | migration.setPathToResources("src/main/resources"); 14 | migration.setPlatform(DbPlatformName.POSTGRES); 15 | 16 | migration.generateMigration(); 17 | 18 | } 19 | 20 | private static void runMultiplePlatforms() throws IOException { 21 | 22 | DbMigration migration = new DbMigration(); 23 | migration.setPathToResources("src/main/resources"); 24 | migration.addPlatform(DbPlatformName.POSTGRES, "pg"); 25 | migration.addPlatform(DbPlatformName.MYSQL, "mysql"); 26 | 27 | migration.generateMigration(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /a-basic/src/main/java/org/example/domain/finder/CustomerFinder.java: -------------------------------------------------------------------------------- 1 | package org.example.domain.finder; 2 | 3 | import com.avaje.ebean.Finder; 4 | import org.example.domain.Customer; 5 | import org.example.domain.query.QCustomer; 6 | 7 | public class CustomerFinder extends Finder { 8 | 9 | /** 10 | * Construct using the default EbeanServer. 11 | */ 12 | public CustomerFinder() { 13 | super(Customer.class); 14 | } 15 | 16 | /** 17 | * Construct with a given EbeanServer. 18 | */ 19 | public CustomerFinder(String serverName) { 20 | super(Customer.class, serverName); 21 | } 22 | 23 | /** 24 | * Start a new typed query. 25 | */ 26 | public QCustomer where() { 27 | return new QCustomer(db()); 28 | } 29 | 30 | /** 31 | * Find by name equal (case insensitive). 32 | */ 33 | public Customer byName(String name) { 34 | return where().name.iequalTo(name).findUnique(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /x-postgres-features/src/test/java/org/example/BatchInsertTest.java: -------------------------------------------------------------------------------- 1 | package org.example; 2 | 3 | import org.example.domain.Customer; 4 | import org.junit.Test; 5 | 6 | import com.avaje.ebean.Transaction; 7 | 8 | public class BatchInsertTest extends ExampleBaseTestCase { 9 | 10 | @Test 11 | public void testInsert() { 12 | 13 | Transaction txn = Customer.db().beginTransaction(); 14 | try { 15 | txn.setBatchMode(true); 16 | txn.setBatchSize(10); 17 | 18 | // turn of getGeneratedKeys if we don't need to use the 19 | // customer bean instances after we have inserted them 20 | txn.setBatchGetGeneratedKeys(false); 21 | 22 | for (int i = 0; i < 40; i++) { 23 | Customer c = new Customer(); 24 | c.setName("batch insert test "+i); 25 | c.save(); 26 | } 27 | 28 | txn.commit(); 29 | 30 | } finally { 31 | txn.end(); 32 | } 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /a-basic/src/main/java/org/example/domain/finder/AddressFinder.java: -------------------------------------------------------------------------------- 1 | package org.example.domain.finder; 2 | 3 | import com.avaje.ebean.Finder; 4 | import org.example.domain.Address; 5 | import org.example.domain.query.QAddress; 6 | 7 | import java.util.List; 8 | 9 | public class AddressFinder extends Finder { 10 | 11 | /** 12 | * Construct using the default EbeanServer. 13 | */ 14 | public AddressFinder() { 15 | super(Address.class); 16 | } 17 | 18 | /** 19 | * Construct with a given EbeanServer. 20 | */ 21 | public AddressFinder(String serverName) { 22 | super(Address.class, serverName); 23 | } 24 | 25 | /** 26 | * Start a new typed query. 27 | */ 28 | protected QAddress where() { 29 | return new QAddress(db()); 30 | } 31 | 32 | /** 33 | * Find all addresses with city like. 34 | */ 35 | public List
byCityLike(String city) { 36 | return where().city.like(city).findList(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/main/java/org/example/module/EbeanServerProvider.kt: -------------------------------------------------------------------------------- 1 | package org.example.module 2 | 3 | import com.avaje.ebean.EbeanServer 4 | import com.avaje.ebean.EbeanServerFactory 5 | import com.avaje.ebean.config.ServerConfig 6 | import com.google.inject.Provider 7 | import java.util.* 8 | import java.util.concurrent.ConcurrentHashMap 9 | 10 | /** 11 | * Creates the default EbeanServer instance. 12 | * 13 | * This should be a singleton. 14 | */ 15 | public class EbeanServerProvider : Provider { 16 | 17 | override fun get(): EbeanServer? { 18 | 19 | val config = ServerConfig() 20 | config.setName("h2") 21 | 22 | // need to set defaultServer to true so that 23 | // this is the same instance used via Model 24 | config.setDefaultServer(true) 25 | 26 | // can pass in Properties or just load them 27 | // via ebean.properties 28 | config.loadFromProperties() 29 | 30 | return EbeanServerFactory.create(config); 31 | } 32 | } -------------------------------------------------------------------------------- /a-basic/src/main/java/org/example/domain/finder/CountryFinder.java: -------------------------------------------------------------------------------- 1 | package org.example.domain.finder; 2 | 3 | import com.avaje.ebean.Finder; 4 | import org.example.domain.Country; 5 | import org.example.domain.query.QCountry; 6 | 7 | import java.util.List; 8 | 9 | public class CountryFinder extends Finder { 10 | 11 | /** 12 | * Construct using the default EbeanServer. 13 | */ 14 | public CountryFinder() { 15 | super(Country.class); 16 | } 17 | 18 | /** 19 | * Construct with a given EbeanServer. 20 | */ 21 | public CountryFinder(String serverName) { 22 | super(Country.class, serverName); 23 | } 24 | 25 | /** 26 | * Start a new typed query. 27 | */ 28 | protected QCountry where() { 29 | return new QCountry(db()); 30 | } 31 | 32 | /** 33 | * Return countries with name like (case insensitive). 34 | */ 35 | public List byNameLike(String name) { 36 | return where().name.ilike(name).findList(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /e-groovy/src/main/groovy/org/example/domain/Contact.groovy: -------------------------------------------------------------------------------- 1 | package org.example.domain 2 | 3 | import com.avaje.ebean.Model 4 | import groovy.transform.CompileStatic 5 | 6 | import javax.persistence.Column 7 | import javax.persistence.Entity 8 | import javax.persistence.ManyToOne 9 | import javax.persistence.Table 10 | 11 | /** 12 | * 13 | * @author: Richard Vowles - https://plus.google.com/+RichardVowles 14 | * Contact entity bean. 15 | */ 16 | @Entity 17 | @Table(name="be_contact") 18 | @CompileStatic 19 | class Contact extends BaseModel { 20 | /** 21 | * Convenience Finder for 'active record' style. 22 | */ 23 | public static final Model.Finder find = new Model.Finder(Long, Contact) 24 | 25 | @Column(length=50) 26 | String firstName 27 | 28 | @Column(length=50) 29 | String lastName 30 | 31 | @Column(length=200) 32 | String email 33 | 34 | @Column(length=20) 35 | String phone 36 | 37 | @ManyToOne(optional=false) 38 | Customer customer 39 | } 40 | -------------------------------------------------------------------------------- /a-basic/src/test/java/main/MainQueryBeanGenerator.java: -------------------------------------------------------------------------------- 1 | package main; 2 | 3 | import org.avaje.ebean.typequery.generator.Generator; 4 | import org.avaje.ebean.typequery.generator.GeneratorConfig; 5 | 6 | import java.io.IOException; 7 | 8 | /** 9 | * Generate type query beans for each entity bean. 10 | */ 11 | public class MainQueryBeanGenerator { 12 | 13 | public static void main(String[] args) throws IOException { 14 | 15 | GeneratorConfig config = new GeneratorConfig(); 16 | //config.setClassesDirectory("./target/classes"); 17 | //config.setDestDirectory("./src/main/java"); 18 | //config.setDestResourceDirectory("./src/main/resources"); 19 | 20 | config.setEntityBeanPackage("org.example.domain"); 21 | //config.setDestPackage("org.example.domain.query"); 22 | 23 | 24 | Generator generator = new Generator(config); 25 | generator.generateQueryBeans(); 26 | //generator.generateFinders(); 27 | //generator.modifyEntityBeansAddFinderField(); 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/main/java/org/example/domain/Country.kt: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.Model 4 | import com.avaje.ebean.annotation.CacheStrategy 5 | import com.avaje.ebean.annotation.CacheTuning 6 | import javax.persistence.Entity; 7 | import javax.persistence.Id; 8 | import javax.persistence.Table; 9 | import javax.validation.constraints.Size; 10 | 11 | /** 12 | * Country entity bean. 13 | *

14 | * Uses constructor with properties having default values. 15 | */ 16 | @CacheStrategy(readOnly = true) 17 | @CacheTuning(maxSize = 500) 18 | @Entity 19 | @Table(name = "o_country") 20 | public class Country( 21 | 22 | @Id @Size(max = 2) 23 | public var code: String, 24 | 25 | @Size(max = 60) 26 | public var name: String 27 | 28 | ) : Model() { 29 | 30 | override fun toString(): String { 31 | return "code:$code name:$name"; 32 | } 33 | 34 | /** 35 | * Find helper singleton. 36 | */ 37 | companion object : Model.Find() {} 38 | 39 | } 40 | -------------------------------------------------------------------------------- /a-basic/src/main/java/org/example/domain/ContactNote.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import org.example.domain.finder.ContactNoteFinder; 4 | 5 | import javax.persistence.Entity; 6 | import javax.persistence.Lob; 7 | import javax.persistence.ManyToOne; 8 | 9 | @Entity 10 | public class ContactNote extends BaseModel { 11 | 12 | public static final ContactNoteFinder find = new ContactNoteFinder(); 13 | 14 | @ManyToOne(optional = false) 15 | Contact contact; 16 | 17 | String title; 18 | 19 | @Lob 20 | String note; 21 | 22 | public String getTitle() { 23 | return title; 24 | } 25 | 26 | public void setTitle(String title) { 27 | this.title = title; 28 | } 29 | 30 | public String getNote() { 31 | return note; 32 | } 33 | 34 | public void setNote(String note) { 35 | this.note = note; 36 | } 37 | 38 | public Contact getContact() { 39 | return contact; 40 | } 41 | 42 | public void setContact(Contact contact) { 43 | this.contact = contact; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /x-postgres-features/src/test/java/org/example/ListTableNamesTest.java: -------------------------------------------------------------------------------- 1 | package org.example; 2 | 3 | import java.util.List; 4 | 5 | import org.junit.Test; 6 | 7 | import com.avaje.ebean.Ebean; 8 | import com.avaje.ebean.SqlQuery; 9 | import com.avaje.ebean.SqlRow; 10 | 11 | /** 12 | * Test the use of HStore being mapped to a Map value. 13 | */ 14 | public class ListTableNamesTest extends ExampleBaseTestCase { 15 | 16 | @Test 17 | public void test() { 18 | 19 | String sql = 20 | " SELECT table_name " 21 | + " FROM information_schema.tables " 22 | + " WHERE table_schema = 'public'" 23 | + " AND table_name like :name"; 24 | 25 | SqlQuery sqlQuery = Ebean.createSqlQuery(sql); 26 | sqlQuery.setParameter("name", "o_%"); 27 | 28 | List rows = sqlQuery.findList(); 29 | for (SqlRow sqlRow : rows) { 30 | String tableName = sqlRow.getString("table_name"); 31 | System.out.println(tableName); 32 | } 33 | 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/main/java/org/example/domain/Order.kt: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.Model 4 | import java.sql.Date 5 | import java.util.* 6 | import javax.persistence.* 7 | import javax.validation.constraints.NotNull 8 | 9 | /** 10 | * Order entity bean. 11 | */ 12 | @Entity 13 | @Table(name = "o_order") 14 | public class Order : BaseModel() { 15 | 16 | companion object : Model.Find() {} 17 | 18 | enum class Status { 19 | NEW, 20 | APPROVED, 21 | SHIPPED, 22 | COMPLETE 23 | } 24 | 25 | public var status: Status = Status.NEW; 26 | 27 | public var orderDate: Date? = null; 28 | 29 | public var shipDate: Date? = null; 30 | 31 | @ManyToOne @NotNull 32 | public var customer: Customer? = null; 33 | 34 | @ManyToOne 35 | public var shippingAddress: Address? = null; 36 | 37 | @OneToMany(mappedBy = "order", cascade = arrayOf(CascadeType.PERSIST)) 38 | @OrderBy("id asc") 39 | public var details: MutableList = ArrayList(); 40 | 41 | 42 | } 43 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/main/java/org/example/domain/BaseModel.kt: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.Model 4 | import com.avaje.ebean.annotation.WhenCreated 5 | import com.avaje.ebean.annotation.WhenModified 6 | import java.sql.Timestamp; 7 | 8 | import javax.persistence.Id; 9 | import javax.persistence.MappedSuperclass; 10 | import javax.persistence.Version; 11 | 12 | /** 13 | * Base domain object with Id, version, whenCreated and whenUpdated. 14 | * 15 | *

16 | * Extending Model to enable the 'active record' style. 17 | * 18 | *

19 | * whenCreated and whenUpdated are generally useful for maintaining external search services (like 20 | * elasticsearch) and audit. 21 | */ 22 | @MappedSuperclass 23 | public abstract class BaseModel : Model() { 24 | 25 | @Id 26 | public var id: Long? = null 27 | 28 | @Version 29 | public var version: Long? = null 30 | 31 | @WhenCreated 32 | public var whenCreated: Timestamp? = null 33 | 34 | @WhenModified 35 | public var whenModified: Timestamp? = null 36 | 37 | } 38 | -------------------------------------------------------------------------------- /x-postgres-features/pg-create-backup.sql: -------------------------------------------------------------------------------- 1 | create table p_customer ( 2 | id bigserial not null, 3 | inactive boolean, 4 | name varchar(100), 5 | registered timestamp, 6 | comments varchar(1000), 7 | tags hstore, 8 | version bigint not null, 9 | when_created timestamp not null, 10 | when_updated timestamp not null, 11 | constraint pk_p_customer primary key (id)) 12 | ; 13 | 14 | drop table p_doc; 15 | create table p_doc ( 16 | id bigserial not null, 17 | name varchar(255), 18 | content json, 19 | version bigint not null, 20 | constraint pk_p_doc primary key (id)) 21 | ; 22 | 23 | create table p_user ( 24 | user_id bigserial not null, 25 | name varchar(255), 26 | constraint pk_p_user primary key (user_id)) 27 | ; 28 | 29 | 30 | 31 | select * from p_doc; -------------------------------------------------------------------------------- /x-postgres-history/src/test/java/org/example/domain/CustomerTest.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.DelegateEbeanServer; 4 | import com.avaje.ebean.MockiEbean; 5 | import org.junit.Test; 6 | 7 | import static org.assertj.core.api.Assertions.*; 8 | 9 | public class CustomerTest { 10 | 11 | @Test 12 | public void updateJim() { 13 | 14 | Customer jim = Customer.find.byName("jim"); 15 | //jim.setComments("another update"); 16 | jim.save(); 17 | } 18 | 19 | @Test 20 | public void insert() { 21 | 22 | 23 | DelegateEbeanServer mock = new DelegateEbeanServer(); 24 | mock.withPersisting(true); 25 | 26 | MockiEbean.runWithMock(mock, () -> { 27 | 28 | Customer customer = new Customer(); 29 | customer.setName("jim"); 30 | //customer.setComments("first comment"); 31 | 32 | customer.save(); 33 | 34 | //customer.setComments("second comment"); 35 | customer.save(); 36 | 37 | }); 38 | 39 | assertThat(mock.capturedBeans.save).hasSize(2); 40 | 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /x-dbmigration/src/test/java/org/example/domain/CustomerTest.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.Ebean; 4 | import org.junit.Test; 5 | 6 | import java.sql.Timestamp; 7 | import java.util.List; 8 | 9 | import static org.assertj.core.api.Assertions.assertThat; 10 | 11 | public class CustomerTest { 12 | 13 | @Test 14 | public void canInsert() { 15 | 16 | Customer customer = new Customer(); 17 | customer.setName("Rob"); 18 | //customer.setFoo("one"); 19 | customer.save(); 20 | // 21 | // assertThat(customer.getId()).isNotNull(); 22 | 23 | // Customer rob = Ebean.find(Customer.class) 24 | // .where().eq("name", "Rob") 25 | // .findUnique(); 26 | // 27 | // rob.setShortDesc("cone"); 28 | // Ebean.save(rob); 29 | 30 | // long now = System.currentTimeMillis(); 31 | // Timestamp ts = new Timestamp(now - 1000*60*10); 32 | // 33 | // List list = Ebean.find(Customer.class) 34 | // .asOf(ts) 35 | // .findList(); 36 | // 37 | // System.out.print("asd"); 38 | 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /a-basic/src/main/java/org/example/domain/Product.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | //import org.example.domain.finder.ProductFinder; 4 | 5 | import org.example.domain.finder.ProductFinder; 6 | 7 | import javax.persistence.Entity; 8 | import javax.persistence.Table; 9 | import javax.validation.constraints.Size; 10 | 11 | /** 12 | * Product entity bean. 13 | */ 14 | @Entity 15 | @Table(name = "o_product") 16 | public class Product extends BaseModel { 17 | 18 | public static final ProductFinder find = new ProductFinder(); 19 | 20 | @Size(max = 20) 21 | String sku; 22 | 23 | String name; 24 | 25 | /** 26 | * Return sku. 27 | */ 28 | public String getSku() { 29 | return sku; 30 | } 31 | 32 | /** 33 | * Set sku. 34 | */ 35 | public void setSku(String sku) { 36 | this.sku = sku; 37 | } 38 | 39 | /** 40 | * Return name. 41 | */ 42 | public String getName() { 43 | return name; 44 | } 45 | 46 | /** 47 | * Set name. 48 | */ 49 | public void setName(String name) { 50 | this.name = name; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /x-dbmigration/src/test/resources/ebean-test.properties: -------------------------------------------------------------------------------- 1 | 2 | 3 | #load.properties=${HOME}/config/myapp.ebean.properties 4 | 5 | ebean.ddl.generate=true 6 | ebean.ddl.run=true 7 | datasource.default=mysql 8 | 9 | 10 | ebean.migration.appName=myapp 11 | ebean.migration.resourcePath=dbmigration/myapp 12 | 13 | 14 | ## ------------------------------------------------------------- 15 | ## DataSources (If using default Ebean DataSourceFactory) 16 | ## ------------------------------------------------------------- 17 | 18 | datasource.h2.username=sa 19 | datasource.h2.password= 20 | datasource.h2.databaseUrl=jdbc:h2:mem:tests;DB_CLOSE_DELAY=-1 21 | datasource.h2.databaseDriver=org.h2.Driver 22 | 23 | datasource.mysql.username=mig 24 | datasource.mysql.password=mig 25 | datasource.mysql.databaseUrl=jdbc:mysql://127.0.0.1:3306/mig 26 | datasource.mysql.databaseDriver=com.mysql.jdbc.Driver 27 | 28 | 29 | datasource.pg.username=mig 30 | datasource.pg.password=mig 31 | datasource.pg.databaseUrl=jdbc:postgresql://127.0.0.1:5432/mig 32 | datasource.pg.databaseDriver=org.postgresql.Driver 33 | 34 | -------------------------------------------------------------------------------- /e-groovy/src/main/groovy/org/example/domain/BaseModel.groovy: -------------------------------------------------------------------------------- 1 | package org.example.domain 2 | 3 | import com.avaje.ebean.Model 4 | import com.avaje.ebean.annotation.CreatedTimestamp 5 | import com.avaje.ebean.annotation.UpdatedTimestamp 6 | import groovy.transform.CompileStatic 7 | 8 | import javax.persistence.Id 9 | import javax.persistence.MappedSuperclass 10 | import javax.persistence.Version 11 | import java.sql.Timestamp 12 | 13 | /** 14 | * Base domain object with Id, version, whenCreated and whenUpdated. 15 | * 16 | *

17 | * Extending Model to enable the 'active record' style. 18 | * 19 | *

20 | * whenCreated and whenUpdated are generally useful for maintaining external search services (like 21 | * elasticsearch) and audit. 22 | * @author: Richard Vowles - https://plus.google.com/+RichardVowles 23 | */ 24 | @MappedSuperclass 25 | @CompileStatic 26 | class BaseModel extends Model { 27 | @Id 28 | Long id 29 | 30 | @Version 31 | Long version 32 | 33 | @CreatedTimestamp 34 | Timestamp whenCreated 35 | 36 | @UpdatedTimestamp 37 | Timestamp whenUpdated 38 | } 39 | -------------------------------------------------------------------------------- /a-basic/src/test/java/org/example/domain/ExQueryJoins.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | 4 | import com.avaje.ebean.FetchConfig; 5 | import org.example.ExampleBaseTestCase; 6 | import org.example.service.LoadExampleData; 7 | import org.junit.Test; 8 | 9 | import java.util.List; 10 | 11 | public class ExQueryJoins extends ExampleBaseTestCase { 12 | 13 | @Test 14 | public void simple() { 15 | 16 | LoadExampleData.load(); 17 | 18 | List orders = Order.find.query() 19 | .select("status") 20 | .fetch("customer") 21 | .fetch("customer.contacts") 22 | .fetch("details") 23 | .orderBy("customer.name") 24 | .findList(); 25 | 26 | orders.hashCode(); 27 | 28 | } 29 | 30 | @Test 31 | public void deepOneToMany() { 32 | 33 | LoadExampleData.load(); 34 | 35 | List customers = Customer.find 36 | .query() 37 | .fetch("contacts") 38 | .fetch("contacts.notes") 39 | .orderBy("name") 40 | .findList(); 41 | 42 | customers.hashCode(); 43 | 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /x-postgres-history/src/main/java/org/example/domain/Product.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import org.example.domain.finder.ProductFinder; 4 | 5 | import javax.persistence.Entity; 6 | import javax.persistence.Table; 7 | import javax.validation.constraints.Size; 8 | 9 | /** 10 | * Product entity bean. 11 | */ 12 | @Entity 13 | @Table(name = "product") 14 | public class Product extends BaseModel { 15 | 16 | /** 17 | * Convenience Finder for 'active record' style. 18 | */ 19 | public static final ProductFinder find = new ProductFinder(); 20 | 21 | @Size(max = 20) 22 | String sku; 23 | 24 | String name; 25 | 26 | /** 27 | * Return sku. 28 | */ 29 | public String getSku() { 30 | return sku; 31 | } 32 | 33 | /** 34 | * Set sku. 35 | */ 36 | public void setSku(String sku) { 37 | this.sku = sku; 38 | } 39 | 40 | /** 41 | * Return name. 42 | */ 43 | public String getName() { 44 | return name; 45 | } 46 | 47 | /** 48 | * Set name. 49 | */ 50 | public void setName(String name) { 51 | this.name = name; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /a-basic/src/test/java/org/example/ExampleBaseTestCase.java: -------------------------------------------------------------------------------- 1 | package org.example; 2 | 3 | import org.avaje.agentloader.AgentLoader; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | 7 | 8 | /** 9 | * Used to dynamically load the avaje-ebeanorm-agent. 10 | * 11 | * Alternatives: 12 | * - Use IntelliJ or Eclipse plugins, 13 | * - Rely on maven/ant enhancement (pain in dev, test cycle) 14 | * - Specify the java agent on the command line 15 | * 16 | */ 17 | public class ExampleBaseTestCase { 18 | 19 | protected static Logger logger = LoggerFactory.getLogger(ExampleBaseTestCase.class); 20 | 21 | static { 22 | logger.debug("... preStart"); 23 | //if (!AgentLoader.loadAgentFromClasspath("avaje-ebeanorm-typequery-agent","debug=9")) { 24 | // logger.info("avaje-ebeanorm-tyepquery-agent not found in classpath - not dynamically loaded"); 25 | //} 26 | if (!AgentLoader.loadAgentFromClasspath("avaje-ebeanorm-agent","debug=0;packages=org.example.**")) { 27 | logger.info("avaje-ebeanorm-agent not found in classpath - not dynamically loaded"); 28 | } 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /a-basic/src/test/java/org/example/domain/ExPartialObjects.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | 4 | import org.example.ExampleBaseTestCase; 5 | import org.example.service.LoadExampleData; 6 | import org.junit.Test; 7 | 8 | import java.util.List; 9 | 10 | public class ExPartialObjects extends ExampleBaseTestCase { 11 | 12 | @Test 13 | public void test() { 14 | 15 | LoadExampleData.load(); 16 | 17 | List customerList = Customer.find.where() 18 | .inactive.isTrue() 19 | .findList(); 20 | 21 | Customer customer = Customer. 22 | find.where().id.equalTo(1L) 23 | .select("name") 24 | .findUnique(); 25 | 26 | customer.hashCode(); 27 | 28 | } 29 | 30 | 31 | @Test 32 | public void testFindPartial() { 33 | 34 | LoadExampleData.load(); 35 | 36 | Order order = Order.find.query() 37 | .select("status, orderDate, shipDate") 38 | .fetch("customer", "name") 39 | .fetch("details") 40 | .fetch("details.product", "sku") 41 | .where().idEq(1L) 42 | .findUnique(); 43 | 44 | order.hashCode(); 45 | 46 | } 47 | } 48 | 49 | -------------------------------------------------------------------------------- /e-groovy/src/main/groovy/org/example/domain/Address.groovy: -------------------------------------------------------------------------------- 1 | package org.example.domain 2 | 3 | import groovy.transform.CompileStatic 4 | import groovy.transform.TypeCheckingMode 5 | 6 | import javax.persistence.Entity 7 | import javax.persistence.ManyToOne 8 | import javax.persistence.Table 9 | import javax.validation.constraints.Size 10 | 11 | /** 12 | * 13 | * @author: Richard Vowles - https://plus.google.com/+RichardVowles 14 | * Address entity bean. 15 | */ 16 | @Entity 17 | @Table(name = "o_address") 18 | @CompileStatic 19 | class Address extends BaseModel { 20 | @Size(max = 100) 21 | String line1 22 | 23 | @Size(max = 100) 24 | String line2 25 | 26 | @Size(max = 100) 27 | String city 28 | 29 | @ManyToOne 30 | Country country 31 | 32 | /** 33 | * Create a copy of the address. Used to provide a 'snapshot' of 34 | * the shippingAddress for a give order. 35 | * 36 | * Does not copy inherited fields 37 | */ 38 | public Address createCopy() { 39 | return new Address(line1: line1, line2: line2, city: city, country: country) 40 | } 41 | 42 | public String toString() { 43 | return "$id $line1 $line2 $city $country" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/main/java/org/example/domain/Contact.kt: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.Model 4 | import javax.persistence.Column; 5 | import javax.persistence.Entity; 6 | import javax.persistence.ManyToOne; 7 | import javax.persistence.Table; 8 | import javax.validation.constraints.NotNull 9 | import javax.validation.constraints.Size 10 | 11 | /** 12 | * Contact entity bean. 13 | */ 14 | @Entity 15 | @Table(name = "be_contact") 16 | public class Contact() : BaseModel() { 17 | 18 | 19 | @Size(max = 50) 20 | public var firstName: String? = null 21 | 22 | @Size(max = 50) 23 | public var lastName: String? = null 24 | 25 | @Size(max = 200) 26 | public var email: String? = null; 27 | 28 | @Size(max = 20) 29 | public var phone: String? = null; 30 | 31 | @NotNull 32 | @ManyToOne(optional = false) 33 | public var customer: Customer? = null; 34 | 35 | /** 36 | * Construct with firstName and lastName. 37 | */ 38 | constructor(firstName: String, lastName: String) : this() { 39 | this.firstName = firstName; 40 | this.lastName = lastName; 41 | } 42 | 43 | companion object : Model.Find() {} 44 | } 45 | -------------------------------------------------------------------------------- /e-eventadapters/src/main/java/org/example/domain/User.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import java.util.UUID; 4 | 5 | import javax.persistence.Entity; 6 | import javax.persistence.Table; 7 | 8 | /** 9 | * @author wjx 10 | * 11 | */ 12 | @Entity 13 | @Table(name = "e_user") 14 | public class User extends AbstractModel { 15 | 16 | private static final long serialVersionUID = 1L; 17 | 18 | public static Finder find = new Finder<>(UUID.class, User.class); 19 | 20 | private String name; 21 | 22 | private Integer age; 23 | 24 | public String getName() { 25 | return name; 26 | } 27 | 28 | public void setName(String name) { 29 | this.name = name; 30 | } 31 | 32 | public Integer getAge() { 33 | return age; 34 | } 35 | 36 | public void setAge(Integer age) { 37 | this.age = age; 38 | } 39 | 40 | @Override 41 | public String toString() { 42 | return "User [name=" + name + ", age=" + age + ", id=" + id 43 | + ", creatorId=" + creatorId + ", creatorName=" + creatorName 44 | + ", createTime=" + createTime + ", modifierId=" + modifierId 45 | + ", modifierName=" + modifierName + ", modifyTime=" 46 | + modifyTime + "]"; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /a-basic/src/main/java/org/example/domain/finder/ContactFinder.java: -------------------------------------------------------------------------------- 1 | package org.example.domain.finder; 2 | 3 | import com.avaje.ebean.Finder; 4 | import org.example.domain.Contact; 5 | import org.example.domain.query.QContact; 6 | 7 | import java.util.List; 8 | 9 | public class ContactFinder extends Finder { 10 | 11 | /** 12 | * Construct using the default EbeanServer. 13 | */ 14 | public ContactFinder() { 15 | super(Contact.class); 16 | } 17 | 18 | /** 19 | * Construct with a given EbeanServer. 20 | */ 21 | public ContactFinder(String serverName) { 22 | super(Contact.class, serverName); 23 | } 24 | 25 | /** 26 | * Start a new typed query. 27 | */ 28 | protected QContact where() { 29 | return new QContact(db()); 30 | } 31 | 32 | /** 33 | * Find the contact by exact email address. 34 | */ 35 | public Contact byEmail(String email) { 36 | return where().email.equalTo(email).findUnique(); 37 | } 38 | 39 | /** 40 | * Find by email address ending in (case insensitive). 41 | */ 42 | public List byEmailEndsWith(String emailEndsIn) { 43 | return where().email.iendsWith(emailEndsIn).findList(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /x-json-features/src/main/java/org/example/EJson.java: -------------------------------------------------------------------------------- 1 | package org.example; 2 | 3 | import java.io.Reader; 4 | import java.io.Writer; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | import javax.json.stream.JsonGenerator; 9 | import javax.json.stream.JsonParser; 10 | 11 | public class EJson { 12 | 13 | public static String write(Object object) { 14 | return EJsonWriter.write(object); 15 | } 16 | 17 | public static void write(Object object, Writer writer) { 18 | EJsonWriter.write(object, writer); 19 | } 20 | 21 | public static void write(Object object, JsonGenerator jsonGenerator) { 22 | EJsonWriter.write(object, jsonGenerator); 23 | } 24 | 25 | public static Map parseObject(String json) { 26 | return EJsonReader.parseObject(json); 27 | } 28 | 29 | public static List parseList(String json) { 30 | return EJsonReader.parseList(json); 31 | } 32 | 33 | public static Object parse(String json) { 34 | return EJsonReader.parse(json); 35 | } 36 | 37 | public static Object parse(Reader reader) { 38 | return EJsonReader.parse(reader); 39 | } 40 | 41 | public static Object parse(JsonParser parser) { 42 | return EJsonReader.parse(parser); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /x-postgres-history/src/main/java/org/example/domain/excludem2m/FeatureExcludeM2M.java: -------------------------------------------------------------------------------- 1 | package org.example.domain.excludem2m; 2 | 3 | import org.example.domain.BaseModel; 4 | 5 | import javax.persistence.Entity; 6 | import javax.persistence.JoinTable; 7 | import javax.persistence.ManyToMany; 8 | import javax.persistence.Table; 9 | import javax.validation.constraints.Size; 10 | import java.util.List; 11 | 12 | /** 13 | * Feature entity bean that relates to CustomerExcludeM2M. 14 | */ 15 | @Entity 16 | @Table(name = "feature") 17 | public class FeatureExcludeM2M extends BaseModel { 18 | 19 | @Size(max = 60) 20 | String name; 21 | 22 | @ManyToMany 23 | @JoinTable(name = "customer_feature") 24 | List customers; 25 | 26 | public String toString() { 27 | return name; 28 | } 29 | 30 | /** 31 | * Return name. 32 | */ 33 | public String getName() { 34 | return name; 35 | } 36 | 37 | /** 38 | * Set name. 39 | */ 40 | public void setName(String name) { 41 | this.name = name; 42 | } 43 | 44 | public List getCustomers() { 45 | return customers; 46 | } 47 | 48 | public void setCustomers(List customers) { 49 | this.customers = customers; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /e-eventadapters/src/main/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | %d{HH:mm:ss.SSS} %-5level %logger{125} - %msg%n 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /e-eventadapters/src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | %d{HH:mm:ss.SSS} %-5level %logger{125} - %msg%n 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /x-dbmigration/src/main/resources/ebean.properties: -------------------------------------------------------------------------------- 1 | 2 | 3 | #load.properties=${HOME}/config/myapp.ebean.properties 4 | 5 | ebean.h2.ddl.generate=true 6 | ebean.h2.ddl.run=true 7 | 8 | datasource.default=ora 9 | 10 | 11 | ebean.migration.appName=myapp 12 | ebean.migration.resourcePath=dbmigration/myapp 13 | 14 | 15 | ## ------------------------------------------------------------- 16 | ## DataSources (If using default Ebean DataSourceFactory) 17 | ## ------------------------------------------------------------- 18 | 19 | datasource.h2.username=sa 20 | datasource.h2.password= 21 | datasource.h2.databaseUrl=jdbc:h2:mem:tests;DB_CLOSE_DELAY=-1 22 | datasource.h2.databaseDriver=org.h2.Driver 23 | 24 | datasource.mysql.username=mig 25 | datasource.mysql.password=mig 26 | datasource.mysql.databaseUrl=jdbc:mysql://127.0.0.1:3306/mig 27 | datasource.mysql.databaseDriver=com.mysql.jdbc.Driver 28 | 29 | 30 | datasource.pg.username=mig 31 | datasource.pg.password=mig 32 | datasource.pg.databaseUrl=jdbc:postgresql://127.0.0.1:5432/mig 33 | datasource.pg.databaseDriver=org.postgresql.Driver 34 | 35 | datasource.ora.username=scott 36 | datasource.ora.password=tiger 37 | datasource.ora.databaseUrl=jdbc:oracle:thin:@//127.0.0.1:1521/orcl 38 | datasource.ora.databaseDriver=oracle.jdbc.driver.OracleDriver -------------------------------------------------------------------------------- /x-postgres-features/pg-create.sql: -------------------------------------------------------------------------------- 1 | create table p_customer ( 2 | id bigserial not null, 3 | inactive boolean, 4 | name varchar(100), 5 | registered timestamp, 6 | comments varchar(1000), 7 | tags hstore, 8 | version bigint not null, 9 | when_created timestamp not null, 10 | when_updated timestamp not null, 11 | constraint pk_p_customer primary key (id)) 12 | ; 13 | 14 | create table p_doc ( 15 | id bigserial not null, 16 | name varchar(255), 17 | content jsonb, 18 | version bigint not null, 19 | constraint pk_p_doc primary key (id)) 20 | ; 21 | 22 | create table p_doc_jsonnode ( 23 | id bigserial not null, 24 | name varchar(255), 25 | content jsonb, 26 | version bigint not null, 27 | constraint pk_p_doc_jsonnode primary key (id)) 28 | ; 29 | 30 | create table p_user ( 31 | user_id bigserial not null, 32 | name varchar(255), 33 | constraint pk_p_user primary key (user_id)) 34 | ; 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /x-postgres-history/src/main/java/org/example/domain/Feature.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.JoinTable; 5 | import javax.persistence.ManyToMany; 6 | import javax.persistence.Table; 7 | import javax.validation.constraints.Size; 8 | import java.util.List; 9 | 10 | /** 11 | * Feature entity bean. 12 | */ 13 | @Entity 14 | @Table(name = "feature") 15 | public class Feature extends BaseModel { 16 | 17 | @Size(max = 60) 18 | String name; 19 | 20 | String notes; 21 | 22 | @ManyToMany 23 | @JoinTable(name = "customer_feature") 24 | List customers; 25 | 26 | public String toString() { 27 | return name; 28 | } 29 | 30 | /** 31 | * Return name. 32 | */ 33 | public String getName() { 34 | return name; 35 | } 36 | 37 | /** 38 | * Set name. 39 | */ 40 | public void setName(String name) { 41 | this.name = name; 42 | } 43 | 44 | public List getCustomers() { 45 | return customers; 46 | } 47 | 48 | public void setCustomers(List customers) { 49 | this.customers = customers; 50 | } 51 | 52 | public String getNotes() { 53 | return notes; 54 | } 55 | 56 | public void setNotes(String notes) { 57 | this.notes = notes; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /x-postgres-features/src/test/java/org/example/RawJdbcInsertWithGeneratedKeyTest.java: -------------------------------------------------------------------------------- 1 | package org.example; 2 | 3 | import java.sql.Connection; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | import org.junit.Test; 9 | 10 | import com.avaje.ebean.Ebean; 11 | import com.avaje.ebean.Transaction; 12 | 13 | public class RawJdbcInsertWithGeneratedKeyTest extends ExampleBaseTestCase { 14 | 15 | @Test 16 | public void test() throws SQLException { 17 | 18 | Transaction txn = Ebean.beginTransaction(); 19 | try { 20 | Connection connection = txn.getConnection(); 21 | 22 | String sql = "insert into p_customer (name, version, when_created, when_updated) values (?, 1, now(), now())"; 23 | PreparedStatement pstmt = connection.prepareStatement(sql, new String[]{"id"}); 24 | pstmt.setString(1, "Hello Rob"); 25 | int rows = pstmt.executeUpdate(); 26 | 27 | Long idValue = null; 28 | ResultSet resultSet = pstmt.getGeneratedKeys(); 29 | if(resultSet.next()) { 30 | idValue = resultSet.getLong(1); 31 | } 32 | 33 | System.out.println("rows:"+rows+" idValue:"+idValue); 34 | 35 | } finally { 36 | txn.end(); 37 | } 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /x-postgres-features/src/test/java/org/example/SlashInLikeTest.java: -------------------------------------------------------------------------------- 1 | package org.example; 2 | 3 | import java.util.List; 4 | 5 | import org.example.domain.Customer; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | public class SlashInLikeTest extends ExampleBaseTestCase { 10 | 11 | @Test 12 | public void test() { 13 | 14 | Customer customer = new Customer(); 15 | customer.setName("slash\\monkey"); 16 | customer.save(); 17 | 18 | List list = Customer.find.where().eq("name", "slash\\monkey").findList(); 19 | Assert.assertEquals("equals with slash", 1, list.size()); 20 | 21 | list = Customer.find.where().like("name", "slash\\mon%").findList(); 22 | Assert.assertEquals("like with slash", 1, list.size()); 23 | 24 | list = Customer.find.where().raw("name like 'slash\\mon%' escape''").findList(); 25 | Assert.assertEquals("like with slash using raw", 1, list.size()); 26 | 27 | list = Customer.find.where().raw("name like ? escape''", "slash\\mon%").findList(); 28 | Assert.assertEquals("like with slash using raw", 1, list.size()); 29 | 30 | list = Customer.find.where().raw("name like 'slash\\mon%'").findList(); 31 | Assert.assertEquals("raw like with no escape - not found", 0, list.size()); 32 | 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /x-postgres-features/src/test/java/org/example/domain/type/ScalarTypeJsonNodeTextTest.java: -------------------------------------------------------------------------------- 1 | package org.example.domain.type; 2 | 3 | import org.junit.Test; 4 | 5 | import java.sql.Types; 6 | 7 | import static org.junit.Assert.*; 8 | 9 | /** 10 | * 11 | */ 12 | public class ScalarTypeJsonNodeTextTest { 13 | 14 | // ScalarTypeJsonNodePostgres type = new ScalarTypeJsonNodePostgres.JSON(new ObjectMapper()); 15 | 16 | @Test 17 | public void testParse() throws Exception { 18 | 19 | // String jsonInput = "{\"id\":123123, \"name\":\"foo\"}"; 20 | // 21 | // JsonNode node = type.parse(jsonInput); 22 | // assertEquals(123123L, node.get("id").asLong()); 23 | // assertEquals("foo", node.get("name").asText()); 24 | // 25 | // node = type.convertFromDbString(jsonInput); 26 | // assertEquals(123123L, node.get("id").asLong()); 27 | // assertEquals("foo", node.get("name").asText()); 28 | // 29 | // String rawJson = type.formatValue(node); 30 | // 31 | // assertTrue(rawJson, rawJson.contains("\"id\":123123")); 32 | // assertTrue(rawJson, rawJson.contains("\"name\":\"foo\"")); 33 | // 34 | // rawJson = type.convertToDbString(node); 35 | // assertTrue(rawJson, rawJson.contains("\"id\":123123")); 36 | // assertTrue(rawJson, rawJson.contains("\"name\":\"foo\"")); 37 | 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /x-dbmigration/src/main/java/org/example/domain/Customer.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.annotation.History; 4 | import com.avaje.ebean.annotation.Index; 5 | 6 | import javax.persistence.Column; 7 | import javax.persistence.Entity; 8 | import javax.persistence.Table; 9 | import javax.persistence.UniqueConstraint; 10 | 11 | /** 12 | */ 13 | //@History 14 | @Entity 15 | @Table(name = "customer") 16 | public class Customer extends BaseModel { 17 | 18 | @Index() 19 | @Column(length = 50, nullable = false) 20 | String name; 21 | 22 | String shortDesc; 23 | 24 | @Column(length = 50) 25 | String fooButReallyLooooooooooooooooog; 26 | 27 | public String getName() { 28 | return name; 29 | } 30 | 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | 35 | public String getShortDesc() { 36 | return shortDesc; 37 | } 38 | 39 | public void setShortDesc(String shortDesc) { 40 | this.shortDesc = shortDesc; 41 | } 42 | 43 | public String getFooButReallyLooooooooooooooooog() { 44 | return fooButReallyLooooooooooooooooog; 45 | } 46 | 47 | public void setFooButReallyLooooooooooooooooog(String fooButReallyLooooooooooooooooog) { 48 | this.fooButReallyLooooooooooooooooog = fooButReallyLooooooooooooooooog; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /a-basic/src/test/java/org/example/domain/LargeQueryWithFindIterateTest.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import org.example.ExampleBaseTestCase; 4 | import org.junit.Test; 5 | 6 | import com.avaje.ebean.QueryIterator; 7 | 8 | public class LargeQueryWithFindIterateTest extends ExampleBaseTestCase { 9 | 10 | @Test 11 | public void testFindIterate() { 12 | 13 | // insert 1000 customers 14 | int j = 0; 15 | for (int i = 0; i < 1000; i++) { 16 | Customer customer = new Customer(); 17 | customer.setName("Hello"+j++); 18 | customer.save(); 19 | } 20 | 21 | QueryIterator iterate = 22 | Customer.find 23 | .query().select("id") //.fetch("contacts", new FetchConfig().lazy(20)) 24 | .findIterate(); 25 | 26 | try { 27 | 28 | while (iterate.hasNext()) { 29 | Customer customer = iterate.next(); 30 | // do something interesting with customer 31 | //customer.getContacts().size(); 32 | System.out.println("got name "+customer.getId()+" "+customer.getName()); 33 | } 34 | 35 | } finally { 36 | // close the underlying resources held by the QueryIterator 37 | // those are: ResultSet and Connection 38 | iterate.close(); 39 | } 40 | 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /x-postgres-features/src/main/java/org/example/domain/SimpleDoc.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.Model; 4 | import com.avaje.ebean.annotation.DbJsonB; 5 | 6 | import javax.persistence.Entity; 7 | import javax.persistence.Id; 8 | import javax.persistence.Table; 9 | import javax.persistence.Version; 10 | import java.util.Map; 11 | 12 | @Entity 13 | @Table(name="p_doc") 14 | public class SimpleDoc extends Model { 15 | 16 | public static Find find = new Find(){}; 17 | 18 | @Id 19 | Long id; 20 | 21 | @Version 22 | Long version; 23 | 24 | String name; 25 | 26 | @DbJsonB 27 | Map content; 28 | 29 | public Long getId() { 30 | return id; 31 | } 32 | 33 | public void setId(Long id) { 34 | this.id = id; 35 | } 36 | 37 | public Long getVersion() { 38 | return version; 39 | } 40 | 41 | public void setVersion(Long version) { 42 | this.version = version; 43 | } 44 | 45 | public String getName() { 46 | return name; 47 | } 48 | 49 | public void setName(String name) { 50 | this.name = name; 51 | } 52 | 53 | public Map getContent() { 54 | return content; 55 | } 56 | 57 | public void setContent(Map content) { 58 | this.content = content; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/test/java/org/example/domain/ExamplePartialObjectQueryTest.kt: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import org.example.ExampleBaseTestCase; 4 | import org.junit.Test; 5 | import org.example.service.LoadExampleData 6 | 7 | public class ExamplePartialObjectQueryTest : ExampleBaseTestCase() { 8 | 9 | @Test 10 | fun test() { 11 | 12 | val customer = 13 | Customer.select("name, email") 14 | .where().idEq(12) 15 | .findUnique(); 16 | 17 | System.out.println("name: ${customer?.name}"); 18 | } 19 | 20 | @Test 21 | fun automaticallyAddJoins() { 22 | 23 | LoadExampleData().load() 24 | 25 | val country = Country.ref("NZ") 26 | 27 | val customer = 28 | Customer 29 | .select("name") 30 | .where() 31 | .eq("name","Rob") 32 | .eq("billingAddress.country", country) 33 | .findUnique(); 34 | 35 | if (customer != null) { 36 | val customerName = customer.name; 37 | val countryName = customer.billingAddress?.country?.name; 38 | 39 | if (null == customer.billingAddress?.country?.name) { 40 | System.out.println("got a null"); 41 | } 42 | 43 | System.out.println("name: ${customer.name} $customerName ... and ${customer.billingAddress?.country} $countryName"); 44 | } 45 | 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /x-postgres-features/pg-create-all.sql: -------------------------------------------------------------------------------- 1 | create table p_customer ( 2 | id bigserial not null, 3 | inactive boolean, 4 | name varchar(100), 5 | registered timestamp, 6 | comments varchar(1000), 7 | tags hstore, 8 | version bigint not null, 9 | when_created timestamp not null, 10 | when_updated timestamp not null, 11 | constraint pk_p_customer primary key (id) 12 | ); 13 | 14 | create table p_doc ( 15 | id bigserial not null, 16 | name varchar(255), 17 | content jsonb, 18 | version bigint not null, 19 | constraint pk_p_doc primary key (id) 20 | ); 21 | 22 | create table p_doc_jsonnode ( 23 | id bigserial not null, 24 | name varchar(255), 25 | content jsonb, 26 | version bigint not null, 27 | constraint pk_p_doc_jsonnode primary key (id) 28 | ); 29 | 30 | create table p_user ( 31 | user_id bigserial not null, 32 | name varchar(255), 33 | constraint pk_p_user primary key (user_id) 34 | ); 35 | 36 | -------------------------------------------------------------------------------- /x-dbmigration/src/main/java/org/example/domain/BaseModel.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.Model; 4 | import com.avaje.ebean.annotation.WhenCreated; 5 | import com.avaje.ebean.annotation.WhenModified; 6 | 7 | import javax.persistence.Id; 8 | import javax.persistence.MappedSuperclass; 9 | import javax.persistence.Version; 10 | import java.sql.Timestamp; 11 | 12 | /** 13 | */ 14 | @MappedSuperclass 15 | public class BaseModel extends Model { 16 | 17 | @Id 18 | Long id; 19 | 20 | @WhenCreated 21 | Timestamp whenCreated; 22 | 23 | @WhenModified 24 | Timestamp whenModified; 25 | 26 | @Version 27 | Long version; 28 | 29 | public Long getId() { 30 | return id; 31 | } 32 | 33 | public void setId(Long id) { 34 | this.id = id; 35 | } 36 | 37 | public Timestamp getWhenCreated() { 38 | return whenCreated; 39 | } 40 | 41 | public void setWhenCreated(Timestamp whenCreated) { 42 | this.whenCreated = whenCreated; 43 | } 44 | 45 | public Timestamp getWhenModified() { 46 | return whenModified; 47 | } 48 | 49 | public void setWhenModified(Timestamp whenModified) { 50 | this.whenModified = whenModified; 51 | } 52 | 53 | public Long getVersion() { 54 | return version; 55 | } 56 | 57 | public void setVersion(Long version) { 58 | this.version = version; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /a-basic/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 6 | 7 | 8 | 9 | 10 | 12 | log/application.log 13 | 14 | log/application.%d{yyyy-MM-dd}.log 15 | 90 16 | 17 | 18 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /e-groovy/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 6 | 7 | 8 | 9 | 10 | 12 | log/application.log 13 | 14 | log/application.%d{yyyy-MM-dd}.log 15 | 90 16 | 17 | 18 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 6 | 7 | 8 | 9 | 10 | 12 | log/application.log 13 | 14 | log/application.%d{yyyy-MM-dd}.log 15 | 90 16 | 17 | 18 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /x-postgres-features/src/main/java/org/example/domain/SimpleDocUsingJsonNode.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.Model; 4 | import com.avaje.ebean.annotation.DbJsonB; 5 | import com.fasterxml.jackson.databind.JsonNode; 6 | 7 | import javax.persistence.Entity; 8 | import javax.persistence.Id; 9 | import javax.persistence.Table; 10 | import javax.persistence.Version; 11 | 12 | 13 | @Entity 14 | @Table(name = "p_doc_jsonnode") 15 | public class SimpleDocUsingJsonNode extends Model { 16 | 17 | public static Find find = new Find(){}; 18 | 19 | @Id 20 | Long id; 21 | 22 | @Version 23 | Long version; 24 | 25 | String name; 26 | 27 | @DbJsonB 28 | JsonNode content; 29 | 30 | public Long getId() { 31 | return id; 32 | } 33 | 34 | public void setId(Long id) { 35 | this.id = id; 36 | } 37 | 38 | public Long getVersion() { 39 | return version; 40 | } 41 | 42 | public void setVersion(Long version) { 43 | this.version = version; 44 | } 45 | 46 | public String getName() { 47 | return name; 48 | } 49 | 50 | public void setName(String name) { 51 | this.name = name; 52 | } 53 | 54 | public JsonNode getContent() { 55 | return content; 56 | } 57 | 58 | public void setContent(JsonNode content) { 59 | this.content = content; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /x-postgres-features/src/test/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 6 | 7 | 8 | 9 | 10 | 12 | log/application.log 13 | 14 | log/application.%d{yyyy-MM-dd}.log 15 | 90 16 | 17 | 18 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /x-postgres-history/src/main/java/org/example/domain/Country.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.annotation.CacheStrategy; 4 | import com.avaje.ebean.annotation.CacheTuning; 5 | import org.example.domain.finder.CountryFinder; 6 | 7 | import javax.persistence.Entity; 8 | import javax.persistence.Id; 9 | import javax.persistence.Table; 10 | import javax.validation.constraints.Size; 11 | 12 | /** 13 | * Country entity bean. 14 | */ 15 | @CacheStrategy(readOnly = true) 16 | @CacheTuning(maxSize = 500) 17 | @Entity 18 | @Table(name = "country") 19 | public class Country { 20 | 21 | /** 22 | * Convenience Finder for 'active record' style. 23 | */ 24 | public static final CountryFinder find = new CountryFinder(); 25 | 26 | @Id 27 | @Size(max = 2) 28 | String code; 29 | 30 | @Size(max = 60) 31 | String name; 32 | 33 | public String toString() { 34 | return code; 35 | } 36 | 37 | /** 38 | * Return code. 39 | */ 40 | public String getCode() { 41 | return code; 42 | } 43 | 44 | /** 45 | * Set code. 46 | */ 47 | public void setCode(String code) { 48 | this.code = code; 49 | } 50 | 51 | /** 52 | * Return name. 53 | */ 54 | public String getName() { 55 | return name; 56 | } 57 | 58 | /** 59 | * Set name. 60 | */ 61 | public void setName(String name) { 62 | this.name = name; 63 | } 64 | 65 | 66 | } 67 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/main/java/org/example/domain/OrderDetail.kt: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.Model 4 | import javax.persistence.Entity; 5 | import javax.persistence.ManyToOne; 6 | import javax.persistence.Table; 7 | import javax.validation.constraints.NotNull 8 | 9 | /** 10 | * Order Detail entity bean. 11 | */ 12 | @Entity 13 | @Table(name = "o_order_detail") 14 | public class OrderDetail() : BaseModel() { 15 | 16 | companion object : Model.Find() {} 17 | 18 | /** 19 | * Construct with product, order quantity and unit price. 20 | */ 21 | constructor(product: Product, orderQty: Int, unitPrice: Double) : this() { 22 | this.product = product 23 | this.orderQty = orderQty 24 | this.unitPrice = unitPrice 25 | } 26 | 27 | /** 28 | * The owning order - should be not null really. 29 | */ 30 | @NotNull 31 | @ManyToOne 32 | public var order: Order? = null; 33 | 34 | public var orderQty: Int? = null; 35 | 36 | public var shipQty: Int? = null; 37 | 38 | public var unitPrice: Double? = null; 39 | 40 | @NotNull 41 | @ManyToOne 42 | public var product: Product? = null; 43 | 44 | /** 45 | * Helper method to set some properties. 46 | */ 47 | fun set(product: Product, orderQty: Int, unitPrice: Double) { 48 | this.product = product; 49 | this.unitPrice = unitPrice 50 | this.orderQty = orderQty 51 | } 52 | 53 | 54 | } 55 | -------------------------------------------------------------------------------- /a-basic/src/main/java/org/example/domain/Country.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.Id; 5 | import javax.persistence.Table; 6 | import javax.validation.constraints.Size; 7 | 8 | import com.avaje.ebean.annotation.CacheStrategy; 9 | import com.avaje.ebean.annotation.CacheTuning; 10 | import org.example.domain.finder.CountryFinder; 11 | 12 | /** 13 | * Country entity bean. 14 | */ 15 | @CacheStrategy(readOnly = true)//, warmingQuery = "order by name") 16 | @CacheTuning(maxSize = 500) 17 | @Entity 18 | @Table(name = "o_country") 19 | public class Country { 20 | 21 | /** 22 | * Convenience Finder for 'active record' style. 23 | */ 24 | public static final CountryFinder find = new CountryFinder(); 25 | 26 | @Id 27 | @Size(max = 2) 28 | String code; 29 | 30 | @Size(max = 60) 31 | String name; 32 | 33 | public String toString() { 34 | return code; 35 | } 36 | 37 | /** 38 | * Return code. 39 | */ 40 | public String getCode() { 41 | return code; 42 | } 43 | 44 | /** 45 | * Set code. 46 | */ 47 | public void setCode(String code) { 48 | this.code = code; 49 | } 50 | 51 | /** 52 | * Return name. 53 | */ 54 | public String getName() { 55 | return name; 56 | } 57 | 58 | /** 59 | * Set name. 60 | */ 61 | public void setName(String name) { 62 | this.name = name; 63 | } 64 | 65 | 66 | } 67 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/test/java/org/example/domain/CreateCountryTest.kt: -------------------------------------------------------------------------------- 1 | package org.example.domain 2 | 3 | import com.avaje.ebean.Ebean 4 | import org.example.ExampleBaseTestCase 5 | import org.junit.Test 6 | 7 | class CreateCountryTest : ExampleBaseTestCase() { 8 | 9 | @Test fun doInsert() { 10 | 11 | // val defaultServer = Ebean.getDefaultServer() 12 | 13 | // val nzl = QCountry() 14 | // .code.equalTo("NZ") 15 | // .findUnique(); 16 | // 17 | // assertNull(nzl); 18 | // 19 | // var cust = Customer() 20 | // var size = cust.contacts.size; 21 | 22 | var sa = Country(code = "SA", name = "South Af"); 23 | sa.save(); 24 | 25 | //sa = Country(code="SA", name="South Af"); 26 | //sa.save(); 27 | 28 | val mutableList = Country.all(); 29 | mutableList.size; 30 | 31 | //Country.db().save(null); 32 | 33 | //val any = Country.db().getServerCacheManager().getBeanCache(Country.javaClass).get("NZ") 34 | //any.toString(); 35 | 36 | val country = Country.byId("NZ") 37 | 38 | country?.name = "something" 39 | country?.save(); 40 | 41 | val ebeanServer = Ebean.getDefaultServer(); 42 | 43 | val nzC = Ebean.find(Country::class.java, "NZ") 44 | nzC?.name = "something" 45 | 46 | ebeanServer.find(Country::class.java, "NZ"); 47 | 48 | 49 | val nzUni = Country.where().findUnique() 50 | nzUni?.code; 51 | 52 | val nz = Country.byId("NZ") 53 | nz?.code; 54 | 55 | } 56 | 57 | } -------------------------------------------------------------------------------- /x-postgres-features/ora-create-all.sql: -------------------------------------------------------------------------------- 1 | create table p_customer ( 2 | id number(19) not null, 3 | inactive number(1), 4 | name varchar2(100), 5 | registered timestamp, 6 | comments varchar2(1000), 7 | tags hstore, 8 | version number(19) not null, 9 | when_created timestamp not null, 10 | when_updated timestamp not null, 11 | constraint pk_p_customer primary key (id) 12 | ); 13 | create sequence p_customer_seq; 14 | 15 | create table p_doc ( 16 | id number(19) not null, 17 | name varchar2(255), 18 | content clob, 19 | version number(19) not null, 20 | constraint pk_p_doc primary key (id) 21 | ); 22 | create sequence p_doc_seq; 23 | 24 | create table p_doc_jsonnode ( 25 | id number(19) not null, 26 | name varchar2(255), 27 | content clob, 28 | version number(19) not null, 29 | constraint pk_p_doc_jsonnode primary key (id) 30 | ); 31 | create sequence p_doc_jsonnode_seq; 32 | 33 | create table p_user ( 34 | user_id number(19) not null, 35 | name varchar2(255), 36 | constraint pk_p_user primary key (user_id) 37 | ); 38 | create sequence p_user_seq; 39 | 40 | -------------------------------------------------------------------------------- /e-groovy/src/main/groovy/org/example/domain/Order.groovy: -------------------------------------------------------------------------------- 1 | package org.example.domain 2 | 3 | import groovy.transform.CompileStatic 4 | 5 | import javax.persistence.CascadeType 6 | import javax.persistence.Entity 7 | import javax.persistence.ManyToOne 8 | import javax.persistence.OneToMany 9 | import javax.persistence.Table 10 | import javax.validation.constraints.NotNull 11 | 12 | /** 13 | * 14 | * @author: Richard Vowles - https://plus.google.com/+RichardVowles 15 | * Order entity bean. 16 | */ 17 | @Entity 18 | @Table(name = "o_order") 19 | @CompileStatic 20 | class Order extends BaseModel { 21 | public enum Status { 22 | NEW, APPROVED, SHIPPED, COMPLETE 23 | } 24 | 25 | Status status 26 | 27 | java.sql.Date orderDate 28 | 29 | java.sql.Date shipDate 30 | 31 | @NotNull 32 | @ManyToOne 33 | Customer customer 34 | 35 | @ManyToOne 36 | Address shippingAddress 37 | 38 | @OneToMany(cascade = CascadeType.ALL, mappedBy = "order") 39 | @javax.persistence.OrderBy("id asc") 40 | List details 41 | 42 | public String toString() { 43 | return "$id $status: $status, $customer: $customer" 44 | } 45 | 46 | /** 47 | * Set the customer with their current shipping address. 48 | */ 49 | public void setCustomerWithShipping(Customer customer) { 50 | this.customer = customer 51 | this.shippingAddress = customer.shippingAddress 52 | } 53 | 54 | public void addDetail(OrderDetail detail) { 55 | if (details == null) { 56 | details = [] 57 | } 58 | 59 | details.add(detail) 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/main/java/org/example/domain/Customer.kt: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.Model 4 | import java.util.* 5 | import javax.persistence.* 6 | import javax.validation.constraints.Size 7 | 8 | /** 9 | * Customer entity bean. 10 | */ 11 | @Entity 12 | @Table(name = "be_customer") 13 | public class Customer() : BaseModel() { 14 | 15 | companion object : Model.Find() {} 16 | 17 | public var inactive: Boolean = false; 18 | 19 | @Size(max = 100) 20 | public var name: String? = null; 21 | 22 | public var registered: Date? = null; 23 | 24 | @Size(max = 1000) 25 | public var comments: String? = null; 26 | 27 | @ManyToOne(cascade = arrayOf(CascadeType.ALL)) 28 | public var billingAddress: Address? = null; 29 | 30 | @ManyToOne(cascade = arrayOf(CascadeType.ALL)) 31 | public var shippingAddress: Address? = null; 32 | 33 | @OneToMany(mappedBy = "customer", cascade = arrayOf(CascadeType.PERSIST)) 34 | public var contacts: MutableList = ArrayList(); 35 | 36 | constructor (name: String) : this() { 37 | this.name = name; 38 | } 39 | 40 | override public fun toString(): String { 41 | return "customer(id:$id name:$name)"; 42 | } 43 | 44 | /** 45 | * Helper method to add a contact to the customer. 46 | */ 47 | fun addContact(contact: Contact) { 48 | 49 | // setting the customer is automatically done when Ebean does 50 | // a cascade save from customer to contacts. 51 | contact.customer = this; 52 | contacts.add(contact); 53 | } 54 | 55 | 56 | } 57 | -------------------------------------------------------------------------------- /e-eventadapters/src/test/java/org/example/domain/UserTest.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertNotNull; 5 | 6 | import org.junit.Test; 7 | 8 | /** 9 | * Test the use of BusinessBeanPersistAdapter to set additional user id and name properties on 10 | * preInsert and preUpdate. 11 | */ 12 | public class UserTest extends ExampleBaseTestCase { 13 | 14 | @Test 15 | public void testBeanPersist() throws Exception { 16 | 17 | // ... the BusinessBeanPersistAdapter will set the 18 | // created user id and name onto the bean 19 | User user = new User(); 20 | user.setName("Test"); 21 | user.setAge(20); 22 | user.save(); 23 | 24 | assertNotNull(user.getId()); 25 | assertEquals("CreatorId", user.getCreatorId()); 26 | assertEquals("CreatorName", user.getCreatorName()); 27 | 28 | // stateless update ... the BusinessBeanPersistAdapter will 29 | // set the modified user id and name onto the bean 30 | User user2 = new User(); 31 | user2.setId(user.getId()); 32 | user2.setName("Test2"); 33 | user2.update(); 34 | 35 | // check that all the values are expected with the 36 | // appropriate values set by BusinessBeanPersistAdapter 37 | User user3 = User.find.byId(user.getId()); 38 | 39 | assertEquals("CreatorId", user3.getCreatorId()); 40 | assertEquals("CreatorName", user3.getCreatorName()); 41 | assertEquals("ModifierId", user3.getModifierId()); 42 | assertEquals("ModifierName", user3.getModifierName()); 43 | 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /e-eventadapters/src/main/java/org/example/domain/BusinessBeanPersistAdapter.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.event.BeanPersistAdapter; 4 | import com.avaje.ebean.event.BeanPersistRequest; 5 | 6 | /** 7 | * Example which sets user id and name on insert and update of beans 8 | * that extend AbstractModel. 9 | * 10 | * @author wjx 11 | */ 12 | public class BusinessBeanPersistAdapter extends BeanPersistAdapter { 13 | 14 | @Override 15 | public boolean isRegisterFor(Class cls) { 16 | return AbstractModel.class.isAssignableFrom(cls); 17 | } 18 | 19 | @Override 20 | public boolean preInsert(BeanPersistRequest request) { 21 | 22 | Object bean = request.getBean(); 23 | AbstractModel model = (AbstractModel) bean; 24 | 25 | // Typically get user details from a ThreadLocal security context 26 | String userId = "CreatorId"; 27 | String userName = "CreatorName"; 28 | 29 | model.setCreatorId(userId); 30 | model.setCreatorName(userName); 31 | model.setModifierId(userId); 32 | model.setModifierName(userName); 33 | 34 | return super.preInsert(request); 35 | } 36 | 37 | @Override 38 | public boolean preUpdate(BeanPersistRequest request) { 39 | 40 | Object bean = request.getBean(); 41 | AbstractModel model = (AbstractModel) bean; 42 | 43 | // Typically get user details from a ThreadLocal security context 44 | String userId = "ModifierId"; 45 | String userName = "ModifierName"; 46 | 47 | model.setModifierId(userId); 48 | model.setModifierName(userName); 49 | 50 | return super.preUpdate(request); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /e-groovy/src/main/groovy/org/example/domain/Customer.groovy: -------------------------------------------------------------------------------- 1 | package org.example.domain 2 | 3 | import com.avaje.ebean.Model 4 | import groovy.transform.CompileStatic 5 | 6 | import javax.persistence.CascadeType 7 | import javax.persistence.Column 8 | import javax.persistence.Entity 9 | import javax.persistence.ManyToOne 10 | import javax.persistence.OneToMany 11 | import javax.persistence.Table 12 | 13 | /** 14 | * 15 | * @author: Richard Vowles - https://plus.google.com/+RichardVowles 16 | * Customer entity bean. 17 | */ 18 | @Entity 19 | @Table(name="be_customer") 20 | @CompileStatic 21 | class Customer extends BaseModel { 22 | /** 23 | * Convenience Finder for 'active record' style. 24 | */ 25 | public static final Model.Finder find = new Model.Finder(Long, Customer) 26 | 27 | boolean inactive 28 | 29 | @Column(length=100) 30 | String name 31 | 32 | Date registered 33 | 34 | @Column(length=1000) 35 | String comments 36 | 37 | @ManyToOne(cascade=CascadeType.ALL) 38 | Address billingAddress 39 | 40 | @ManyToOne(cascade=CascadeType.ALL) 41 | Address shippingAddress 42 | 43 | @OneToMany(mappedBy="customer", cascade=CascadeType.PERSIST) 44 | List contacts 45 | 46 | /** 47 | * Helper method to add a contact to the customer. 48 | */ 49 | public void addContact(Contact contact) { 50 | if (contacts == null) { 51 | contacts = [] 52 | } 53 | // setting the customer is automatically done when Ebean does 54 | // a cascade save from customer to contacts. 55 | contact.customer = this 56 | 57 | contacts.add(contact); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /x-json-features/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | org.example 5 | example-json-features 6 | 1.0.1-SNAPSHOT 7 | example-json-features 8 | 9 | 10 | 11 | 12 | org.avaje.ebeanorm 13 | avaje-ebeanorm 14 | 4.0.5-SNAPSHOT 15 | 16 | 17 | 18 | io.fastjson 19 | boon 20 | 0.21 21 | 22 | 23 | 24 | com.fasterxml.jackson.core 25 | jackson-databind 26 | 2.4.0 27 | 28 | 29 | 37 | 38 | org.glassfish 39 | javax.json 40 | 1.0.4 41 | 42 | 43 | 44 | junit 45 | junit 46 | 4.11 47 | test 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /a-basic/src/main/java/org/example/domain/finder/OrderFinder.java: -------------------------------------------------------------------------------- 1 | package org.example.domain.finder; 2 | 3 | import com.avaje.ebean.Finder; 4 | import com.avaje.ebean.OrderBy; 5 | import com.avaje.ebean.PagedList; 6 | import com.avaje.ebean.Query; 7 | import org.example.domain.Order; 8 | import org.example.domain.query.QOrder; 9 | 10 | import java.sql.Date; 11 | import java.util.List; 12 | 13 | public class OrderFinder extends Finder { 14 | 15 | /** 16 | * Construct using the default EbeanServer. 17 | */ 18 | public OrderFinder() { 19 | super(Order.class); 20 | } 21 | 22 | /** 23 | * Construct with a given EbeanServer. 24 | */ 25 | public OrderFinder(String serverName) { 26 | super(Order.class, serverName); 27 | } 28 | 29 | /** 30 | * Start a new typed query. 31 | */ 32 | protected QOrder where() { 33 | return new QOrder(db()); 34 | } 35 | 36 | /** 37 | * Find orders with the given status. 38 | */ 39 | public List byStatus(Order.Status... values) { 40 | 41 | return where() 42 | .status.in(values) 43 | .orderBy() 44 | .orderDate.desc() 45 | .id.desc() 46 | .findList(); 47 | } 48 | 49 | /** 50 | * Find new orders with an orderDate after the given since value. 51 | */ 52 | public PagedList newOrdersSince(Date since, int pageIndex) { 53 | 54 | return where() 55 | .status.in() 56 | .status.eq(Order.Status.NEW) 57 | .or() 58 | .orderDate.after(since) 59 | .orderDate.isNull() 60 | .endOr() 61 | .findPagedList(pageIndex, 100); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /x-postgres-features/src/test/java/org/example/RawJdbcHstoreTest.java: -------------------------------------------------------------------------------- 1 | package org.example; 2 | 3 | import java.sql.Connection; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | import java.util.HashMap; 8 | import java.util.Map; 9 | 10 | import org.junit.Test; 11 | 12 | import com.avaje.ebean.Ebean; 13 | import com.avaje.ebean.Transaction; 14 | 15 | public class RawJdbcHstoreTest extends ExampleBaseTestCase { 16 | 17 | @Test 18 | public void test() throws SQLException { 19 | 20 | Transaction txn = Ebean.beginTransaction(); 21 | try { 22 | Connection connection = txn.getConnection(); 23 | 24 | String insert = "update p_customer set tags = ? where id = 1"; 25 | PreparedStatement pstmt1 = connection.prepareStatement(insert); 26 | 27 | Map map = new HashMap<>(); 28 | map.put("one","123123"); 29 | map.put("two","hello"); 30 | 31 | pstmt1.setObject(1, map); 32 | int rows = pstmt1.executeUpdate(); 33 | System.out.println("updated rows "+rows); 34 | 35 | String sql = "select id, name, tags from p_customer"; 36 | PreparedStatement pstmt = connection.prepareStatement(sql); 37 | 38 | ResultSet rset = pstmt.executeQuery(); 39 | 40 | while( rset.next()) { 41 | Long id = rset.getLong(1); 42 | String name = rset.getString(2); 43 | Object tags = rset.getObject(3); 44 | 45 | System.out.println("rows:"+id+" "+name+" "+tags); 46 | } 47 | 48 | 49 | } finally { 50 | txn.end(); 51 | } 52 | 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /a-basic/src/main/java/org/example/domain/BaseModel.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import java.sql.Timestamp; 4 | 5 | import javax.persistence.Id; 6 | import javax.persistence.MappedSuperclass; 7 | import javax.persistence.Version; 8 | 9 | import com.avaje.ebean.Model; 10 | import com.avaje.ebean.annotation.CreatedTimestamp; 11 | import com.avaje.ebean.annotation.UpdatedTimestamp; 12 | 13 | /** 14 | * Base domain object with Id, version, whenCreated and whenUpdated. 15 | * 16 | *

17 | * Extending Model to enable the 'active record' style. 18 | * 19 | *

20 | * whenCreated and whenUpdated are generally useful for maintaining external search services (like 21 | * elasticsearch) and audit. 22 | */ 23 | @MappedSuperclass 24 | public abstract class BaseModel extends Model { 25 | 26 | @Id 27 | Long id; 28 | 29 | @Version 30 | Long version; 31 | 32 | @CreatedTimestamp 33 | Timestamp whenCreated; 34 | 35 | @UpdatedTimestamp 36 | Timestamp whenUpdated; 37 | 38 | public Long getId() { 39 | return id; 40 | } 41 | 42 | public void setId(Long id) { 43 | this.id = id; 44 | } 45 | 46 | public Long getVersion() { 47 | return version; 48 | } 49 | 50 | public void setVersion(Long version) { 51 | this.version = version; 52 | } 53 | 54 | public Timestamp getWhenCreated() { 55 | return whenCreated; 56 | } 57 | 58 | public void setWhenCreated(Timestamp whenCreated) { 59 | this.whenCreated = whenCreated; 60 | } 61 | 62 | public Timestamp getWhenUpdated() { 63 | return whenUpdated; 64 | } 65 | 66 | public void setWhenUpdated(Timestamp whenUpdated) { 67 | this.whenUpdated = whenUpdated; 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /a-basic/pg-drop-all.sql: -------------------------------------------------------------------------------- 1 | alter table o_address drop constraint if exists fk_o_address_country_code; 2 | drop index if exists ix_o_address_country_code; 3 | 4 | alter table be_contact drop constraint if exists fk_be_contact_customer_id; 5 | drop index if exists ix_be_contact_customer_id; 6 | 7 | alter table contact_note drop constraint if exists fk_contact_note_contact_id; 8 | drop index if exists ix_contact_note_contact_id; 9 | 10 | alter table be_customer drop constraint if exists fk_be_customer_billing_address_id; 11 | drop index if exists ix_be_customer_billing_address_id; 12 | 13 | alter table be_customer drop constraint if exists fk_be_customer_shipping_address_id; 14 | drop index if exists ix_be_customer_shipping_address_id; 15 | 16 | alter table o_order drop constraint if exists fk_o_order_customer_id; 17 | drop index if exists ix_o_order_customer_id; 18 | 19 | alter table o_order drop constraint if exists fk_o_order_shipping_address_id; 20 | drop index if exists ix_o_order_shipping_address_id; 21 | 22 | alter table o_order_detail drop constraint if exists fk_o_order_detail_order_id; 23 | drop index if exists ix_o_order_detail_order_id; 24 | 25 | alter table o_order_detail drop constraint if exists fk_o_order_detail_product_id; 26 | drop index if exists ix_o_order_detail_product_id; 27 | 28 | drop table if exists o_address cascade; 29 | 30 | drop table if exists be_contact cascade; 31 | 32 | drop table if exists contact_note cascade; 33 | 34 | drop table if exists o_country cascade; 35 | 36 | drop table if exists be_customer cascade; 37 | 38 | drop table if exists o_order cascade; 39 | 40 | drop table if exists o_order_detail cascade; 41 | 42 | drop table if exists o_product cascade; 43 | 44 | -------------------------------------------------------------------------------- /x-postgres-features/src/main/java/org/example/domain/BaseModel.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import java.sql.Timestamp; 4 | 5 | import javax.persistence.Id; 6 | import javax.persistence.MappedSuperclass; 7 | import javax.persistence.Version; 8 | 9 | import com.avaje.ebean.Model; 10 | import com.avaje.ebean.annotation.CreatedTimestamp; 11 | import com.avaje.ebean.annotation.UpdatedTimestamp; 12 | 13 | /** 14 | * Base domain object with Id, version, whenCreated and whenUpdated. 15 | * 16 | *

17 | * Extending Model to enable the 'active record' style. 18 | * 19 | *

20 | * whenCreated and whenUpdated are generally useful for maintaining external search services (like 21 | * elasticsearch) and audit. 22 | */ 23 | @MappedSuperclass 24 | public abstract class BaseModel extends Model { 25 | 26 | @Id 27 | Long id; 28 | 29 | @Version 30 | Long version; 31 | 32 | @CreatedTimestamp 33 | Timestamp whenCreated; 34 | 35 | @UpdatedTimestamp 36 | Timestamp whenUpdated; 37 | 38 | public Long getId() { 39 | return id; 40 | } 41 | 42 | public void setId(Long id) { 43 | this.id = id; 44 | } 45 | 46 | public Long getVersion() { 47 | return version; 48 | } 49 | 50 | public void setVersion(Long version) { 51 | this.version = version; 52 | } 53 | 54 | public Timestamp getWhenCreated() { 55 | return whenCreated; 56 | } 57 | 58 | public void setWhenCreated(Timestamp whenCreated) { 59 | this.whenCreated = whenCreated; 60 | } 61 | 62 | public Timestamp getWhenUpdated() { 63 | return whenUpdated; 64 | } 65 | 66 | public void setWhenUpdated(Timestamp whenUpdated) { 67 | this.whenUpdated = whenUpdated; 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /x-postgres-history/src/main/java/org/example/domain/BaseModel.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.Model; 4 | import com.avaje.ebean.annotation.CreatedTimestamp; 5 | import com.avaje.ebean.annotation.UpdatedTimestamp; 6 | 7 | import javax.persistence.Id; 8 | import javax.persistence.MappedSuperclass; 9 | import javax.persistence.Version; 10 | import java.sql.Timestamp; 11 | 12 | /** 13 | * Base domain object with Id, version, whenCreated and whenUpdated. 14 | * 15 | *

16 | * Extending Model to enable the 'active record' style. 17 | * 18 | *

19 | * whenCreated and whenUpdated are generally useful for maintaining external search services (like 20 | * elasticsearch) and audit. 21 | */ 22 | @MappedSuperclass 23 | public abstract class BaseModel extends Model { 24 | 25 | @Id 26 | protected Long id; 27 | 28 | @Version 29 | protected Long version; 30 | 31 | @CreatedTimestamp 32 | protected Timestamp whenCreated; 33 | 34 | @UpdatedTimestamp 35 | protected Timestamp whenUpdated; 36 | 37 | public Long getId() { 38 | return id; 39 | } 40 | 41 | public void setId(Long 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 | } 70 | -------------------------------------------------------------------------------- /x-dbmigration/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.example 8 | example-dbmigration 9 | 1.1-SNAPSHOT 10 | 11 | 12 | org.avaje 13 | avaje-parent 14 | 2.0 15 | 16 | 17 | 18 | 19 | 20 | org.avaje.ebeanorm 21 | avaje-ebeanorm 22 | 6.3.2-SNAPSHOT 23 | 24 | 25 | 26 | mysql 27 | mysql-connector-java 28 | 5.1.36 29 | test 30 | 31 | 32 | 33 | org.postgresql 34 | postgresql 35 | 9.4-1201-jdbc41 36 | test 37 | 38 | 39 | 40 | oracle 41 | oracle-jdbc 42 | 7.0 43 | test 44 | 45 | 46 | 47 | org.avaje.composite 48 | avaje-composite-testing-ebean 49 | 4.1 50 | pom 51 | test 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /x-postgres-features/src/test/java/org/example/storedprocedure/CallableSqlTest.java: -------------------------------------------------------------------------------- 1 | package org.example.storedprocedure; 2 | 3 | import com.avaje.ebean.CallableSql; 4 | import com.avaje.ebean.Ebean; 5 | import com.avaje.ebean.EbeanServer; 6 | import org.example.ExampleBaseTestCase; 7 | import org.junit.Test; 8 | 9 | import java.sql.Types; 10 | 11 | import static org.assertj.core.api.Assertions.assertThat; 12 | 13 | 14 | public class CallableSqlTest extends ExampleBaseTestCase { 15 | 16 | @Test 17 | public void testCallable() { 18 | 19 | EbeanServer server = Ebean.getDefaultServer(); 20 | 21 | //create or replace function sp_insert (p_name VARCHAR, OUT p_id INTEGER) 22 | CallableSql callableSql = server.createCallableSql("{call sp_insert(?, ?)}"); 23 | callableSql.setParameter(1, "rob name"); 24 | callableSql.registerOut(2, Types.INTEGER); 25 | 26 | server.execute(callableSql); 27 | 28 | Integer value = (Integer)callableSql.getObject(2); 29 | 30 | assertThat(value).isNotNull(); 31 | } 32 | 33 | 34 | @Test 35 | public void testCallable_withInOut() { 36 | 37 | EbeanServer server = Ebean.getDefaultServer(); 38 | 39 | //create or replace function sp_insert (p_name VARCHAR, OUT p_id INTEGER) 40 | CallableSql callableSql = server.createCallableSql("{call sp_insert2(?, ?)}"); 41 | callableSql.setParameter(1, "rob name"); 42 | callableSql.registerOut(1, Types.VARCHAR); 43 | callableSql.registerOut(2, Types.INTEGER); 44 | 45 | server.execute(callableSql); 46 | 47 | String nameOut = (String)callableSql.getObject(1); 48 | Integer intOut = (Integer)callableSql.getObject(2); 49 | 50 | assertThat(nameOut).isNotNull(); 51 | assertThat(intOut).isNotNull(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /x-postgres-features/src/main/java/org/example/domain/Customer.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import java.util.Date; 4 | import java.util.Map; 5 | 6 | import javax.persistence.Column; 7 | import javax.persistence.Entity; 8 | import javax.persistence.Table; 9 | 10 | import com.avaje.ebean.annotation.DbHstore; 11 | 12 | /** 13 | * Customer entity bean. 14 | */ 15 | @Entity 16 | @Table(name="p_customer") 17 | public class Customer extends BaseModel { 18 | 19 | /** 20 | * Convenience Finder for 'active record' style. 21 | */ 22 | public static final Finder find = new Finder<>(Customer.class); 23 | 24 | boolean inactive; 25 | 26 | @Column(length=100) 27 | String name; 28 | 29 | Date registered; 30 | 31 | @Column(length=1000) 32 | String comments; 33 | 34 | @DbHstore 35 | Map tags; 36 | 37 | public String toString() { 38 | return "id:"+id+" name:"+name+" tags:"+tags; 39 | } 40 | 41 | public boolean isInactive() { 42 | return inactive; 43 | } 44 | 45 | public void setInactive(boolean inactive) { 46 | this.inactive = inactive; 47 | } 48 | 49 | public Map getTags() { 50 | return tags; 51 | } 52 | 53 | public void setTags(Map tags) { 54 | this.tags = tags; 55 | } 56 | 57 | public String getName() { 58 | return name; 59 | } 60 | 61 | public void setName(String name) { 62 | this.name = name; 63 | } 64 | 65 | public Date getRegistered() { 66 | return registered; 67 | } 68 | 69 | public void setRegistered(Date registered) { 70 | this.registered = registered; 71 | } 72 | 73 | public String getComments() { 74 | return comments; 75 | } 76 | 77 | public void setComments(String comments) { 78 | this.comments = comments; 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /a-basic/h2-drop-all.sql: -------------------------------------------------------------------------------- 1 | alter table o_address drop constraint if exists fk_o_address_country_code; 2 | drop index if exists ix_o_address_country_code; 3 | 4 | alter table be_contact drop constraint if exists fk_be_contact_customer_id; 5 | drop index if exists ix_be_contact_customer_id; 6 | 7 | alter table contact_note drop constraint if exists fk_contact_note_contact_id; 8 | drop index if exists ix_contact_note_contact_id; 9 | 10 | alter table be_customer drop constraint if exists fk_be_customer_billing_address_id; 11 | drop index if exists ix_be_customer_billing_address_id; 12 | 13 | alter table be_customer drop constraint if exists fk_be_customer_shipping_address_id; 14 | drop index if exists ix_be_customer_shipping_address_id; 15 | 16 | alter table o_order drop constraint if exists fk_o_order_customer_id; 17 | drop index if exists ix_o_order_customer_id; 18 | 19 | alter table o_order drop constraint if exists fk_o_order_shipping_address_id; 20 | drop index if exists ix_o_order_shipping_address_id; 21 | 22 | alter table o_order_detail drop constraint if exists fk_o_order_detail_order_id; 23 | drop index if exists ix_o_order_detail_order_id; 24 | 25 | alter table o_order_detail drop constraint if exists fk_o_order_detail_product_id; 26 | drop index if exists ix_o_order_detail_product_id; 27 | 28 | drop table if exists o_address; 29 | drop sequence if exists o_address_seq; 30 | 31 | drop table if exists be_contact; 32 | drop sequence if exists be_contact_seq; 33 | 34 | drop table if exists contact_note; 35 | drop sequence if exists contact_note_seq; 36 | 37 | drop table if exists o_country; 38 | 39 | drop table if exists be_customer; 40 | drop sequence if exists be_customer_seq; 41 | 42 | drop table if exists o_order; 43 | drop sequence if exists o_order_seq; 44 | 45 | drop table if exists o_order_detail; 46 | drop sequence if exists o_order_detail_seq; 47 | 48 | drop table if exists o_product; 49 | drop sequence if exists o_product_seq; 50 | 51 | -------------------------------------------------------------------------------- /x-dbmigration/src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | TRACE 5 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | log/ebean.log 13 | 14 | TRACE 15 | 16 | 17 | log/application.log.%d{yyyy-MM-dd} 18 | 90 19 | 20 | 21 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /x-postgres-history/src/main/java/org/example/domain/Contact.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.annotation.History; 4 | import org.example.domain.finder.ContactFinder; 5 | 6 | import javax.persistence.Column; 7 | import javax.persistence.Entity; 8 | import javax.persistence.ManyToOne; 9 | import javax.persistence.Table; 10 | 11 | /** 12 | * Contact entity bean. 13 | */ 14 | @History 15 | @Entity 16 | @Table(name="contact") 17 | public class Contact extends BaseModel { 18 | 19 | /** 20 | * Convenience Finder for 'active record' style. 21 | */ 22 | public static final ContactFinder find = new ContactFinder(); 23 | 24 | @ManyToOne(optional=false) 25 | Customer customer; 26 | 27 | @Column(length=50) 28 | String firstName; 29 | 30 | @Column(length=50) 31 | String lastName; 32 | 33 | @Column(length=200) 34 | String email; 35 | 36 | @Column(length=20) 37 | String phone; 38 | 39 | /** 40 | * Default constructor. 41 | */ 42 | public Contact() { 43 | } 44 | 45 | /** 46 | * Construct with a firstName and lastName. 47 | */ 48 | public Contact(String firstName, String lastName) { 49 | this.firstName = firstName; 50 | this.lastName = lastName; 51 | } 52 | 53 | public String getFirstName() { 54 | return firstName; 55 | } 56 | 57 | public void setFirstName(String firstName) { 58 | this.firstName = firstName; 59 | } 60 | 61 | public String getLastName() { 62 | return lastName; 63 | } 64 | 65 | public void setLastName(String lastName) { 66 | this.lastName = lastName; 67 | } 68 | 69 | public String getEmail() { 70 | return email; 71 | } 72 | 73 | public void setEmail(String email) { 74 | this.email = email; 75 | } 76 | 77 | public String getPhone() { 78 | return phone; 79 | } 80 | 81 | public void setPhone(String phone) { 82 | this.phone = phone; 83 | } 84 | 85 | public Customer getCustomer() { 86 | return customer; 87 | } 88 | 89 | public void setCustomer(Customer customer) { 90 | this.customer = customer; 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /e-groovy/src/test/groovy/org/example/service/ExampleUsingPathProperties.groovy: -------------------------------------------------------------------------------- 1 | package org.example.service 2 | 3 | import com.avaje.ebean.Ebean 4 | import com.avaje.ebean.Query 5 | import com.avaje.ebean.text.PathProperties 6 | import com.avaje.ebean.text.json.JsonContext 7 | import groovy.transform.CompileStatic 8 | import org.example.domain.Contact 9 | import org.example.domain.Customer 10 | import org.example.domain.Order 11 | import org.junit.Test 12 | 13 | /** 14 | * 15 | * @author: Richard Vowles - https://plus.google.com/+RichardVowles 16 | */ 17 | @CompileStatic 18 | class ExampleUsingPathProperties extends LoadAgentAtRuntime { 19 | public static void main(String[] args) { 20 | 21 | // new ExampleUsingPathProperties().runExample() 22 | new ExampleUsingPathProperties().runExampleUsingOrders() 23 | } 24 | 25 | @Test 26 | public void runExample() { 27 | LoadExampleData.load() 28 | 29 | List list2 = Customer.find 30 | .select("name") 31 | .fetch("contacts") 32 | .findList() 33 | 34 | 35 | PathProperties pathProperties = PathProperties.parse("(id,version,name,contacts(id,email,version))") 36 | 37 | 38 | Query query = Customer.find.query() 39 | pathProperties.apply(query) 40 | 41 | List list = query.findList() 42 | 43 | JsonContext jsonContext = Customer.find.db().createJsonContext() 44 | 45 | println jsonContext.toJsonString(list, true) 46 | 47 | 48 | for (Customer customer : list2) { 49 | customer.name // ?? 50 | 51 | customer.contacts?.each { Contact contact -> 52 | println "contact: ${contact.firstName} ${contact.lastName}" 53 | } 54 | } 55 | } 56 | 57 | @Test 58 | public void runExampleUsingOrders() { 59 | 60 | LoadExampleData.load() 61 | 62 | PathProperties pathProperties = PathProperties.parse("(id,status,orderDate,customer(id,name),details(*,product(sku)))") 63 | 64 | Query query = Ebean.createQuery(Order) 65 | pathProperties.apply(query) 66 | 67 | List orders = query.where().gt("id", 1).findList() 68 | 69 | println Ebean.createJsonContext().toJsonString(orders, true) 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/test/java/org/example/domain/ExampleUsingPathProperties.kt: -------------------------------------------------------------------------------- 1 | package org.example.domain 2 | 3 | import org.example.service.LoadAgentAtRuntime 4 | 5 | public class ExampleUsingPathProperties : LoadAgentAtRuntime() { 6 | 7 | // public static void main(String[] args) { 8 | // 9 | //// new ExampleUsingPathProperties().runExample(); 10 | // new ExampleUsingPathProperties().runExampleUsingOrders(); 11 | // } 12 | // 13 | // public void runExample() { 14 | // 15 | // 16 | // 17 | // LoadExampleData.load(); 18 | // 19 | // List list2 = Customer.find 20 | // .select("name") 21 | // .fetch("contacts") 22 | // .findList(); 23 | // 24 | // 25 | // PathProperties pathProperties = PathProperties.parse("(id,version,name,contacts(id,email,version))"); 26 | // 27 | // 28 | // Query query = Customer.find.query(); 29 | // pathProperties.apply(query); 30 | // 31 | // List list = query.findList(); 32 | // 33 | // JsonContext jsonContext = Customer.find.db().createJsonContext(); 34 | // 35 | // String jsonString = jsonContext.toJsonString(list, true); 36 | // System.out.println(jsonString); 37 | // 38 | // 39 | // for (Customer customer : list2) { 40 | // customer.getName(); 41 | // List contacts = customer.getContacts(); 42 | // if (contacts != null) { 43 | // for (Contact contact : contacts) { 44 | // System.out.println("contact: "+contact.getFirstName()+" "+contact.getLastName()); 45 | // } 46 | // } 47 | // 48 | // } 49 | // } 50 | // 51 | // public void runExampleUsingOrders() { 52 | // 53 | // LoadExampleData.load(); 54 | // 55 | // PathProperties pathProperties = PathProperties.parse("(id,status,orderDate,customer(id,name),details(*,product(sku)))"); 56 | // 57 | // Query query = Ebean.createQuery(Order.class); 58 | // pathProperties.apply(query); 59 | // 60 | // List orders = query.where().gt("id", 1).findList(); 61 | // 62 | // String rawJson = Ebean.createJsonContext().toJsonString(orders, true); 63 | // System.out.println(rawJson); 64 | // } 65 | 66 | } -------------------------------------------------------------------------------- /x-postgres-history/src/main/java/org/example/domain/excludem2m/ContactExcludeM2M.java: -------------------------------------------------------------------------------- 1 | package org.example.domain.excludem2m; 2 | 3 | import com.avaje.ebean.annotation.History; 4 | import org.example.domain.BaseModel; 5 | import org.example.domain.finder.ContactFinder; 6 | 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.ManyToOne; 10 | import javax.persistence.Table; 11 | 12 | /** 13 | * Contact entity bean. 14 | */ 15 | @History 16 | @Entity 17 | @Table(name="contact") 18 | public class ContactExcludeM2M extends BaseModel { 19 | 20 | /** 21 | * Convenience Finder for 'active record' style. 22 | */ 23 | public static final ContactFinder find = new ContactFinder(); 24 | 25 | @ManyToOne(optional=false) 26 | ContactExcludeM2M customer; 27 | 28 | @Column(length=50) 29 | String firstName; 30 | 31 | @Column(length=50) 32 | String lastName; 33 | 34 | @Column(length=200) 35 | String email; 36 | 37 | @Column(length=20) 38 | String phone; 39 | 40 | /** 41 | * Default constructor. 42 | */ 43 | public ContactExcludeM2M() { 44 | } 45 | 46 | /** 47 | * Construct with a firstName and lastName. 48 | */ 49 | public ContactExcludeM2M(String firstName, String lastName) { 50 | this.firstName = firstName; 51 | this.lastName = lastName; 52 | } 53 | 54 | public String getFirstName() { 55 | return firstName; 56 | } 57 | 58 | public void setFirstName(String firstName) { 59 | this.firstName = firstName; 60 | } 61 | 62 | public String getLastName() { 63 | return lastName; 64 | } 65 | 66 | public void setLastName(String lastName) { 67 | this.lastName = lastName; 68 | } 69 | 70 | public String getEmail() { 71 | return email; 72 | } 73 | 74 | public void setEmail(String email) { 75 | this.email = email; 76 | } 77 | 78 | public String getPhone() { 79 | return phone; 80 | } 81 | 82 | public void setPhone(String phone) { 83 | this.phone = phone; 84 | } 85 | 86 | public ContactExcludeM2M getCustomer() { 87 | return customer; 88 | } 89 | 90 | public void setCustomer(ContactExcludeM2M customer) { 91 | this.customer = customer; 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /a-basic/src/test/java/org/example/domain/ExFindIterate.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | 4 | import com.avaje.ebean.ExpressionList; 5 | import com.avaje.ebean.QueryEachConsumer; 6 | import org.example.ExampleBaseTestCase; 7 | import org.example.domain.query.QContact; 8 | import org.example.domain.query.QCustomer; 9 | import org.example.domain.query.QOrder; 10 | import org.example.domain.query.QOrderDetail; 11 | import org.example.service.LoadExampleData; 12 | import org.junit.Test; 13 | 14 | import java.sql.Date; 15 | import java.util.List; 16 | 17 | public class ExFindIterate extends ExampleBaseTestCase { 18 | 19 | @Test 20 | public void simple() { 21 | 22 | LoadExampleData.load(); 23 | 24 | Date oneWeekAgo = new Date(System.currentTimeMillis()); 25 | 26 | ExpressionList recentNewOrders = new QOrder() 27 | .orderDate.after(oneWeekAgo) 28 | .status.eq(Order.Status.NEW) 29 | .getExpressionList(); 30 | 31 | QCustomer cus = QCustomer.alias(); 32 | QContact con = QContact.alias(); 33 | 34 | List customers = new QCustomer() 35 | // query tuning 36 | .select(cus.name, cus.inactive) 37 | .contacts.fetch(con.email, con.firstName) 38 | .contacts.notes.fetchAll() 39 | 40 | // predicates 41 | .name.ilike("Rob") 42 | .orders.filterMany(recentNewOrders) 43 | .findList(); 44 | 45 | 46 | // ExpressionList detailsFilter = new QOrderDetail() 47 | // .unitPrice.isNotNull() 48 | // .getExpressionList(); 49 | // 50 | // Customer cust = Customer.find.ref(1L); 51 | // 52 | // new QOrder() 53 | // .status.in(Order.Status.APPROVED, Order.Status.COMPLETE) 54 | // .customer.equalTo(cust) 55 | // .details.filterMany(detailsFilter) 56 | // .findList(); 57 | 58 | // new QOrder() 59 | // .id.greaterThan(1) 60 | // .findEach(new QueryEachConsumer() { 61 | // @Override 62 | // public void accept(Order order) { 63 | // Customer customer = order.getCustomer(); 64 | // List contacts = customer.getContacts(); 65 | // System.out.println(contacts); 66 | // } 67 | // }); 68 | 69 | } 70 | 71 | } 72 | 73 | -------------------------------------------------------------------------------- /a-basic/src/main/java/org/example/domain/Contact.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import org.example.domain.finder.ContactFinder; 4 | 5 | import javax.persistence.*; 6 | import java.util.List; 7 | 8 | /** 9 | * Contact entity bean. 10 | */ 11 | @Entity 12 | @Table(name="be_contact") 13 | public class Contact extends BaseModel { 14 | 15 | /** 16 | * Convenience Finder for 'active record' style. 17 | */ 18 | public static final ContactFinder find = new ContactFinder(); 19 | 20 | @Column(length=50) 21 | String firstName; 22 | 23 | @Column(length=50) 24 | String lastName; 25 | 26 | @Column(length=200) 27 | String email; 28 | 29 | @Column(length=20) 30 | String phone; 31 | 32 | @ManyToOne(optional=false) 33 | Customer customer; 34 | 35 | @OneToMany(mappedBy = "contact") 36 | List notes; 37 | 38 | /** 39 | * Default constructor. 40 | */ 41 | public Contact() { 42 | } 43 | 44 | /** 45 | * Construct with a firstName and lastName. 46 | */ 47 | public Contact(String firstName, String lastName) { 48 | this.firstName = firstName; 49 | this.lastName = lastName; 50 | } 51 | 52 | public String getFirstName() { 53 | return firstName; 54 | } 55 | 56 | public void setFirstName(String firstName) { 57 | this.firstName = firstName; 58 | } 59 | 60 | public String getLastName() { 61 | return lastName; 62 | } 63 | 64 | public void setLastName(String lastName) { 65 | this.lastName = lastName; 66 | } 67 | 68 | public String getEmail() { 69 | return email; 70 | } 71 | 72 | public void setEmail(String email) { 73 | this.email = email; 74 | } 75 | 76 | public String getPhone() { 77 | return phone; 78 | } 79 | 80 | public void setPhone(String phone) { 81 | this.phone = phone; 82 | } 83 | 84 | public Customer getCustomer() { 85 | return customer; 86 | } 87 | 88 | public void setCustomer(Customer customer) { 89 | this.customer = customer; 90 | } 91 | 92 | public List getNotes() { 93 | return notes; 94 | } 95 | 96 | public void setNotes(List notes) { 97 | this.notes = notes; 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /e-eventadapters/src/main/java/org/example/domain/AbstractModel.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import java.io.Serializable; 4 | import java.util.Date; 5 | import java.util.UUID; 6 | 7 | import javax.persistence.Column; 8 | import javax.persistence.Id; 9 | import javax.persistence.MappedSuperclass; 10 | 11 | import com.avaje.ebean.Model; 12 | import com.avaje.ebean.annotation.CreatedTimestamp; 13 | import com.avaje.ebean.annotation.UpdatedTimestamp; 14 | 15 | /** 16 | * 17 | */ 18 | @MappedSuperclass 19 | public abstract class AbstractModel extends Model implements Serializable { 20 | 21 | private static final long serialVersionUID = 1L; 22 | 23 | @Id 24 | protected UUID id; 25 | 26 | @Column(insertable = true, updatable = false) 27 | protected String creatorId; 28 | 29 | @Column(insertable = true, updatable = false) 30 | protected String creatorName; 31 | 32 | @CreatedTimestamp 33 | protected Date createTime; 34 | 35 | protected String modifierId; 36 | 37 | protected String modifierName; 38 | 39 | @UpdatedTimestamp 40 | protected Date modifyTime; 41 | 42 | public UUID getId() { 43 | return id; 44 | } 45 | 46 | public void setId(UUID id) { 47 | this.id = id; 48 | } 49 | 50 | public String getCreatorId() { 51 | return creatorId; 52 | } 53 | 54 | public void setCreatorId(String creatorId) { 55 | this.creatorId = creatorId; 56 | } 57 | 58 | public String getCreatorName() { 59 | return creatorName; 60 | } 61 | 62 | public void setCreatorName(String creatorName) { 63 | this.creatorName = creatorName; 64 | } 65 | 66 | public Date getCreateTime() { 67 | return createTime; 68 | } 69 | 70 | public void setCreateTime(Date createTime) { 71 | this.createTime = createTime; 72 | } 73 | 74 | public String getModifierId() { 75 | return modifierId; 76 | } 77 | 78 | public void setModifierId(String modifierId) { 79 | this.modifierId = modifierId; 80 | } 81 | 82 | public String getModifierName() { 83 | return modifierName; 84 | } 85 | 86 | public void setModifierName(String modifierName) { 87 | this.modifierName = modifierName; 88 | } 89 | 90 | public Date getModifyTime() { 91 | return modifyTime; 92 | } 93 | 94 | public void setModifyTime(Date modifyTime) { 95 | this.modifyTime = modifyTime; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /a-basic/src/main/java/org/example/service/ExampleUsingPathProperties.java: -------------------------------------------------------------------------------- 1 | package org.example.service; 2 | 3 | import java.util.List; 4 | 5 | import org.example.domain.Contact; 6 | import org.example.domain.Customer; 7 | import org.example.domain.Order; 8 | 9 | import com.avaje.ebean.Ebean; 10 | import com.avaje.ebean.Query; 11 | import com.avaje.ebean.text.PathProperties; 12 | import com.avaje.ebean.text.json.JsonContext; 13 | 14 | public class ExampleUsingPathProperties extends LoadAgentAtRuntime { 15 | 16 | public static void main(String[] args) { 17 | 18 | // new ExampleUsingPathProperties().runExample(); 19 | new ExampleUsingPathProperties().runExampleUsingOrders(); 20 | } 21 | 22 | public void runExample() { 23 | 24 | 25 | 26 | LoadExampleData.load(); 27 | 28 | List list2 = Customer.find 29 | .query() 30 | .select("name") 31 | .fetch("contacts") 32 | .findList(); 33 | 34 | 35 | PathProperties pathProperties = PathProperties.parse("(id,version,name,contacts(id,email,version))"); 36 | 37 | 38 | Query query = Customer.find.query(); 39 | pathProperties.apply(query); 40 | 41 | List list = query.findList(); 42 | 43 | JsonContext jsonContext = Customer.find.db().json(); 44 | 45 | String jsonString = jsonContext.toJson(list); 46 | System.out.println(jsonString); 47 | 48 | 49 | for (Customer customer : list2) { 50 | customer.getName(); 51 | List contacts = customer.getContacts(); 52 | if (contacts != null) { 53 | for (Contact contact : contacts) { 54 | System.out.println("contact: "+contact.getFirstName()+" "+contact.getLastName()); 55 | } 56 | } 57 | 58 | } 59 | } 60 | 61 | public void runExampleUsingOrders() { 62 | 63 | LoadExampleData.load(); 64 | 65 | PathProperties pathProperties = PathProperties.parse("(id,status,orderDate,customer(id,name),details(*,product(sku)))"); 66 | 67 | Query query = Ebean.createQuery(Order.class); 68 | pathProperties.apply(query); 69 | 70 | List orders = query.where().gt("id", 1).findList(); 71 | 72 | String rawJson = Ebean.json().toJson(orders); 73 | System.out.println(rawJson); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /a-basic/src/main/java/org/example/domain/OrderDetail.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import org.example.domain.finder.OrderDetailFinder; 4 | 5 | import javax.persistence.Entity; 6 | import javax.persistence.ManyToOne; 7 | import javax.persistence.Table; 8 | 9 | /** 10 | * Order Detail entity bean. 11 | */ 12 | @Entity 13 | @Table(name = "o_order_detail") 14 | public class OrderDetail extends BaseModel { 15 | 16 | /** 17 | * Convenience Finder for 'active record' style. 18 | */ 19 | public static final OrderDetailFinder find = new OrderDetailFinder(); 20 | 21 | @ManyToOne 22 | Order order; 23 | 24 | Integer orderQty; 25 | 26 | Integer shipQty; 27 | 28 | Double unitPrice; 29 | 30 | @ManyToOne 31 | Product product; 32 | 33 | public OrderDetail() { 34 | } 35 | 36 | public OrderDetail(Product product, Integer orderQty, Double unitPrice) { 37 | this.product = product; 38 | this.orderQty = orderQty; 39 | this.unitPrice = unitPrice; 40 | } 41 | 42 | /** 43 | * Return order qty. 44 | */ 45 | public Integer getOrderQty() { 46 | return orderQty; 47 | } 48 | 49 | /** 50 | * Set order qty. 51 | */ 52 | public void setOrderQty(Integer orderQty) { 53 | this.orderQty = orderQty; 54 | } 55 | 56 | /** 57 | * Return ship qty. 58 | */ 59 | public Integer getShipQty() { 60 | return shipQty; 61 | } 62 | 63 | /** 64 | * Set ship qty. 65 | */ 66 | public void setShipQty(Integer shipQty) { 67 | this.shipQty = shipQty; 68 | } 69 | 70 | public Double getUnitPrice() { 71 | return unitPrice; 72 | } 73 | 74 | public void setUnitPrice(Double unitPrice) { 75 | this.unitPrice = unitPrice; 76 | } 77 | 78 | /** 79 | * Return order. 80 | */ 81 | public Order getOrder() { 82 | return order; 83 | } 84 | 85 | /** 86 | * Set order. 87 | */ 88 | public void setOrder(Order order) { 89 | this.order = order; 90 | } 91 | 92 | /** 93 | * Return product. 94 | */ 95 | public Product getProduct() { 96 | return product; 97 | } 98 | 99 | /** 100 | * Set product. 101 | */ 102 | public void setProduct(Product product) { 103 | this.product = product; 104 | } 105 | 106 | } 107 | -------------------------------------------------------------------------------- /a-basic/ebean-autotune.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | org.example.domain.finder.OrderFinder.byStatus(OrderFinder.java:40) 8 | org.example.domain.finder.OrderFinderTest.testOther(OrderFinderTest.java:61) 9 | sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 10 | sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 11 | sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 12 | 13 | 14 | 17 | org.example.domain.finder.OrderFinder.byStatus(OrderFinder.java:40) 18 | org.example.domain.finder.OrderFinderTest.test(OrderFinderTest.java:29) 19 | sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 20 | sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 21 | sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 22 | 23 | 24 | 27 | org.example.domain.finder.OrderFinderTest.testOrdersSince(OrderFinderTest.java:87) 28 | sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 29 | sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 30 | sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 31 | java.lang.reflect.Method.invoke(Method.java:497) 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /e-groovy/src/main/groovy/org/avaje/ebean/groovy/EbeanExtensionModule.groovy: -------------------------------------------------------------------------------- 1 | package org.avaje.ebean.groovy 2 | 3 | import com.avaje.ebean.EbeanServer 4 | import com.avaje.ebean.ExpressionList 5 | import com.avaje.ebean.QueryIterator 6 | import com.avaje.ebean.TxRunnable 7 | import groovy.transform.CompileStatic 8 | 9 | /** 10 | * This is just to demonstrate extensions on static classes. You cannot use this in the classes in the same 11 | * project, separate it into a separate artifact if you are wanting to use these features (github.com/uoa-group-applications/common-ebean) 12 | * 13 | * @author: Richard Vowles - https://plus.google.com/+RichardVowles 14 | */ 15 | @CompileStatic 16 | class EbeanExtensionModule { 17 | /** 18 | * As an iterator is a resource and *must* be closed, we use this mechanism to allow for a clean 19 | * cleaning up 20 | * 21 | * @param expressionList 22 | * @param iteratorClosure 23 | */ 24 | public static eachEntity(ExpressionList expressionList, Closure iteratorClosure) { 25 | QueryIterator queryIterator = expressionList.findIterate() 26 | 27 | try { 28 | while (queryIterator.hasNext()) { 29 | iteratorClosure.call(queryIterator.next()) 30 | } 31 | } finally { 32 | if (queryIterator != null) { 33 | queryIterator.close() 34 | } 35 | } 36 | } 37 | 38 | /** 39 | * This kind of iterator expects the closure will return a boolean value, that if false, will cause the iterator 40 | * to exit. 41 | * 42 | * @param expressionList 43 | * @param iteratorClosure 44 | */ 45 | public static eachEntityBoolean(ExpressionList expressionList, Closure iteratorClosure) { 46 | QueryIterator queryIterator = expressionList.findIterate() 47 | 48 | boolean keepGoing = true 49 | 50 | try { 51 | while (keepGoing && queryIterator.hasNext()) { 52 | keepGoing = iteratorClosure.call(queryIterator.next()) 53 | } 54 | } finally { 55 | if (queryIterator != null) { 56 | queryIterator.close() 57 | } 58 | } 59 | } 60 | 61 | /** 62 | * This is for demonstration only. You may not use txRunnables. 63 | * 64 | * @param server 65 | * @param txRunnable 66 | * @return 67 | */ 68 | public static run(EbeanServer server, Closure txRunnable) { 69 | server.execute(new TxRunnable() { 70 | @Override 71 | void run() { 72 | txRunnable.call() 73 | } 74 | }) 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /e-groovy/src/test/groovy/org/example/service/LargeQueryWithFindIterateTest.groovy: -------------------------------------------------------------------------------- 1 | package org.example.service 2 | 3 | import com.avaje.ebean.QueryIterator 4 | import com.avaje.ebean.QueryResultVisitor 5 | import org.example.domain.Customer 6 | import org.junit.Test 7 | 8 | public class LargeQueryWithFindIterateTest extends ExampleBaseTestCase { 9 | 10 | @Test 11 | public void testFindIterate() { 12 | 13 | // insert 100 customers 14 | int j = 0 15 | for (int i = 0; i < 100; i++) { 16 | Customer customer = new Customer(name: "Hello"+j++) 17 | customer.save() 18 | } 19 | 20 | // Use the eachEntity extension method 21 | Customer.find.query().select("id").where().eachEntity { 22 | println "got customter ${it.id} " 23 | } 24 | 25 | // Use the eachEntityBoolean extension method 26 | // ... allows you to stop the iteration by returning false 27 | Customer.find.query().select("id").where().eachEntityBoolean { 28 | println "got customter ${it.id} " 29 | return it.id < 10 30 | } 31 | 32 | // Similar to eachEntityBoolean using findVisit() ... 33 | Customer.find 34 | .query().select("id, name") //.fetch("contacts", new FetchConfig().lazy(20)) 35 | .findVisit(new QueryResultVisitor() { 36 | 37 | @Override 38 | boolean accept(Customer customer) { 39 | println "got name ${customer.id} ${customer.name}" 40 | // return false to stop the iteration early ... 41 | return customer.id < 10 42 | } 43 | }) 44 | 45 | // 'Typical' use of QueryIterator which requires it to be closed 46 | // as per the finally block below 47 | QueryIterator iterate = 48 | Customer.find 49 | .query().select("id, name") //.fetch("contacts", new FetchConfig().lazy(20)) 50 | .findIterate() 51 | 52 | try { 53 | 54 | while (iterate.hasNext()) { 55 | Customer customer = iterate.next() 56 | // do something interesting with customer 57 | //customer.getContacts().size() 58 | println "got name ${customer.id} ${customer.name}" 59 | } 60 | 61 | } finally { 62 | // close the underlying resources held by the QueryIterator 63 | // those are: ResultSet and Connection 64 | iterate.close() 65 | } 66 | 67 | 68 | 69 | } 70 | 71 | 72 | } 73 | -------------------------------------------------------------------------------- /a-basic/src/main/java/org/example/domain/Address.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import org.example.domain.finder.AddressFinder; 4 | 5 | import javax.persistence.Entity; 6 | import javax.persistence.ManyToOne; 7 | import javax.persistence.Table; 8 | import javax.validation.constraints.Size; 9 | 10 | /** 11 | * Address entity bean. 12 | */ 13 | @Entity 14 | @Table(name = "o_address") 15 | public class Address extends BaseModel { 16 | 17 | /** 18 | * Convenience Finder for 'active record' style. 19 | */ 20 | public static final AddressFinder find = new AddressFinder(); 21 | 22 | @Size(max = 100) 23 | String line1; 24 | 25 | @Size(max = 100) 26 | String line2; 27 | 28 | @Size(max = 100) 29 | String city; 30 | 31 | @ManyToOne 32 | Country country; 33 | 34 | /** 35 | * Create a copy of the address. Used to provide a 'snapshot' of 36 | * the shippingAddress for a give order. 37 | */ 38 | public Address createCopy() { 39 | Address copy = new Address(); 40 | copy.setLine1(line1); 41 | copy.setLine2(line2); 42 | copy.setCity(city); 43 | copy.setCountry(country); 44 | return copy; 45 | } 46 | 47 | public String toString() { 48 | return id + " " + line1 + " " + line2 + " " + city + " " + country; 49 | } 50 | 51 | /** 52 | * Return line 1. 53 | */ 54 | public String getLine1() { 55 | return line1; 56 | } 57 | 58 | /** 59 | * Set line 1. 60 | */ 61 | public void setLine1(String line1) { 62 | this.line1 = line1; 63 | } 64 | 65 | /** 66 | * Return line 2. 67 | */ 68 | public String getLine2() { 69 | return line2; 70 | } 71 | 72 | /** 73 | * Set line 2. 74 | */ 75 | public void setLine2(String line2) { 76 | this.line2 = line2; 77 | } 78 | 79 | /** 80 | * Return city. 81 | */ 82 | public String getCity() { 83 | return city; 84 | } 85 | 86 | /** 87 | * Set city. 88 | */ 89 | public void setCity(String city) { 90 | this.city = city; 91 | } 92 | 93 | /** 94 | * Return country. 95 | */ 96 | public Country getCountry() { 97 | return country; 98 | } 99 | 100 | /** 101 | * Set country. 102 | */ 103 | public void setCountry(Country country) { 104 | this.country = country; 105 | } 106 | 107 | } 108 | -------------------------------------------------------------------------------- /x-postgres-history/src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | TRACE 5 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | log/ebean.log 13 | 14 | TRACE 15 | 16 | 17 | log/application.log.%d{yyyy-MM-dd} 18 | 90 19 | 20 | 21 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /e-spring/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.exanple 8 | example-spring-ebean 9 | 1.1-SNAPSHOT 10 | 11 | 12 | org.avaje 13 | avaje-javaparent 14 | 2.1 15 | 16 | 17 | 18 | 19 | 20 | org.springframework 21 | spring-core 22 | 4.1.7.RELEASE 23 | 24 | 25 | 26 | org.avaje.ebeanorm 27 | avaje-ebeanorm 28 | [6.1,) 29 | 30 | 31 | 32 | org.slf4j 33 | slf4j-api 34 | 1.7.12 35 | 36 | 37 | 38 | ch.qos.logback 39 | logback-classic 40 | 1.1.3 41 | 42 | 43 | 44 | org.avaje.composite 45 | avaje-composite-testing-ebean 46 | 4.1 47 | pom 48 | test 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.avaje.ebeanorm 58 | avaje-ebeanorm-mavenenhancer 59 | 4.1.2 60 | 61 | 62 | main 63 | process-classes 64 | 65 | target/classes 66 | org.example.domain.** 67 | debug=1 68 | 69 | 70 | enhance 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /x-postgres-history/src/extra-ddl-address.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE o_address ADD COLUMN sys_period tstzrange NOT NULL; 2 | 3 | CREATE TABLE o_address_history (LIKE o_address); 4 | 5 | CREATE VIEW o_address_with_history AS SELECT * FROM o_address UNION ALL SELECT * FROM o_address_history; 6 | 7 | 8 | 9 | 10 | CREATE OR REPLACE FUNCTION o_address_history_version() RETURNS TRIGGER AS $$ 11 | BEGIN 12 | 13 | IF (TG_OP = 'INSERT') THEN 14 | NEW.sys_period = tstzrange(CURRENT_TIMESTAMP,null); 15 | RETURN NEW; 16 | 17 | ELSIF (TG_OP = 'UPDATE') THEN 18 | insert into o_address_history (sys_period, id, line1, line2, city, country_code, version, when_created, when_updated) 19 | VALUES (tstzrange(lower(OLD.sys_period), CURRENT_TIMESTAMP), OLD.id, OLD.line1, OLD.line2, OLD.city, OLD.country_code, OLD.version, OLD.when_created, OLD.when_updated); 20 | NEW.sys_period = tstzrange(CURRENT_TIMESTAMP,null); 21 | RETURN NEW; 22 | 23 | ELSEIF (TG_OP = 'DELETE') THEN 24 | insert into o_address_history (sys_period, id, line1, line2, city, country_code, version, when_created, when_updated) 25 | VALUES (tstzrange(lower(OLD.sys_period), CURRENT_TIMESTAMP), OLD.id, OLD.line1, OLD.line2, OLD.city, OLD.country_code, OLD.version, OLD.when_created, OLD.when_updated); 26 | RETURN OLD; 27 | 28 | END IF; 29 | END; 30 | $$ LANGUAGE plpgsql; 31 | 32 | 33 | CREATE TRIGGER o_address_versioning_upd 34 | BEFORE INSERT OR UPDATE OR DELETE ON o_address 35 | FOR EACH ROW EXECUTE PROCEDURE o_address_history_version(); 36 | 37 | -- 38 | -- END -------------------------- 39 | -- 40 | 41 | insert into country (code, name) VALUES ('NZ', 'New Zealand'); 42 | insert into country (code, name) VALUES ('US', 'United States'); 43 | insert into country (code, name) VALUES ('AU', 'Australia'); 44 | 45 | select * from country; 46 | 47 | insert into o_address (line1, city, country_code, version, when_created, when_updated) 48 | values ('9 Queen St','Auckland','NZ', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP); 49 | 50 | insert into o_address (line1, city, country_code, version, when_created, when_updated) 51 | values ('23 Bee St','Wellington','NZ', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP); 52 | 53 | 54 | update o_address set line1 = '42 Good luck' where id = 1; 55 | update o_address set line2 = 'central' where id = 1; 56 | 57 | 58 | select sys_period, * from o_address; 59 | select sys_period, * from o_address_history; 60 | 61 | delete from address where id = 5; 62 | 63 | 64 | -------------------------------------------------------------------------------- /x-postgres-history/src/main/java/org/example/domain/Address.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.annotation.History; 4 | import org.example.domain.finder.AddressFinder; 5 | 6 | import javax.persistence.Entity; 7 | import javax.persistence.ManyToOne; 8 | import javax.persistence.Table; 9 | import javax.validation.constraints.Size; 10 | 11 | /** 12 | * Address entity bean. 13 | */ 14 | @History 15 | @Entity 16 | @Table(name = "o_address") 17 | public class Address extends BaseModel { 18 | 19 | /** 20 | * Convenience Finder for 'active record' style. 21 | */ 22 | public static final AddressFinder find = new AddressFinder(); 23 | 24 | @Size(max = 100) 25 | String line1; 26 | 27 | @Size(max = 100) 28 | String line2; 29 | 30 | @Size(max = 100) 31 | String city; 32 | 33 | @ManyToOne 34 | Country country; 35 | 36 | /** 37 | * Create a copy of the address. Used to provide a 'snapshot' of 38 | * the shippingAddress for a give order. 39 | */ 40 | public Address createCopy() { 41 | Address copy = new Address(); 42 | copy.setLine1(line1); 43 | copy.setLine2(line2); 44 | copy.setCity(city); 45 | copy.setCountry(country); 46 | return copy; 47 | } 48 | 49 | public String toString() { 50 | return id + " " + line1 + " " + line2 + " " + city + " " + country; 51 | } 52 | 53 | /** 54 | * Return line 1. 55 | */ 56 | public String getLine1() { 57 | return line1; 58 | } 59 | 60 | /** 61 | * Set line 1. 62 | */ 63 | public void setLine1(String line1) { 64 | this.line1 = line1; 65 | } 66 | 67 | /** 68 | * Return line 2. 69 | */ 70 | public String getLine2() { 71 | return line2; 72 | } 73 | 74 | /** 75 | * Set line 2. 76 | */ 77 | public void setLine2(String line2) { 78 | this.line2 = line2; 79 | } 80 | 81 | /** 82 | * Return city. 83 | */ 84 | public String getCity() { 85 | return city; 86 | } 87 | 88 | /** 89 | * Set city. 90 | */ 91 | public void setCity(String city) { 92 | this.city = city; 93 | } 94 | 95 | /** 96 | * Return country. 97 | */ 98 | public Country getCountry() { 99 | return country; 100 | } 101 | 102 | /** 103 | * Set country. 104 | */ 105 | public void setCountry(Country country) { 106 | this.country = country; 107 | } 108 | 109 | } 110 | -------------------------------------------------------------------------------- /x-postgres-history/src/extra-ddl-customer.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE customer ADD COLUMN sys_period tstzrange NOT NULL; 2 | 3 | CREATE TABLE customer_history (LIKE customer); 4 | 5 | CREATE VIEW customer_with_history AS SELECT * FROM customer UNION ALL SELECT * FROM customer_history; 6 | 7 | 8 | CREATE OR REPLACE FUNCTION customer_history_version() RETURNS TRIGGER AS $$ 9 | BEGIN 10 | 11 | IF (TG_OP = 'INSERT') THEN 12 | NEW.sys_period = tstzrange(CURRENT_TIMESTAMP,null); 13 | RETURN NEW; 14 | 15 | ELSIF (TG_OP = 'UPDATE') THEN 16 | insert into customer_history (sys_period, id, inactive, name, comments, billing_address_id, shipping_address_id, version, when_created, when_updated) 17 | VALUES (tstzrange(lower(OLD.sys_period), CURRENT_TIMESTAMP), OLD.id, OLD.inactive, OLD.name, OLD.comments, OLD.billing_address_id, OLD.shipping_address_id, OLD.version, OLD.when_created, OLD.when_updated); 18 | NEW.sys_period = tstzrange(CURRENT_TIMESTAMP,null); 19 | RETURN NEW; 20 | 21 | ELSEIF (TG_OP = 'DELETE') THEN 22 | insert into customer_history (sys_period, id, inactive, name, comments, billing_address_id, shipping_address_id, version, when_created, when_updated) 23 | VALUES (tstzrange(lower(OLD.sys_period), CURRENT_TIMESTAMP), OLD.id, OLD.inactive, OLD.name, OLD.comments, OLD.billing_address_id, OLD.shipping_address_id, OLD.version, OLD.when_created, OLD.when_updated); 24 | RETURN OLD; 25 | 26 | END IF; 27 | END; 28 | $$ LANGUAGE plpgsql; 29 | 30 | 31 | CREATE TRIGGER customer_versioning_upd 32 | BEFORE INSERT OR UPDATE OR DELETE ON customer 33 | FOR EACH ROW EXECUTE PROCEDURE customer_history_version(); 34 | 35 | 36 | 37 | 38 | 39 | select * from customer; 40 | select * from customer_history; 41 | 42 | 43 | insert into customer (name, comments, version, when_created, when_updated) 44 | values ('jack','jack 1',1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP); 45 | 46 | insert into customer (name, comments, version, when_created, when_updated) 47 | values ('jill','jill 1',1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP); 48 | 49 | update customer set comments = 'jack 2' where id = 1; 50 | update customer set comments = 'jack 3' where id = 1; 51 | select sys_period, * from customer_history; 52 | 53 | select sys_period, * from customer; 54 | select sys_period, * from customer_history; 55 | 56 | select sys_period, * from o_address; 57 | update customer set billing_address_id = 1 where id = 1; 58 | 59 | delete from customer where id = 5; 60 | 61 | 62 | -------------------------------------------------------------------------------- /x-postgres-history/src/extra-ddl-customer_feature.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE customer_feature ADD COLUMN sys_period tstzrange NOT NULL; 2 | 3 | CREATE TABLE customer_feature_history (LIKE customer_feature); 4 | 5 | CREATE VIEW customer_feature_with_history AS SELECT * FROM customer_feature UNION ALL SELECT * FROM customer_feature_history; 6 | 7 | 8 | 9 | 10 | CREATE OR REPLACE FUNCTION customer_feature_history_version() RETURNS TRIGGER AS $$ 11 | BEGIN 12 | 13 | IF (TG_OP = 'INSERT') THEN 14 | NEW.sys_period = tstzrange(CURRENT_TIMESTAMP,null); 15 | RETURN NEW; 16 | 17 | ELSIF (TG_OP = 'UPDATE') THEN 18 | insert into customer_feature_history (sys_period, customer_id, feature_id) 19 | VALUES (tstzrange(lower(OLD.sys_period), CURRENT_TIMESTAMP), OLD.customer_id, OLD.feature_id); 20 | NEW.sys_period = tstzrange(CURRENT_TIMESTAMP,null); 21 | RETURN NEW; 22 | 23 | ELSEIF (TG_OP = 'DELETE') THEN 24 | insert into customer_feature_history (sys_period, customer_id, feature_id) 25 | VALUES (tstzrange(lower(OLD.sys_period), CURRENT_TIMESTAMP), OLD.customer_id, OLD.feature_id); 26 | RETURN OLD; 27 | 28 | END IF; 29 | END; 30 | $$ LANGUAGE plpgsql; 31 | 32 | 33 | CREATE TRIGGER customer_feature_versioning_upd 34 | BEFORE INSERT OR UPDATE OR DELETE ON customer_feature 35 | FOR EACH ROW EXECUTE PROCEDURE customer_feature_history_version(); 36 | 37 | -- 38 | -- END -------------------------- 39 | -- 40 | 41 | select * from feature; 42 | 43 | insert into feature (name, version, when_created, when_updated) VALUES ('green', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP); 44 | insert into feature (name, version, when_created, when_updated) VALUES ('red', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP); 45 | insert into feature (name, version, when_created, when_updated) VALUES ('blue', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP); 46 | 47 | select * from customer_feature; 48 | select * from customer_feature_history; 49 | select * from customer_feature_with_history; 50 | 51 | insert into o_address (line1, city, country_code, version, when_created, when_updated) 52 | values ('9 Queen St','Auckland','NZ', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP); 53 | 54 | insert into o_address (line1, city, country_code, version, when_created, when_updated) 55 | values ('23 Bee St','Wellington','NZ', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP); 56 | 57 | 58 | update o_address set line1 = '42 Good luck' where id = 1; 59 | update o_address set line2 = 'central' where id = 1; 60 | 61 | 62 | select sys_period, * from o_address; 63 | select sys_period, * from o_address_history; 64 | 65 | delete from address where id = 5; 66 | 67 | 68 | -------------------------------------------------------------------------------- /x-postgres-features/src/test/java/org/example/SimpleDocWithJsonNodeTest.java: -------------------------------------------------------------------------------- 1 | package org.example; 2 | 3 | import com.avaje.ebean.Ebean; 4 | import com.fasterxml.jackson.databind.JsonNode; 5 | import com.fasterxml.jackson.databind.ObjectMapper; 6 | import org.example.domain.SimpleDocUsingJsonNode; 7 | import org.junit.Test; 8 | 9 | import java.io.IOException; 10 | 11 | import static org.junit.Assert.assertEquals; 12 | import static org.junit.Assert.assertNull; 13 | 14 | public class SimpleDocWithJsonNodeTest extends ExampleBaseTestCase { 15 | 16 | @Test 17 | public void test() throws IOException { 18 | 19 | ObjectMapper mapper = new ObjectMapper(); 20 | 21 | String rawJson = "{\"docName\":\"rob doc\", \"docScore\":234}"; 22 | 23 | JsonNode jsonNode = mapper.readTree(rawJson); 24 | 25 | SimpleDocUsingJsonNode doc = new SimpleDocUsingJsonNode(); 26 | doc.setName("doc1"); 27 | doc.setContent(jsonNode); 28 | 29 | doc.save(); 30 | 31 | 32 | String fullJson = Ebean.json().toJson(doc); 33 | 34 | SimpleDocUsingJsonNode doc2 = Ebean.json().toBean(SimpleDocUsingJsonNode.class, fullJson); 35 | 36 | assertEquals(doc.getId(), doc2.getId()); 37 | assertEquals(doc.getName(), doc2.getName()); 38 | assertEquals(doc.getVersion(), doc2.getVersion()); 39 | assertEquals(doc.getContent().toString(), doc2.getContent().toString()); 40 | 41 | SimpleDocUsingJsonNode doc3 = Ebean.find(SimpleDocUsingJsonNode.class, doc.getId()); 42 | assertEquals(doc.getId(), doc3.getId()); 43 | assertEquals(doc.getName(), doc3.getName()); 44 | assertEquals(doc.getVersion(), doc3.getVersion()); 45 | assertEquals(doc.getContent().toString(), doc3.getContent().toString()); 46 | 47 | } 48 | 49 | @Test 50 | public void testNullValue() throws IOException { 51 | 52 | 53 | SimpleDocUsingJsonNode doc = new SimpleDocUsingJsonNode(); 54 | doc.setName("doc1"); 55 | 56 | Ebean.save(doc); 57 | 58 | 59 | String fullJson = Ebean.json().toJson(doc); 60 | 61 | SimpleDocUsingJsonNode doc2 = Ebean.json().toBean(SimpleDocUsingJsonNode.class, fullJson); 62 | 63 | assertEquals(doc.getId(), doc2.getId()); 64 | assertEquals(doc.getName(), doc2.getName()); 65 | assertEquals(doc.getVersion(), doc2.getVersion()); 66 | assertNull(doc2.getContent()); 67 | 68 | 69 | SimpleDocUsingJsonNode doc3 = SimpleDocUsingJsonNode.find.byId(doc.getId()); 70 | assertEquals(doc.getId(), doc3.getId()); 71 | assertEquals(doc.getName(), doc3.getName()); 72 | assertEquals(doc.getVersion(), doc3.getVersion()); 73 | assertNull(doc3.getContent()); 74 | 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/test/java/org/example/domain/LargeQueryWithFindIterateTest.kt: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.Ebean 4 | import org.avaje.agentloader.AgentLoader 5 | import org.example.ExampleBaseTestCase 6 | import org.junit.Test 7 | 8 | public class LargeQueryWithFindIterateTest : ExampleBaseTestCase() { 9 | 10 | init { 11 | // load the enhancement agent 'early' prior to the bean classes like Customer being loaded 12 | AgentLoader.loadAgentFromClasspath("avaje-ebeanorm-agent", "debug=1;packages=org.example.**") 13 | } 14 | 15 | @Test 16 | fun testFindIterate() { 17 | 18 | // insert some customers 19 | Ebean.execute { 20 | for (i in 0..10) { 21 | val customer = Customer(); 22 | customer.name = "Hello" + i; 23 | customer.save(); 24 | } 25 | } 26 | 27 | 28 | // The following queries show how to process very large resultSets a bean at a time. 29 | // Internally Ebean uses a persistence context per per bean (and associated object graphs) 30 | // unlike findList() which holds the entire list of beans in memory before giving the list 31 | // to the caller for processing. 32 | 33 | Customer 34 | .select("id, name") 35 | .where() 36 | .findEach({ 37 | val id = it?.id 38 | val name = it?.name 39 | System.out.println(" using findEach extension method - $id $name") 40 | }) 41 | 42 | Customer 43 | .select("id, name") 44 | .where() 45 | .findEachWhile { 46 | System.out.println(" got ... ${it.id} ${it.name}") 47 | // stop iterating through the results if id > 5 48 | 49 | it?.comments!!.length > 5 50 | //it.contacts.size() > 5; 51 | //var t :Long? = 42 52 | //t!!.toLong() > 4L 53 | it.id!!.toLong() > 5L 54 | } 55 | 56 | 57 | // Perform the query using findIterate (Java7 style) ... 58 | // Note that the iterator MUST be closed (in the finally block below) or there 59 | // will be leaked jdbc resources 60 | 61 | val iterate = 62 | Customer 63 | .query().select("id, name") 64 | .findIterate(); 65 | 66 | try { 67 | 68 | while (iterate.hasNext()) { 69 | val customer = iterate.next(); 70 | // do something interesting with customer 71 | System.out.println("iterator got name ${customer.id} ${customer.name}"); 72 | } 73 | 74 | } finally { 75 | // close the underlying resources held by the QueryIterator 76 | // those are: ResultSet and Connection 77 | iterate.close(); 78 | } 79 | 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /x-postgres-history/src/main/java/org/example/domain/Customer.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import com.avaje.ebean.annotation.History; 4 | import org.example.domain.finder.CustomerFinder; 5 | 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Entity; 8 | import javax.persistence.ManyToMany; 9 | import javax.persistence.ManyToOne; 10 | import javax.persistence.OneToMany; 11 | import javax.persistence.Table; 12 | import javax.validation.constraints.Size; 13 | import java.time.LocalDate; 14 | import java.util.List; 15 | 16 | /** 17 | * Customer entity bean. 18 | */ 19 | @History 20 | @Entity 21 | @Table(name="customer") 22 | public class Customer extends BaseModel { 23 | 24 | /** 25 | * Convenience Finder for 'active record' style. 26 | */ 27 | public static final CustomerFinder find = new CustomerFinder(); 28 | 29 | @Size(max = 100) 30 | String name; 31 | 32 | LocalDate registered; 33 | 34 | @Size(max = 1000) 35 | String comments; 36 | 37 | @ManyToOne(cascade=CascadeType.ALL) 38 | Address billingAddress; 39 | 40 | @ManyToOne(cascade=CascadeType.ALL) 41 | Address shippingAddress; 42 | 43 | @OneToMany(mappedBy="customer") 44 | List contacts; 45 | 46 | @ManyToMany 47 | List features; 48 | 49 | 50 | public String toString() { 51 | return "id"+id+" name:"+name+" comments:"+comments; 52 | } 53 | 54 | public String getName() { 55 | return name; 56 | } 57 | 58 | public void setName(String name) { 59 | this.name = name; 60 | } 61 | 62 | public LocalDate getRegistered() { 63 | return registered; 64 | } 65 | 66 | public void setRegistered(LocalDate registered) { 67 | this.registered = registered; 68 | } 69 | 70 | public String getComments() { 71 | return comments; 72 | } 73 | 74 | public void setComments(String comments) { 75 | this.comments = comments; 76 | } 77 | 78 | public Address getBillingAddress() { 79 | return billingAddress; 80 | } 81 | 82 | public void setBillingAddress(Address billingAddress) { 83 | this.billingAddress = billingAddress; 84 | } 85 | 86 | public Address getShippingAddress() { 87 | return shippingAddress; 88 | } 89 | 90 | public void setShippingAddress(Address shippingAddress) { 91 | this.shippingAddress = shippingAddress; 92 | } 93 | 94 | public List getContacts() { 95 | return contacts; 96 | } 97 | 98 | public void setContacts(List contacts) { 99 | this.contacts = contacts; 100 | } 101 | 102 | public List getFeatures() { 103 | return features; 104 | } 105 | 106 | public void setFeatures(List features) { 107 | this.features = features; 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /e-kotlin-maven/src/test/java/org/example/domain/InsertCustomerTest.kt: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | 4 | import com.avaje.ebean.Ebean 5 | import org.example.ExampleBaseTestCase; 6 | import org.junit.Test; 7 | import kotlin.test.assertEquals 8 | import kotlin.test.assertNotNull 9 | 10 | public class InsertCustomerTest : ExampleBaseTestCase() { 11 | 12 | 13 | @Test 14 | fun test() { 15 | 16 | val customer = Customer("Roberto") 17 | //customer.name = "Roberto" 18 | customer.comments = "Say hello" 19 | 20 | // insert the customer 21 | customer.save(); 22 | 23 | val fetched = Customer.byId(customer.id); 24 | 25 | // fetch using the Ebean singleton style 26 | val fetched2 = Ebean.find(Customer::class.java, customer.id); 27 | 28 | assertEquals("Roberto", fetched?.name); 29 | assertEquals("Roberto", fetched2?.name); 30 | } 31 | 32 | 33 | /** 34 | * Test showing an explicit transaction. 35 | */ 36 | @Test 37 | fun testExplicitTransaction() { 38 | 39 | // create a transaction to wrap multiple saves 40 | val transaction = Ebean.beginTransaction(); 41 | try { 42 | 43 | val customer = Customer(); 44 | customer.name = "Roberto"; 45 | customer.save(); 46 | 47 | val otherCustomer = Customer(); 48 | otherCustomer.name = "Franko"; 49 | otherCustomer.save(); 50 | 51 | transaction.commit(); 52 | 53 | } finally { 54 | // this cleans up the transaction if something 55 | // fails in the try block 56 | transaction.end(); 57 | } 58 | 59 | } 60 | 61 | 62 | /** 63 | * Test showing an explicit transaction with extra control 64 | * the use of jdbc batching. 65 | */ 66 | @Test 67 | fun testExplicitTransactionWithBatchControls() { 68 | 69 | val transaction = Ebean.beginTransaction(); 70 | try { 71 | 72 | // turn off cascade persist for this transaction 73 | transaction.setPersistCascade(false); 74 | 75 | // extra control over jdbc batching for this transaction 76 | transaction.setBatchGetGeneratedKeys(false); 77 | transaction.setBatchMode(true); 78 | transaction.setBatchSize(20); 79 | transaction.setBatchFlushOnQuery(false); 80 | 81 | val customer = Customer(); 82 | customer.name = "Roberto"; 83 | customer.save(); 84 | 85 | val otherCustomer = Customer(); 86 | otherCustomer.name = "Franko"; 87 | otherCustomer.save(); 88 | 89 | transaction.commit(); 90 | 91 | } finally { 92 | transaction.end(); 93 | } 94 | 95 | } 96 | 97 | 98 | @Test 99 | fun testQuery() { 100 | 101 | val customers = 102 | Customer. 103 | where().ilike("name", "rob%") 104 | .findList(); 105 | 106 | assertNotNull(customers); 107 | 108 | } 109 | 110 | } 111 | -------------------------------------------------------------------------------- /x-postgres-features/src/test/java/org/example/BasicInsertTest.java: -------------------------------------------------------------------------------- 1 | package org.example; 2 | 3 | import com.avaje.ebean.Ebean; 4 | import com.avaje.ebean.annotation.Transactional; 5 | import org.example.domain.Customer; 6 | import org.example.domain.User; 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import java.io.IOException; 11 | import java.util.HashMap; 12 | import java.util.Map; 13 | 14 | import static org.junit.Assert.assertEquals; 15 | import static org.junit.Assert.assertNull; 16 | 17 | public class BasicInsertTest extends ExampleBaseTestCase { 18 | 19 | @Transactional 20 | @Test 21 | public void testInsert() throws IOException { 22 | 23 | Customer customer = new Customer(); 24 | customer.setName("Frankie"); 25 | 26 | Map tags = new HashMap<>(); 27 | tags.put("height","100"); 28 | tags.put("length","400"); 29 | tags.put("trim","large"); 30 | tags.put("colour","red"); 31 | 32 | customer.setTags(tags); 33 | customer.save(); 34 | 35 | Assert.assertNotNull(customer.getId()); 36 | 37 | String json = Ebean.json().toJson(customer); 38 | System.out.println(json); 39 | 40 | Customer customer1 = Ebean.json().toBean(Customer.class, json); 41 | 42 | assertEquals(customer.getId(), customer1.getId()); 43 | assertEquals(customer.getName(), customer1.getName()); 44 | assertEquals(customer.getTags().size(), customer1.getTags().size()); 45 | assertEquals(customer.getTags().get("height"), customer1.getTags().get("height")); 46 | assertEquals("400", customer1.getTags().get("length")); 47 | assertEquals("large", customer1.getTags().get("trim")); 48 | 49 | 50 | Customer customer2 = Customer.find.byId(customer.getId()); 51 | 52 | assertEquals(customer.getId(), customer2.getId()); 53 | assertEquals(customer.getName(), customer2.getName()); 54 | assertEquals(customer.getTags().size(), customer2.getTags().size()); 55 | assertEquals(customer.getTags().get("height"), customer2.getTags().get("height")); 56 | assertEquals("400", customer2.getTags().get("length")); 57 | assertEquals("large", customer2.getTags().get("trim")); 58 | 59 | customer.setTags(new HashMap()); 60 | String jsonWithEmpty = Ebean.json().toJson(customer); 61 | System.out.println("WITH EMPTY-> "+jsonWithEmpty); 62 | Customer customerWithEmpty = Ebean.json().toBean(Customer.class, jsonWithEmpty); 63 | assertEquals(0, customerWithEmpty.getTags().size()); 64 | 65 | 66 | customer.setTags(null); 67 | String jsonWithNull = Ebean.json().toJson(customer); 68 | System.out.println("WITH NULL-> " + jsonWithNull); 69 | Customer customerWithNull = Ebean.json().toBean(Customer.class, jsonWithNull); 70 | assertNull(customerWithNull.getTags()); 71 | } 72 | 73 | @Test 74 | public void testInsertUser() { 75 | 76 | User user= new User(); 77 | user.setName("Frankie"); 78 | 79 | Ebean.save(user); 80 | 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /e-groovy/src/test/groovy/org/example/service/InsertCustomerTest.groovy: -------------------------------------------------------------------------------- 1 | package org.example.service 2 | 3 | import com.avaje.ebean.Ebean 4 | import com.avaje.ebean.Transaction 5 | import org.example.domain.Customer 6 | import org.junit.Test 7 | 8 | import java.util.List 9 | 10 | import static org.junit.Assert.assertEquals 11 | import static org.junit.Assert.assertNotNull 12 | 13 | public class InsertCustomerTest extends ExampleBaseTestCase { 14 | 15 | 16 | @Test 17 | public void test() { 18 | 19 | Customer customer = new Customer(name: 'Rob') 20 | 21 | // insert the customer using extension method 22 | customer.save() 23 | 24 | Customer fetched = Customer.find.byId(customer.id) 25 | 26 | // fetch using the Ebean singleton style 27 | Customer fetched2 = Ebean.find(Customer, customer.id) 28 | 29 | assert "Rob" == fetched.name 30 | assert "Rob" == fetched2.name 31 | 32 | } 33 | 34 | 35 | /** 36 | * Test showing an explicit transaction. 37 | */ 38 | @Test 39 | public void testExplicitTransaction() { 40 | 41 | // create a transaction to wrap multiple saves 42 | Transaction transaction = Customer.db().beginTransaction() 43 | try { 44 | 45 | Customer customer = new Customer(name: 'Roberto') 46 | customer.save() 47 | 48 | Customer otherCustomer = new Customer() 49 | otherCustomer.name = "Franko" 50 | otherCustomer.save() 51 | 52 | transaction.commit() 53 | 54 | } finally { 55 | // this cleans up the transaction if something 56 | // fails in the try block 57 | transaction.end() 58 | } 59 | 60 | } 61 | 62 | 63 | /** 64 | * Test showing an explicit transaction with extra control 65 | * the use of jdbc batching. 66 | */ 67 | @Test 68 | public void testExplicitTransactionWithBatchControls() { 69 | 70 | Transaction transaction = Customer.db().beginTransaction() 71 | try { 72 | 73 | // turn off cascade persist for this transaction 74 | transaction.setPersistCascade(false) 75 | 76 | // extra control over jdbc batching for this transaction 77 | transaction.setBatchGetGeneratedKeys(false) 78 | transaction.setBatchMode(true) 79 | transaction.setBatchSize(20) 80 | 81 | Customer customer = new Customer(name: 'Roberto') 82 | customer.save() 83 | 84 | transaction.setBatchFlushOnQuery(false) 85 | 86 | Customer otherCustomer = new Customer(name: 'Franko') 87 | otherCustomer.save() 88 | 89 | transaction.commit() 90 | 91 | } finally { 92 | transaction.end() 93 | } 94 | 95 | } 96 | 97 | 98 | @Test 99 | public void testQuery() { 100 | 101 | List customers = 102 | Customer.find. 103 | where().ilike("name", "rob%") 104 | .findList() 105 | 106 | assert customers != null 107 | 108 | } 109 | 110 | } 111 | -------------------------------------------------------------------------------- /x-postgres-history/src/extra-ddl.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE customer ADD COLUMN sys_period tstzrange NOT NULL; 2 | 3 | 4 | CREATE TABLE customer_history (LIKE employees); 5 | 6 | 7 | CREATE TRIGGER customer_versioning_trigger 8 | BEFORE INSERT OR UPDATE OR DELETE ON customer 9 | FOR EACH ROW EXECUTE PROCEDURE versioning('sys_period', 10 | 'customer_history', 11 | true); 12 | 13 | 14 | CREATE TRIGGER customer_versioning_upd 15 | BEFORE UPDATE ON customer 16 | FOR EACH ROW EXECUTE PROCEDURE cust_update(); 17 | 18 | CREATE TRIGGER customer_versioning_ins 19 | BEFORE INSERT ON customer 20 | FOR EACH ROW EXECUTE PROCEDURE cust_insert(); 21 | 22 | CREATE TRIGGER customer_versioning_del 23 | BEFORE DELETE ON customer 24 | FOR EACH ROW EXECUTE PROCEDURE cust_delete(); 25 | 26 | drop TRIGGER customer_versioning_del on customer; 27 | 28 | create or REPLACE FUNCTION cust_update() RETURNS TRIGGER AS $cust_update$ 29 | BEGIN 30 | insert into customer_history (sys_period, 31 | id, inactive, name, comments, version, when_created, when_updated) 32 | VALUES (tstzrange(lower(OLD.sys_period), CURRENT_TIMESTAMP), 33 | OLD.id, OLD.inactive, OLD.name, OLD.comments, OLD.version, OLD.when_created, OLD.when_updated); 34 | NEW.sys_period = tstzrange(CURRENT_TIMESTAMP,null); 35 | RETURN NEW; 36 | END; 37 | $cust_update$ LANGUAGE plpgsql; 38 | 39 | create or REPLACE FUNCTION cust_insert() RETURNS TRIGGER AS $$ 40 | BEGIN 41 | NEW.sys_period = tstzrange(CURRENT_TIMESTAMP,null); 42 | RETURN NEW; 43 | END; 44 | $$ LANGUAGE plpgsql; 45 | 46 | CREATE VIEW customer_with_history AS 47 | SELECT * FROM customer 48 | UNION ALL 49 | SELECT * FROM customer_history; 50 | 51 | create or REPLACE FUNCTION cust_update() RETURNS TRIGGER AS $$ 52 | BEGIN 53 | insert into customer_history (sys_period, 54 | id, inactive, name, comments, version, when_created, when_updated) 55 | VALUES (tstzrange(lower(OLD.sys_period), CURRENT_TIMESTAMP), 56 | OLD.id, OLD.inactive, OLD.name, OLD.comments, OLD.version, OLD.when_created, OLD.when_updated); 57 | NEW.sys_period = tstzrange(CURRENT_TIMESTAMP,null); 58 | RETURN NEW; 59 | END; 60 | $$ LANGUAGE plpgsql; 61 | 62 | insert into customer (name, comments, version, when_created, when_updated) 63 | values ('jack','jack 1',1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP); 64 | 65 | select sys_period, * from customer; 66 | select sys_period, * from customer_history; 67 | delete from customer where id = 5; 68 | 69 | 70 | update customer set comments = 'rob update' where id = 1; 71 | select sys_period, * from customer_history; 72 | 73 | 74 | create or REPLACE FUNCTION cust_delete() RETURNS TRIGGER AS $$ 75 | BEGIN 76 | insert into customer_history (sys_period, 77 | id, inactive, name, comments, version, when_created, when_updated) 78 | VALUES (tstzrange(lower(OLD.sys_period), CURRENT_TIMESTAMP), 79 | OLD.id, OLD.inactive, OLD.name, OLD.comments, OLD.version, OLD.when_created, OLD.when_updated); 80 | RETURN OLD; 81 | END; 82 | $$ LANGUAGE plpgsql; -------------------------------------------------------------------------------- /x-postgres-history/src/main/java/org/example/domain/excludem2m/CustomerExcludeM2M.java: -------------------------------------------------------------------------------- 1 | package org.example.domain.excludem2m; 2 | 3 | import com.avaje.ebean.Model; 4 | import com.avaje.ebean.annotation.History; 5 | import com.avaje.ebean.annotation.HistoryExclude; 6 | import org.example.domain.Address; 7 | import org.example.domain.BaseModel; 8 | 9 | import javax.persistence.CascadeType; 10 | import javax.persistence.Entity; 11 | import javax.persistence.ManyToMany; 12 | import javax.persistence.ManyToOne; 13 | import javax.persistence.OneToMany; 14 | import javax.persistence.Table; 15 | import javax.validation.constraints.Size; 16 | import java.time.LocalDate; 17 | import java.util.List; 18 | 19 | /** 20 | * Test Customer that @HistoryExclude on the features ManyToMany property. 21 | */ 22 | @History 23 | @Entity 24 | @Table(name="customer") 25 | public class CustomerExcludeM2M extends BaseModel { 26 | 27 | public static final Model.Finder find = new Model.Finder<>(CustomerExcludeM2M.class); 28 | 29 | @Size(max = 100) 30 | String name; 31 | 32 | LocalDate registered; 33 | 34 | @Size(max = 1000) 35 | String comments; 36 | 37 | @ManyToOne(cascade=CascadeType.ALL) 38 | Address billingAddress; 39 | 40 | @ManyToOne(cascade=CascadeType.ALL) 41 | Address shippingAddress; 42 | 43 | @OneToMany(mappedBy="customer") 44 | List contacts; 45 | 46 | /** 47 | * HistoryExclude on ManyToMany means the intersection table does not have history. 48 | */ 49 | @HistoryExclude 50 | @ManyToMany 51 | List features; 52 | 53 | 54 | public String toString() { 55 | return "id"+id+" name:"+name+" comments:"+comments; 56 | } 57 | 58 | public String getName() { 59 | return name; 60 | } 61 | 62 | public void setName(String name) { 63 | this.name = name; 64 | } 65 | 66 | public LocalDate getRegistered() { 67 | return registered; 68 | } 69 | 70 | public void setRegistered(LocalDate registered) { 71 | this.registered = registered; 72 | } 73 | 74 | public String getComments() { 75 | return comments; 76 | } 77 | 78 | public void setComments(String comments) { 79 | this.comments = comments; 80 | } 81 | 82 | public Address getBillingAddress() { 83 | return billingAddress; 84 | } 85 | 86 | public void setBillingAddress(Address billingAddress) { 87 | this.billingAddress = billingAddress; 88 | } 89 | 90 | public Address getShippingAddress() { 91 | return shippingAddress; 92 | } 93 | 94 | public void setShippingAddress(Address shippingAddress) { 95 | this.shippingAddress = shippingAddress; 96 | } 97 | 98 | public List getContacts() { 99 | return contacts; 100 | } 101 | 102 | public void setContacts(List contacts) { 103 | this.contacts = contacts; 104 | } 105 | 106 | public List getFeatures() { 107 | return features; 108 | } 109 | 110 | public void setFeatures(List features) { 111 | this.features = features; 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /a-basic/src/test/java/org/example/domain/InsertCustomerTest.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.util.List; 6 | 7 | import org.example.ExampleBaseTestCase; 8 | import org.junit.Test; 9 | 10 | import com.avaje.ebean.Ebean; 11 | import com.avaje.ebean.Transaction; 12 | 13 | public class InsertCustomerTest extends ExampleBaseTestCase { 14 | 15 | 16 | @Test 17 | public void test() { 18 | 19 | Customer customer = new Customer(); 20 | customer.setName("Rob"); 21 | 22 | // insert the customer 23 | customer.save(); 24 | 25 | Customer fetched = Customer.find.byId(customer.getId()); 26 | 27 | // fetch using the Ebean singleton style 28 | Customer fetched2 = Ebean.find(Customer.class, customer.getId()); 29 | 30 | assertEquals("Rob", fetched.getName()); 31 | assertEquals("Rob", fetched2.getName()); 32 | 33 | } 34 | 35 | 36 | /** 37 | * Test showing an explicit transaction. 38 | */ 39 | @Test 40 | public void testExplicitTransaction() { 41 | 42 | // create a transaction to wrap multiple saves 43 | Transaction transaction = Customer.db().beginTransaction(); 44 | try { 45 | 46 | Customer customer = new Customer(); 47 | customer.setName("Roberto"); 48 | customer.save(); 49 | 50 | Customer otherCustomer = new Customer(); 51 | otherCustomer.setName("Franko"); 52 | otherCustomer.save(); 53 | 54 | transaction.commit(); 55 | 56 | } finally { 57 | // this cleans up the transaction if something 58 | // fails in the try block 59 | transaction.end(); 60 | } 61 | 62 | } 63 | 64 | 65 | /** 66 | * Test showing an explicit transaction with extra control 67 | * the use of jdbc batching. 68 | */ 69 | @Test 70 | public void testExplicitTransactionWithBatchControls() { 71 | 72 | Transaction transaction = Customer.db().beginTransaction(); 73 | try { 74 | 75 | // turn off cascade persist for this transaction 76 | transaction.setPersistCascade(false); 77 | 78 | // extra control over jdbc batching for this transaction 79 | transaction.setBatchGetGeneratedKeys(false); 80 | transaction.setBatchMode(true); 81 | transaction.setBatchSize(20); 82 | 83 | Customer customer = new Customer(); 84 | customer.setName("Roberto"); 85 | customer.save(); 86 | 87 | transaction.setBatchFlushOnQuery(false); 88 | 89 | Customer otherCustomer = new Customer(); 90 | otherCustomer.setName("Franko"); 91 | otherCustomer.save(); 92 | 93 | transaction.commit(); 94 | 95 | } finally { 96 | transaction.end(); 97 | } 98 | 99 | } 100 | 101 | 102 | @Test 103 | public void testQuery() { 104 | 105 | List customers = 106 | Customer.find. 107 | where().name.ilike("rob%") 108 | .findList(); 109 | 110 | assertNotNull(customers); 111 | 112 | } 113 | 114 | } 115 | -------------------------------------------------------------------------------- /a-basic/src/main/java/org/example/domain/Customer.java: -------------------------------------------------------------------------------- 1 | package org.example.domain; 2 | 3 | import org.example.domain.finder.CustomerFinder; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Date; 7 | import java.util.List; 8 | 9 | import javax.persistence.CascadeType; 10 | import javax.persistence.Column; 11 | import javax.persistence.Entity; 12 | import javax.persistence.ManyToOne; 13 | import javax.persistence.OneToMany; 14 | import javax.persistence.Table; 15 | 16 | /** 17 | * Customer entity bean. 18 | */ 19 | @Entity 20 | @Table(name="be_customer") 21 | public class Customer extends BaseModel { 22 | 23 | /** 24 | * Convenience Finder for 'active record' style. 25 | */ 26 | public static final CustomerFinder find = new CustomerFinder(); 27 | 28 | boolean inactive; 29 | 30 | @Column(length=100) 31 | String name; 32 | 33 | Date registered; 34 | 35 | @Column(length=1000) 36 | String comments; 37 | 38 | @ManyToOne(cascade=CascadeType.ALL) 39 | Address billingAddress; 40 | 41 | @ManyToOne(cascade=CascadeType.ALL) 42 | Address shippingAddress; 43 | 44 | @OneToMany(mappedBy="customer", cascade=CascadeType.PERSIST) 45 | List contacts; 46 | 47 | @OneToMany(mappedBy="customer") 48 | List orders; 49 | 50 | public boolean isInactive() { 51 | return inactive; 52 | } 53 | 54 | public void setInactive(boolean inactive) { 55 | this.inactive = inactive; 56 | } 57 | 58 | public String getName() { 59 | return name; 60 | } 61 | 62 | public void setName(String name) { 63 | this.name = name; 64 | } 65 | 66 | public Date getRegistered() { 67 | return registered; 68 | } 69 | 70 | public void setRegistered(Date registered) { 71 | this.registered = registered; 72 | } 73 | 74 | public String getComments() { 75 | return comments; 76 | } 77 | 78 | public void setComments(String comments) { 79 | this.comments = comments; 80 | } 81 | 82 | public Address getBillingAddress() { 83 | return billingAddress; 84 | } 85 | 86 | public void setBillingAddress(Address billingAddress) { 87 | this.billingAddress = billingAddress; 88 | } 89 | 90 | public Address getShippingAddress() { 91 | return shippingAddress; 92 | } 93 | 94 | public void setShippingAddress(Address shippingAddress) { 95 | this.shippingAddress = shippingAddress; 96 | } 97 | 98 | public List getOrders() { 99 | return orders; 100 | } 101 | 102 | public void setOrders(List orders) { 103 | this.orders = orders; 104 | } 105 | 106 | public List getContacts() { 107 | return contacts; 108 | } 109 | 110 | public void setContacts(List contacts) { 111 | this.contacts = contacts; 112 | } 113 | 114 | /** 115 | * Helper method to add a contact to the customer. 116 | */ 117 | public void addContact(Contact contact) { 118 | if (contacts == null) { 119 | contacts = new ArrayList<>(); 120 | } 121 | // setting the customer is automatically done when Ebean does 122 | // a cascade save from customer to contacts. 123 | contact.setCustomer(this); 124 | contacts.add(contact); 125 | } 126 | 127 | } 128 | -------------------------------------------------------------------------------- /a-basic/src/test/java/org/example/domain/finder/OrderFinderTest.java: -------------------------------------------------------------------------------- 1 | package org.example.domain.finder; 2 | 3 | import com.avaje.ebean.PagedList; 4 | import org.example.ExampleBaseTestCase; 5 | import org.example.domain.Address; 6 | import org.example.domain.Contact; 7 | import org.example.domain.Customer; 8 | import org.example.domain.Order; 9 | import org.example.domain.OrderDetail; 10 | import org.example.domain.Product; 11 | import org.example.service.LoadExampleData; 12 | import org.junit.BeforeClass; 13 | import org.junit.Test; 14 | 15 | import java.sql.Date; 16 | import java.time.OffsetDateTime; 17 | import java.util.List; 18 | 19 | public class OrderFinderTest extends ExampleBaseTestCase { 20 | 21 | @BeforeClass 22 | public static void before() { 23 | //LoadExampleData.load(); 24 | } 25 | 26 | @Test 27 | public void test() { 28 | 29 | List orders = Order.find.byStatus(Order.Status.NEW, Order.Status.APPROVED, Order.Status.COMPLETE); 30 | 31 | for (Order order : orders) { 32 | order.getOrderDate(); 33 | order.getShipDate(); 34 | order.getStatus(); 35 | Customer customer = order.getCustomer(); 36 | customer.getName(); 37 | // Address billingAddress = customer.getBillingAddress(); 38 | // if (billingAddress != null) { 39 | // billingAddress.getLine1(); 40 | // billingAddress.getLine2(); 41 | // billingAddress.getCity(); 42 | // billingAddress.getCountry().getCode(); 43 | // } 44 | 45 | List details = order.getDetails(); 46 | for (OrderDetail detail : details) { 47 | detail.getUnitPrice(); 48 | detail.getShipQty(); 49 | Product product = detail.getProduct(); 50 | product.getId(); 51 | product.getName(); 52 | } 53 | } 54 | 55 | } 56 | 57 | @Test 58 | public void testOther() { 59 | 60 | 61 | List orders = Order.find.byStatus(Order.Status.NEW, Order.Status.APPROVED); 62 | 63 | for (Order order : orders) { 64 | order.getOrderDate(); 65 | order.getShipDate(); 66 | Customer customer = order.getCustomer(); 67 | customer.getName(); 68 | //customer.getComments(); 69 | 70 | List contacts = customer.getContacts(); 71 | for (Contact contact : contacts) { 72 | contact.getFirstName(); 73 | contact.getLastName(); 74 | //contact.getEmail(); 75 | } 76 | } 77 | } 78 | 79 | @Test 80 | public void testOrdersSince() { 81 | 82 | OffsetDateTime odt = OffsetDateTime.now().minusDays(20); 83 | Date daysAgo = new Date(odt.toInstant().toEpochMilli()); 84 | 85 | PagedList orderPaged = Order.find.newOrdersSince(daysAgo, 0); 86 | orderPaged.loadRowCount(); 87 | List list = orderPaged.getList(); 88 | orderPaged.getTotalRowCount(); 89 | 90 | for (Order order : list) { 91 | order.getCustomer().getName(); 92 | List details = order.getDetails(); 93 | for (OrderDetail detail : details) { 94 | detail.getOrderQty(); 95 | detail.getShipQty(); 96 | detail.getUnitPrice(); 97 | detail.getProduct().getName(); 98 | } 99 | } 100 | 101 | } 102 | 103 | } 104 | --------------------------------------------------------------------------------