Compare commits

..
Author SHA1 Message Date
Robin Bygrave 998d8ed6f2 [maven-release-plugin] prepare release avaje-ebeanorm-6.16.3 2016-02-02 13:37:46 +13:00
Robin Bygrave a8da472872 #550 - ENH: Add generated file warning to DB migration XML files 2016-02-02 13:33:46 +13:00
Robin Bygrave a9cdf9736a Bump maven plugin version to 4.9.1 2016-02-02 13:23:46 +13:00
Robin Bygrave c930a1dd6b #549 - Regression: ArrayIndexOutOfBoundsException when lazy loading on bean with inheritance - tidy up 2016-02-02 11:29:38 +13:00
Robin Bygrave 67ce6ef58e #549 - Regression: ArrayIndexOutOfBoundsException when lazy loading on bean with inheritance 2016-02-02 11:24:46 +13:00
Robin Bygrave c4f28efc45 Testing CI - .travis - git depth + skip mvn install 2016-01-28 10:54:16 +13:00
Robin Bygrave afa4bc06d9 Testing CI - .travis - try setup_database 2016-01-28 10:44:14 +13:00
Robin Bygrave 828c9ce128 Testing CI - .travis - try sudo required 2016-01-28 09:31:18 +13:00
Robin Bygrave 6f37a0bd72 Testing CI - .travis - print surefire reports + setup_database script 2016-01-28 09:20:27 +13:00
Robin Bygrave 036086af02 Testing CI - .travis - print surefire reports on failure 2016-01-28 09:00:44 +13:00
Robin Bygrave f34b5e52e0 [maven-release-plugin] prepare for next development iteration 2016-01-28 08:36:40 +13:00
Robin Bygrave a70e289b00 [maven-release-plugin] prepare release avaje-ebeanorm-6.16.2 2016-01-28 08:36:18 +13:00
Robin Bygrave 4ad0e06aab Testing CI - TestEncrypt change to only run against H2 for now 2016-01-27 22:53:06 +13:00
Robin Bygrave 86de7e845e Testing CI - add sql logging for TestEncrypt run on CI server 2016-01-27 22:39:03 +13:00
Robin Bygrave 38501ca372 Modify .travis.yml - add postgres pgcrypto extension 2016-01-27 22:15:39 +13:00
Robin Bygrave 4790c83fa1 Modify .travis.yml - cache .m2 directory 2016-01-27 21:56:00 +13:00
Robin Bygrave b410c83492 Modify .travis.yml for postgres - add hstore and postgis extensions 2016-01-27 21:50:43 +13:00
Robin Bygrave b19145f16f Modify .travis.yml for postgres - add hstore and postgis extensions 2016-01-27 21:46:30 +13:00
Robin Bygrave c28cff2f40 Modify .travis.yml for h2 and postgres runs using EBEAN_DB environment variable 2016-01-27 21:37:57 +13:00
Robin Bygrave 0c8cf78ecc Modify .travis.yml for h2 and postgres runs using EBEAN_DB environment variable 2016-01-27 21:21:01 +13:00
Robin Bygrave df112815ff #545 - ENH: Read environment variable "EBEAN_DB" to set default datasource - typically for CI builds 2016-01-27 21:20:13 +13:00
Robin Bygrave 85522ed8bc Modify .travis.yml for h2 and postgres runs 2016-01-27 20:39:53 +13:00
Robin Bygrave cf88f2375b #544 - Postgres @History DB trigger using current_timestamp (transaction) and should instead use statement_timestamp() 2016-01-27 20:34:51 +13:00
Robin Bygrave 24151f11fd #543 - Tidy up test logging - adjust test LoggedSqlCollector - remove logging noise when running tests 2016-01-27 20:12:36 +13:00
Robin Bygrave 424a83fa0a #543 - Tidy up test logging - adjust test LoggedSqlCollector - remove logging noise when running tests 2016-01-27 19:10:52 +13:00
Robin Bygrave 53dd91416c #543 - Tidy up test logging - adjust test LoggedSqlCollector - remove logging noise when running tests 2016-01-27 19:05:19 +13:00
Robin Bygrave 5a463c5071 #543 - Tidy up test logging - adjust test LoggedSqlCollector - remove trace level logging when running tests 2016-01-27 18:25:54 +13:00
Robin Bygrave bc3cf1348a #543 - Tidy up test logging - adjust test LoggedSqlCollector 2016-01-27 18:25:16 +13:00
Robin Bygrave f3b1024283 Fix for test asserts to run against Postgres with escape clause on LIKE expression 2016-01-27 17:22:55 +13:00
Robin Bygrave 7ba35d7a62 Update .travis - postgres service 2016-01-27 16:17:45 +13:00
Robin Bygrave 990ca2980b Add initial .travis.yml 2016-01-27 16:05:33 +13:00
Robin Bygrave 38dd3d82fc #536 - @ManyToOne with @JoinColumn throw error - Update tests 2016-01-26 16:41:15 +13:00
Robin Bygrave 6be26aaff2 #537 - Change PersistenceException: Mapping limitation on @JoinColumn such that it includes the bean type 2016-01-26 15:30:57 +13:00
Robin Bygrave d1462b032a #535 - Bean lazy loading can execute an extra query when one of the lazy loaded beans has been deleted 2016-01-21 20:51:45 +13:00
Robin Bygrave 6ab974a330 #535 - Bean lazy loading can execute an extra query when one of the lazy loaded beans has been deleted 2016-01-21 20:51:32 +13:00
Robin Bygrave 64618da6a7 #534 - REFACTOR: Internals for lazy loading specifically improving errors/logging in cases of lazy load errors due to deletes - Update debug logging for LoadManyRequest when empty collection found (expected frequently) 2016-01-21 17:52:23 +13:00
Robin Bygrave d242ff7351 #534 - REFACTOR: Internals for lazy loading specifically improving errors/logging in cases of lazy load errors due to deletes - Initial refactor for LoadManyRequest 2016-01-21 17:30:49 +13:00
Robin Bygrave 7d54aaa9a7 #534 - REFACTOR: Internals for lazy loading specifically improving errors/logging in cases of lazy load errors due to deletes - Initial refactor 2016-01-21 16:50:51 +13:00
Robin Bygrave 0e46184656 No effective change - Rename test class 2016-01-21 16:03:51 +13:00
Robin Bygrave c9e1a6edbd Bump agent to 4.9.1 2016-01-20 15:13:11 +13:00
Robin Bygrave 3c0cffd146 [maven-release-plugin] prepare for next development iteration 2016-01-19 19:49:31 +13:00
Robin Bygrave d31547fb69 [maven-release-plugin] prepare release avaje-ebeanorm-6.16.1 2016-01-19 19:49:08 +13:00
Robin Bygrave 93adaa12bc Bump pom to 6.16.1-SNAPSHOT 2016-01-19 19:48:22 +13:00
Robin Bygrave d1cdbe94de #529 - @OneToOne with @JoinColumn to non primary key throws Data conversion error 2016-01-19 19:46:41 +13:00
Robin Bygrave e7c6223799 #530 - Bad join with raw() expression on a @ManyToMany path. 2016-01-19 13:18:28 +13:00
Robin Bygrave d33afd85de #533 - DDL - Invalid foreign key constraint name for @ManyToMany when using explicit schema in @Table - Temp testing removed 2016-01-19 10:01:04 +13:00
Robin Bygrave 77bcb4aef9 #533 - DDL - Invalid foreign key constraint name for @ManyToMany when using explicit schema in @Table - Temp testing 2016-01-19 09:57:49 +13:00
Robin Bygrave a70fe184c2 #533 - DDL - Invalid foreign key constraint name for @ManyToMany when using explicit schema in @Table - Fix 2016-01-19 09:56:27 +13:00
Robin Bygrave 7238f39be5 #531 - AutoTune - On shutdown provide ability to saving profiling without garbage collection 2016-01-19 08:16:17 +13:00
Robin Bygrave ae44b59c5f #532 - Change example expression such that it supports nested beans 2016-01-18 23:52:28 +13:00
Robin Bygrave ced4e71307 [maven-release-plugin] prepare for next development iteration 2016-01-13 16:13:59 +13:00
Robin Bygrave 9cb43bd5dd [maven-release-plugin] prepare release avaje-ebeanorm-6.15.2 2016-01-13 16:13:30 +13:00
Robin Bygrave 9665de2dbe #502 - jsonContext.toJson(o, generator, path) can not apply path on @Transient field that is an entity bean 2016-01-13 13:29:06 +13:00
Robin Bygrave e2666f0c4c No effective change - turn off autotune profiling/tuning flags for test running 2016-01-13 12:38:04 +13:00
Robin Bygrave 89c83c90d1 No effective change - add test to show findRowCount with joins 2016-01-13 12:35:52 +13:00
Robin Bygrave cdd25351a6 No effective change - test code static imports 2016-01-13 11:40:23 +13:00
Robin Bygrave 17e0413837 No effective change - code format 2016-01-13 11:38:53 +13:00
Robin Bygrave b04419d9c7 #527 - @OrderBy on child-of-child property 2016-01-13 11:37:38 +13:00
Robin Bygrave 0453892ebd #526 - findPagedList with @EmbeddedId broken - automatically adds orderby t0.null 2016-01-13 10:43:56 +13:00
Robin Bygrave 55493c5b7d #524 - ExplicitJdbcTransaction leaves dangling “begin” - comment update 2016-01-13 00:17:22 +13:00
Robin Bygrave eb2f7cd852 #525 - Automatic autotune data 2016-01-13 00:08:00 +13:00
Robin Bygrave f44eb234c8 #525 - Automatic autotune data - initial wip 2016-01-12 11:45:24 +13:00
Robin Bygrave 404b57ab21 #524 - ExplicitJdbcTransaction leaves dangling “begin” - expected fix 2016-01-12 08:37:12 +13:00
Robin Bygrave a54f09ca4b #524 - ExplicitJdbcTransaction leaves dangling “begin” - expected fix 2016-01-11 14:04:14 +13:00
Robin Bygrave a3bb909308 [maven-release-plugin] prepare for next development iteration 2016-01-08 00:22:06 +13:00
138 changed files with 2086 additions and 993 deletions
+32
View File
@@ -0,0 +1,32 @@
sudo: required
language: java
jdk:
- oraclejdk8
git:
depth: 3
addons:
postgresql: "9.4"
services:
- postgresql
before_script:
- ./.travis/setup_database
env:
- EBEAN_DB=h2
- EBEAN_DB=pg
install: true
script:
- mvn clean test
after_failure:
- ./.travis/print_surefire_reports
cache:
directories:
- $HOME/.m2
+10
View File
@@ -0,0 +1,10 @@
#!/bin/sh
echo "\n=== SUREFIRE REPORTS ===\n"
for file in target/surefire-reports/*.txt
do
echo ${file}
cat ${file}
echo
done
+14
View File
@@ -0,0 +1,14 @@
#!/bin/sh
echo "\n=== SETUP DATABASE ===\n"
#set -e
#set -x
# Setup postgres' users and databases
sudo -u postgres psql -c "CREATE USER unit WITH PASSWORD 'unit';"
sudo -u postgres psql -c 'CREATE DATABASE unit WITH OWNER unit;'
sudo -u postgres psql unit -c 'CREATE EXTENSION hstore;'
sudo -u postgres psql unit -c 'CREATE EXTENSION pgcrypto;'
#sudo -u postgres psql test2 -c 'CREATE EXTENSION postgis;'
+3 -3
View File
@@ -9,7 +9,7 @@
<groupId>org.avaje.ebeanorm</groupId>
<artifactId>avaje-ebeanorm</artifactId>
<version>6.15.1</version>
<version>6.16.3</version>
<packaging>jar</packaging>
<name>avaje-ebeanorm</name>
@@ -152,7 +152,7 @@
<dependency>
<groupId>org.avaje.ebeanorm</groupId>
<artifactId>avaje-ebeanorm-agent</artifactId>
<version>4.8.1</version>
<version>4.9.1</version>
<scope>test</scope>
</dependency>
@@ -219,7 +219,7 @@
<plugin>
<groupId>org.avaje.ebeanorm</groupId>
<artifactId>avaje-ebeanorm-mavenenhancer</artifactId>
<version>4.8.1</version>
<version>4.9.1</version>
<executions>
<!-- Not going to enhance Model bean -->
<execution>
@@ -11,4 +11,14 @@ public interface AutoTune {
*/
void collectProfiling();
/**
* Output the profiling.
* <p>
* When profiling updates are applied to tuning at runtime this reports all tuning and profiling combined.
* When profiling is not applied at runtime then this reports the diff report with new and diff entries relative
* to the existing tuning.
* </p>
*/
void reportProfiling();
}
@@ -58,15 +58,18 @@ class PrimaryServer {
*/
private static String determineDefaultServerName() {
String defaultServerName = System.getProperty("datasource.default");
String defaultServerName = System.getenv("EBEAN_DB");
if (isEmpty(defaultServerName)) {
defaultServerName = System.getProperty("ebean.default.datasource");
}
if (isEmpty(defaultServerName)) {
defaultServerName = globalProperties.getProperty("datasource.default");
}
if (isEmpty(defaultServerName)) {
defaultServerName = globalProperties.getProperty("ebean.default.datasource");
defaultServerName = System.getProperty("datasource.default");
if (isEmpty(defaultServerName)) {
defaultServerName = System.getProperty("ebean.default.datasource");
if (isEmpty(defaultServerName)) {
defaultServerName = globalProperties.getProperty("datasource.default");
if (isEmpty(defaultServerName)) {
defaultServerName = globalProperties.getProperty("ebean.default.datasource");
}
}
}
}
return defaultServerName;
}
@@ -1,5 +1,10 @@
package com.avaje.ebean.bean;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.ValuePair;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceException;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
@@ -11,12 +16,6 @@ import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceException;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.ValuePair;
/**
* This is the object added to every entity bean using byte code enhancement.
* <p>
@@ -92,6 +91,8 @@ public final class EntityBeanIntercept implements Serializable {
private int lazyLoadProperty = -1;
private Object ownerId;
/**
* Create a intercept with a given entity.
* <p>
@@ -342,18 +343,11 @@ public final class EntityBeanIntercept implements Serializable {
}
/**
* Check if the lazy load succeeded. If not then mark this bean as having
* failed lazy loading due to the underlying row being deleted.
* <p>
* We mark the bean this way rather than immediately fail as we might be batch
* lazy loading and this bean might not be used by the client code at all.
* Instead we will fail as soon as the client code tries to use this bean.
* </p>
* Set lazy load failure flag.
*/
public void checkLazyLoadFailure() {
if (lazyLoadProperty != -1) {
this.lazyLoadFailure = true;
}
public void setLazyLoadFailure(Object ownerId) {
this.lazyLoadFailure = true;
this.ownerId = ownerId;
}
/**
@@ -748,7 +742,7 @@ public final class EntityBeanIntercept implements Serializable {
if (lazyLoadFailure) {
// failed when batch lazy loaded by another bean in the batch
throw new EntityNotFoundException("Bean has been deleted - lazy loading failed");
throw new EntityNotFoundException("Lazy loading failed on type:" + owner.getClass().getName() + " id:" + ownerId + " - Bean has been deleted");
}
if (lazyLoadProperty == -1) {
@@ -763,7 +757,7 @@ public final class EntityBeanIntercept implements Serializable {
if (lazyLoadFailure) {
// failed when lazy loading this bean
throw new EntityNotFoundException("Bean has been deleted - lazy loading failed");
throw new EntityNotFoundException("Lazy loading failed on type:" + owner.getClass().getName() + " id:" + ownerId + " - Bean has been deleted.");
}
// bean should be loaded and intercepting now. setLoaded() has
@@ -21,9 +21,13 @@ public class AutoTuneConfig {
private double profilingRate = 0.01;
private int profilingUpdateFrequency;
private int garbageCollectionWait = 100;
private boolean skipCollectionOnShutdown;
private boolean skipGarbageCollectionOnShutdown;
private boolean skipProfileReportingOnShutdown;
public AutoTuneConfig() {
}
@@ -56,6 +60,20 @@ public class AutoTuneConfig {
this.profilingFile = profilingFile;
}
/**
* Return the frequency in seconds the profiling should be collected and automatically applied to the tuning.
*/
public int getProfilingUpdateFrequency() {
return profilingUpdateFrequency;
}
/**
* Set the frequency in seconds the profiling should be collected and automatically applied to the tuning.
*/
public void setProfilingUpdateFrequency(int profilingUpdateFrequency) {
this.profilingUpdateFrequency = profilingUpdateFrequency;
}
/**
* Return the mode used when autoTune has not been explicit defined on a
* query.
@@ -184,17 +202,33 @@ public class AutoTuneConfig {
}
/**
* Return true if profiling collection should be skipped on shutdown.
* Return true if triggering garbage collection should be skipped on shutdown.
* You might set this when System.GC() slows a application shutdown too much.
*/
public boolean isSkipCollectionOnShutdown() {
return skipCollectionOnShutdown;
public boolean isSkipGarbageCollectionOnShutdown() {
return skipGarbageCollectionOnShutdown;
}
/**
* Set to true if profiling collection should be skipped on shutdown.
* Set to true if triggering garbage collection should be skipped on shutdown.
* You might set this when System.GC() slows a application shutdown too much.
*/
public void setSkipCollectionOnShutdown(boolean skipCollectionOnShutdown) {
this.skipCollectionOnShutdown = skipCollectionOnShutdown;
public void setSkipGarbageCollectionOnShutdown(boolean skipGarbageCollectionOnShutdown) {
this.skipGarbageCollectionOnShutdown = skipGarbageCollectionOnShutdown;
}
/**
* Return true if profile reporting should be skipped on shutdown.
*/
public boolean isSkipProfileReportingOnShutdown() {
return skipProfileReportingOnShutdown;
}
/**
* Set to true if profile reporting should be skipped on shutdown.
*/
public void setSkipProfileReportingOnShutdown(boolean skipProfileReportingOnShutdown) {
this.skipProfileReportingOnShutdown = skipProfileReportingOnShutdown;
}
/**
@@ -206,13 +240,15 @@ public class AutoTuneConfig {
queryTuningAddVersion = p.getBoolean("autoTune.queryTuningAddVersion", queryTuningAddVersion);
queryTuningFile = p.get("autoTune.queryTuningFile", queryTuningFile);
skipCollectionOnShutdown = p.getBoolean("autoTune.skipCollectionOnShutdown", skipCollectionOnShutdown);
skipGarbageCollectionOnShutdown = p.getBoolean("autoTune.skipGarbageCollectionOnShutdown", skipGarbageCollectionOnShutdown);
skipProfileReportingOnShutdown = p.getBoolean("autoTune.skipProfileReportingOnShutdown", skipProfileReportingOnShutdown);
mode = p.getEnum(AutoTuneMode.class, "autoTune.mode", mode);
profiling = p.getBoolean("autoTune.profiling", profiling);
profilingBase = p.getInt("autoTune.profilingBase", profilingBase);
profilingRate = p.getDouble("autoTune.profilingRate", profilingRate);
profilingFile = p.get("autoTune.profilingFile", profilingFile);
profilingUpdateFrequency = p.getInt("autoTune.profilingUpdateFrequency", profilingUpdateFrequency);
}
}
@@ -88,6 +88,8 @@ public class DbMigrationConfig {
protected String modelSuffix = ".model.xml";
protected boolean includeGeneratedFileComment;
/**
* Return the DB platform to generate migration DDL for.
*
@@ -236,6 +238,20 @@ public class DbMigrationConfig {
this.rollbackSuffix = rollbackSuffix;
}
/**
* Return true if the generated file comment should be included.
*/
public boolean isIncludeGeneratedFileComment() {
return includeGeneratedFileComment;
}
/**
* Set to true if the generated file comment should be included.
*/
public void setIncludeGeneratedFileComment(boolean includeGeneratedFileComment) {
this.includeGeneratedFileComment = includeGeneratedFileComment;
}
/**
* Set the migration version.
* <p>
@@ -281,6 +297,7 @@ public class DbMigrationConfig {
dropSuffix = properties.get("migration.dropSuffix", dropSuffix);
rollbackSuffix = properties.get("migration.rollbackSuffix", rollbackSuffix);
modelSuffix = properties.get("migration.modelSuffix", modelSuffix);
includeGeneratedFileComment = properties.getBoolean("migration.includeGeneratedFileComment", includeGeneratedFileComment);
platform = properties.getEnum(DbPlatformName.class, "migration.platform", platform);
suppressRollback = properties.getBoolean("migration.suppressRollback", suppressRollback);
@@ -59,6 +59,8 @@ public class DbMigration {
private static final String initialVersion = "1.0";
private static final String GENERATED_COMMENT = "THIS IS A GENERATED FILE - DO NOT MODIFY";
/**
* Set to true if DbMigration run with online EbeanServer instance.
*/
@@ -304,7 +306,8 @@ public class DbMigration {
if (file.exists()) {
return false;
}
MigrationXmlWriter xmlWriter = new MigrationXmlWriter();
String comment = migrationConfig.isIncludeGeneratedFileComment() ? GENERATED_COMMENT : null;
MigrationXmlWriter xmlWriter = new MigrationXmlWriter(comment);
xmlWriter.write(dbMigration, file);
return true;
}
@@ -14,7 +14,7 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl {
public PostgresHistoryDdl() {
this.currentTimestamp = "current_timestamp";
this.currentTimestamp = "statement_timestamp()";
}
/**
@@ -80,7 +80,7 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl {
.append(" if (TG_OP = 'UPDATE') then").newLine();
appendInsertIntoHistory(apply, historyTable, includedColumns);
apply
.append(" NEW.").append(sysPeriod).append(" = tstzrange(CURRENT_TIMESTAMP,null);").newLine()
.append(" NEW.").append(sysPeriod).append(" = tstzrange(").append(currentTimestamp).append(",null);").newLine()
.append(" return new;").newLine();
apply
.append(" elsif (TG_OP = 'DELETE') then").newLine();
@@ -137,7 +137,7 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl {
buffer.append(" insert into ").append(historyTable).append(" (").append(sysPeriod).append(",");
appendColumnNames(buffer, columns, "");
buffer.append(") values (tstzrange(lower(OLD.").append(sysPeriod).append("), current_timestamp), ");
buffer.append(") values (tstzrange(lower(OLD.").append(sysPeriod).append("), ").append(currentTimestamp).append("), ");
appendColumnNames(buffer, columns, "OLD.");
buffer.append(");").newLine();
}
@@ -7,22 +7,46 @@ import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
* Simple writer for output of the Migration/ChangeSet as an XML document.
*/
public class MigrationXmlWriter {
private final String comment;
public MigrationXmlWriter(String comment) {
this.comment = comment;
}
/**
* Write a Migration to a file as an xml document to the file.
*/
public void write(Migration migration, File file) {
try {
FileWriter writer = new FileWriter(file);
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
if (comment != null) {
writer.write("<!-- ");
writer.write(comment);
writer.write(" -->\n");
}
JAXBContext jaxbContext = JAXBContext.newInstance(Migration.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(migration, file);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.marshal(migration, writer);
writer.close();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (JAXBException e) {
throw new RuntimeException(e);
@@ -43,6 +43,10 @@ public class ModelBuildContext {
model.adjustDraftReferences();
}
public String normaliseTable(String baseTable) {
return constraintNaming.normaliseTable(baseTable);
}
public String primaryKeyName(String tableName) {
return maxLength(constraintNaming.primaryKeyName(tableName), 0);
}
@@ -64,8 +64,7 @@ public class ModelBuildIntersectionTable {
private void buildFkConstraints(BeanDescriptor<?> desc, TableJoinColumn[] columns, boolean direction) {
String tableName = intersectionTableJoin.getTable();
String baseTable = desc.getBaseTable();
String baseTable = ctx.normaliseTable(desc.getBaseTable());
String fkName = ctx.foreignKeyConstraintName(tableName, baseTable, ++countForeignKey);
String fkIndex = ctx.foreignKeyIndexName(tableName, baseTable, countForeignKey);
@@ -1,18 +1,27 @@
package com.avaje.ebeaninternal.api;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.bean.EntityBeanIntercept;
import com.avaje.ebeaninternal.server.core.OrmQueryRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Request for loading ManyToOne and OneToOne relationships.
*/
public class LoadBeanRequest extends LoadRequest {
private static final Logger logger = LoggerFactory.getLogger(LoadBeanRequest.class);
private final List<EntityBeanIntercept> batch;
private final LoadBeanBuffer LoadBuffer;
private final LoadBeanBuffer loadBuffer;
private final String lazyLoadProperty;
@@ -32,20 +41,28 @@ public class LoadBeanRequest extends LoadRequest {
this(LoadBuffer, parentRequest, false, null, false);
}
private LoadBeanRequest(LoadBeanBuffer LoadBuffer, OrmQueryRequest<?> parentRequest, boolean lazy, String lazyLoadProperty, boolean loadCache) {
private LoadBeanRequest(LoadBeanBuffer loadBuffer, OrmQueryRequest<?> parentRequest, boolean lazy,
String lazyLoadProperty, boolean loadCache) {
super(parentRequest, lazy);
this.LoadBuffer = LoadBuffer;
this.batch = LoadBuffer.getBatch();
this.loadBuffer = loadBuffer;
this.batch = loadBuffer.getBatch();
this.lazyLoadProperty = lazyLoadProperty;
this.loadCache = loadCache;
}
@Override
public Class<?> getBeanType() {
return loadBuffer.getBeanDescriptor().getBeanType();
}
public boolean isLoadCache() {
return loadCache;
}
public String getDescription() {
return "path:" + LoadBuffer.getFullPath() + " batch:" + batch.size();
return "path:" + loadBuffer.getFullPath() + " batch:" + batch.size();
}
/**
@@ -59,7 +76,7 @@ public class LoadBeanRequest extends LoadRequest {
* Return the load context.
*/
public LoadBeanBuffer getLoadContext() {
return LoadBuffer;
return loadBuffer;
}
/**
@@ -72,4 +89,90 @@ public class LoadBeanRequest extends LoadRequest {
public int getBatchSize() {
return getLoadContext().getBatchSize();
}
/**
* Return the list of Id values for the beans in the lazy load buffer.
*/
public List<Object> getIdList(int batchSize) {
List<Object> idList = new ArrayList<Object>(batchSize);
BeanDescriptor<?> desc = loadBuffer.getBeanDescriptor();
for (int i = 0; i < batch.size(); i++) {
EntityBeanIntercept ebi = batch.get(i);
EntityBean bean = ebi.getOwner();
idList.add(desc.getId(bean));
}
if (!idList.isEmpty()) {
int extraIds = batchSize - batch.size();
if (extraIds > 0) {
// for performance make up the Id's to the batch size
// so we get the same query (for Ebean and the db)
Object firstId = idList.get(0);
for (int i = 0; i < extraIds; i++) {
// just add the first Id again
idList.add(firstId);
}
}
}
return idList;
}
/**
* Configure the query for lazy loading execution.
*/
public void configureQuery(SpiQuery<?> query, List<Object> idList) {
query.setMode(SpiQuery.Mode.LAZYLOAD_BEAN);
query.setPersistenceContext(loadBuffer.getPersistenceContext());
String mode = isLazy() ? "+lazy" : "+query";
query.setLoadDescription(mode, getDescription());
if (isLazy()) {
// cascade the batch size (if set) for further lazy loading
query.setLazyLoadBatchSize(getBatchSize());
}
loadBuffer.configureQuery(query, lazyLoadProperty);
if (idList.size() == 1) {
query.where().idEq(idList.get(0));
} else {
query.where().idIn(idList);
}
}
/**
* Load the beans into the L2 cache if that is requested and check for load failures due to deletes.
*/
public void postLoad(List<?> list) {
Set<Object> loadedIds = new HashSet<Object>();
BeanDescriptor<?> desc = loadBuffer.getBeanDescriptor();
// collect Ids and maybe load bean cache
for (int i = 0; i < list.size(); i++) {
EntityBean loadedBean = (EntityBean) list.get(i);
loadedIds.add(desc.getId(loadedBean));
if (isLoadCache()) {
desc.cacheBeanPutData(loadedBean);
}
}
if (lazyLoadProperty != null) {
for (int i = 0; i < batch.size(); i++) {
// check if the underlying row in DB was deleted. Mark the bean as 'failed' if
// necessary but allow processing to continue until it is accessed by client code
EntityBeanIntercept ebi = batch.get(i);
Object id = desc.getId(ebi.getOwner());
if (!loadedIds.contains(id)) {
logger.info("Lazy loading unsuccessful for type:" + desc.getName() + " id:" + id + " - expecting when bean has been deleted");
ebi.setLazyLoadFailure(id);
}
}
}
}
}
@@ -1,15 +1,25 @@
package com.avaje.ebeaninternal.api;
import java.util.List;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.bean.BeanCollection;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.core.OrmQueryRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import com.avaje.ebeaninternal.server.lib.util.StringHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
/**
* Request for loading Associated Many Beans.
*/
public class LoadManyRequest extends LoadRequest {
private static final Logger logger = LoggerFactory.getLogger(LoadManyRequest.class);
private final List<BeanCollection<?>> batch;
private final LoadManyBuffer loadContext;
@@ -40,6 +50,11 @@ public class LoadManyRequest extends LoadRequest {
this.loadCache = loadCache;
}
@Override
public Class<?> getBeanType() {
return loadContext.getBeanDescriptor().getBeanType();
}
public String getDescription() {
return "path:" + loadContext.getFullPath() + " size:" + batch.size();
}
@@ -83,4 +98,98 @@ public class LoadManyRequest extends LoadRequest {
public int getBatchSize() {
return loadContext.getBatchSize();
}
private List<Object> getParentIdList(int batchSize) {
ArrayList<Object> idList = new ArrayList<Object>(batchSize);
BeanPropertyAssocMany<?> many = getMany();
for (int i = 0; i < batch.size(); i++) {
BeanCollection<?> bc = batch.get(i);
idList.add(many.getParentId(bc.getOwnerBean()));
}
int extraIds = batchSize - batch.size();
if (extraIds > 0) {
Object firstId = idList.get(0);
for (int i = 0; i < extraIds; i++) {
idList.add(firstId);
}
}
return idList;
}
private BeanPropertyAssocMany<?> getMany() {
return loadContext.getBeanProperty();
}
public SpiQuery<?> createQuery(EbeanServer server, int batchSize) {
BeanPropertyAssocMany<?> many = getMany();
SpiQuery<?> query = (SpiQuery<?>) server.createQuery(many.getTargetType());
String orderBy = many.getLazyFetchOrderBy();
if (orderBy != null) {
query.orderBy(orderBy);
}
String extraWhere = many.getExtraWhere();
if (extraWhere != null) {
// replace special ${ta} placeholder with the base table alias
// which is always t0 and add the extra where clause
String ew = StringHelper.replaceString(extraWhere, "${ta}", "t0");
query.where().raw(ew);
}
query.setLazyLoadForParents(many);
List<Object> idList = getParentIdList(batchSize);
many.addWhereParentIdIn(query, idList);
query.setPersistenceContext(loadContext.getPersistenceContext());
String mode = isLazy() ? "+lazy" : "+query";
query.setLoadDescription(mode, getDescription());
if (isLazy()) {
// cascade the batch size (if set) for further lazy loading
query.setLazyLoadBatchSize(getBatchSize());
}
// potentially changes the joins and selected properties
loadContext.configureQuery(query);
if (isOnlyIds()) {
// override to just select the Id values
query.select(many.getTargetIdProperty());
}
return query;
}
/**
* After the query execution check for empty collections and load L2 cache if desired.
*/
public void postLoad() {
BeanDescriptor<?> desc = loadContext.getBeanDescriptor();
BeanPropertyAssocMany<?> many = getMany();
// check for BeanCollection's that where never processed
// in the +query or +lazy load due to no rows (predicates)
for (int i = 0; i < batch.size(); i++) {
BeanCollection<?> bc = batch.get(i);
if (bc.checkEmptyLazyLoad()) {
if (logger.isDebugEnabled()) {
EntityBean ownerBean = bc.getOwnerBean();
Object parentId = desc.getId(ownerBean);
logger.debug("BeanCollection after lazy load was empty. type:" + ownerBean.getClass().getName() + " id:" + parentId + " owner:" + ownerBean);
}
} else if (isLoadCache()) {
Object parentId = desc.getId(bc.getOwnerBean());
desc.cacheManyPropPut(many, bc, parentId);
}
}
}
}
@@ -21,6 +21,11 @@ public abstract class LoadRequest {
this.lazy = lazy;
}
/**
* Return the associated bean type for this load request.
*/
public abstract Class<?> getBeanType();
/**
* Log the just executed secondary query with the 'root' query if 'logSecondaryQuery' is set to
* true. This is for testing purposes to confirm the secondary query executes etc.
@@ -1,153 +1,153 @@
package com.avaje.ebeaninternal.api;
import java.io.Serializable;
import java.util.Collection;
import java.util.TreeMap;
import java.util.TreeSet;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import com.avaje.ebeaninternal.server.el.ElPropertyDeploy;
import com.avaje.ebeaninternal.server.query.SplitName;
import com.avaje.ebeaninternal.server.query.SqlJoinType;
import java.io.Serializable;
import java.util.Collection;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* Holds the joins needs to support the many where predicates.
* These joins are independent of any 'fetch' joins on the many.
*/
public class ManyWhereJoins implements Serializable {
private static final long serialVersionUID = -6490181101871795417L;
private final TreeMap<String,PropertyJoin> joins = new TreeMap<String,PropertyJoin>();
private static final long serialVersionUID = -6490181101871795417L;
private StringBuilder formulaProperties = new StringBuilder();
private boolean formulaWithJoin;
private final TreeMap<String, PropertyJoin> joins = new TreeMap<String, PropertyJoin>();
private StringBuilder formulaProperties = new StringBuilder();
private boolean formulaWithJoin;
/**
* 'Mode' indicating that joins added while this is true are required to be outer joins.
*/
private boolean requireOuterJoins;
/**
* Return the current 'mode' indicating if outer joins are currently required or not.
*/
public boolean isRequireOuterJoins() {
return requireOuterJoins;
}
private boolean requireOuterJoins;
/**
* Set the 'mode' to be that joins added are required to be outer joins.
* This is set during the evaluation of disjunction predicates.
*/
public void setRequireOuterJoins(boolean requireOuterJoins) {
this.requireOuterJoins = requireOuterJoins;
}
/**
* Return the current 'mode' indicating if outer joins are currently required or not.
*/
public boolean isRequireOuterJoins() {
return requireOuterJoins;
}
/**
* Add a many where join.
*/
public void add(ElPropertyDeploy elProp) {
String join = elProp.getElPrefix();
BeanProperty p = elProp.getBeanProperty();
if (p instanceof BeanPropertyAssocMany<?>){
join = addManyToJoin(join, p.getName());
}
if (join != null){
addJoin(join);
if (p != null) {
String secondaryTableJoinPrefix = p.getSecondaryTableJoinPrefix();
if (secondaryTableJoinPrefix != null) {
addJoin(join+"."+secondaryTableJoinPrefix);
}
}
addParentJoins(join);
}
}
/**
* For 'many' properties we also need to add the name of the
* many property to get the full logical name of the join.
*/
private String addManyToJoin(String join, String manyPropName){
if (join == null){
return manyPropName;
} else {
return join+"."+manyPropName;
}
}
private void addParentJoins(String join) {
String[] split = SplitName.split(join);
if (split[0] != null){
addJoin(split[0]);
addParentJoins(split[0]);
}
}
/**
* Set the 'mode' to be that joins added are required to be outer joins.
* This is set during the evaluation of disjunction predicates.
*/
public void setRequireOuterJoins(boolean requireOuterJoins) {
this.requireOuterJoins = requireOuterJoins;
}
private void addJoin(String property) {
SqlJoinType joinType = (requireOuterJoins) ? SqlJoinType.OUTER: SqlJoinType.INNER;
joins.put(property, new PropertyJoin(property, joinType));
}
/**
* Return true if there are no extra many where joins.
*/
public boolean isEmpty() {
return joins.isEmpty();
}
/**
* Return the set of many where joins.
*/
public Collection<PropertyJoin> getPropertyJoins() {
return joins.values();
}
/**
* Add a many where join.
*/
public void add(ElPropertyDeploy elProp) {
/**
* Return the set of property names for the many where joins.
*/
public TreeSet<String> getPropertyNames() {
TreeSet<String> propertyNames = new TreeSet<String>();
for (PropertyJoin join : joins.values()) {
propertyNames.add(join.getProperty());
String join = elProp.getElPrefix();
BeanProperty p = elProp.getBeanProperty();
if (p instanceof BeanPropertyAssocMany<?>) {
join = addManyToJoin(join, p.getName());
}
if (join != null) {
addJoin(join);
if (p != null) {
String secondaryTableJoinPrefix = p.getSecondaryTableJoinPrefix();
if (secondaryTableJoinPrefix != null) {
addJoin(join + "." + secondaryTableJoinPrefix);
}
}
return propertyNames;
addParentJoins(join);
}
}
/**
* In findRowCount query found a formula property with a join clause so building a select clause
* specifically for the findRowCount query.
*/
public void addFormulaWithJoin(String propertyName) {
if (formulaWithJoin) {
formulaProperties.append(",");
} else {
formulaProperties = new StringBuilder();
formulaWithJoin = true;
}
formulaProperties.append(propertyName);
/**
* For 'many' properties we also need to add the name of the
* many property to get the full logical name of the join.
*/
private String addManyToJoin(String join, String manyPropName) {
if (join == null) {
return manyPropName;
} else {
return join + "." + manyPropName;
}
public boolean isHasMany() {
return formulaWithJoin || !joins.isEmpty();
}
private void addParentJoins(String join) {
String[] split = SplitName.split(join);
if (split[0] != null) {
addJoin(split[0]);
addParentJoins(split[0]);
}
/**
* Return true if the findRowCount query just needs the id property in the select clause.
*/
public boolean isSelectId() {
return !formulaWithJoin;
}
private void addJoin(String property) {
SqlJoinType joinType = (requireOuterJoins) ? SqlJoinType.OUTER : SqlJoinType.INNER;
joins.put(property, new PropertyJoin(property, joinType));
}
/**
* Return true if there are no extra many where joins.
*/
public boolean isEmpty() {
return joins.isEmpty();
}
/**
* Return the set of many where joins.
*/
public Collection<PropertyJoin> getPropertyJoins() {
return joins.values();
}
/**
* Return the set of property names for the many where joins.
*/
public TreeSet<String> getPropertyNames() {
TreeSet<String> propertyNames = new TreeSet<String>();
for (PropertyJoin join : joins.values()) {
propertyNames.add(join.getProperty());
}
/**
* Return the formula properties to build the select clause for a findRowCount query.
*/
public String getFormulaProperties() {
return formulaProperties.toString();
return propertyNames;
}
/**
* In findRowCount query found a formula property with a join clause so building a select clause
* specifically for the findRowCount query.
*/
public void addFormulaWithJoin(String propertyName) {
if (formulaWithJoin) {
formulaProperties.append(",");
} else {
formulaProperties = new StringBuilder();
formulaWithJoin = true;
}
formulaProperties.append(propertyName);
}
public boolean isHasMany() {
return formulaWithJoin || !joins.isEmpty();
}
/**
* Return true if the findRowCount query just needs the id property in the select clause.
*/
public boolean isSelectId() {
return !formulaWithJoin;
}
/**
* Return the formula properties to build the select clause for a findRowCount query.
*/
public String getFormulaProperties() {
return formulaProperties.toString();
}
}
@@ -0,0 +1,61 @@
package com.avaje.ebeaninternal.server.autotune.service;
import com.avaje.ebeaninternal.server.autotune.model.Autotune;
import java.util.Collection;
/**
* Event where all tuned query information is collected.
* <p>
* This is for writing the "all" file on shutdown when using runtime tuning.
* </p>
*/
public class AutoTuneAllCollection {
final Autotune document = new Autotune();
final BaseQueryTuner queryTuner;
/**
* Construct to collect/report all tuned queries.
*/
public AutoTuneAllCollection(BaseQueryTuner queryTuner) {
this.queryTuner = queryTuner;
loadAllTuned();
}
/**
* Return the number of origin elements in the document.
*/
public int size() {
return document.getOrigin().size();
}
/**
* Return the Autotune document object.
*/
public Autotune getDocument() {
return document;
}
/**
* Write the document as an xml file.
*/
public void writeFile(String filePrefix) {
AutoTuneXmlWriter writer = new AutoTuneXmlWriter();
writer.write(document, filePrefix);
}
/**
* Loads all the existing query tuning into the document.
*/
private void loadAllTuned() {
Collection<TunedQueryInfo> all = queryTuner.getAll();
for (TunedQueryInfo tuned: all) {
document.getOrigin().add(tuned.getOrigin());
}
}
}
@@ -0,0 +1,162 @@
package com.avaje.ebeaninternal.server.autotune.service;
import com.avaje.ebean.bean.ObjectGraphOrigin;
import com.avaje.ebeaninternal.server.autotune.AutoTuneCollection;
import com.avaje.ebeaninternal.server.autotune.model.Autotune;
import com.avaje.ebeaninternal.server.autotune.model.Origin;
import com.avaje.ebeaninternal.server.autotune.model.ProfileDiff;
import com.avaje.ebeaninternal.server.autotune.model.ProfileNew;
import com.avaje.ebeaninternal.server.querydefn.OrmQueryDetail;
/**
* Event where profiling information is collected and processed for differences
* relative to the current query tuning.
*/
public class AutoTuneDiffCollection {
final Autotune document = new Autotune();
final AutoTuneCollection profiling;
final BaseQueryTuner queryTuner;
final boolean updateTuning;
int newCount;
int diffCount;
/**
* Construct to collect/report the new/diff query tuning entries.
*/
public AutoTuneDiffCollection(AutoTuneCollection profiling, BaseQueryTuner queryTuner, boolean updateTuning) {
this.profiling = profiling;
this.queryTuner = queryTuner;
this.updateTuning = updateTuning;
}
/**
* Return true if there are no new or diff entries.
*/
public boolean isEmpty() {
return newCount == 0 && diffCount == 0;
}
/**
* Return the underlying Autotune document object.
*/
public Autotune getDocument() {
return document;
}
/**
* Return the number of diff entries.
*/
public int getDiffCount() {
return diffCount;
}
/**
* Return the number of new entries.
*/
public int getNewCount() {
return newCount;
}
/**
* Return the total new and diff entries.
*/
public int getChangeCount() {
return newCount + diffCount;
}
/**
* Write the underlying document as an xml file.
*/
public void writeFile(String filePrefix) {
AutoTuneXmlWriter writer = new AutoTuneXmlWriter();
writer.write(document, filePrefix);
}
/**
* Process checking profiling entries against existing query tuning.
*/
public void process() {
for (AutoTuneCollection.Entry entry : profiling.getEntries()) {
addToDocument(entry);
}
}
/**
* Check if the entry is new or diff and add as necessary.
*/
private void addToDocument(AutoTuneCollection.Entry entry) {
ObjectGraphOrigin point = entry.getOrigin();
OrmQueryDetail profileDetail = entry.getDetail();
// compare with the existing query tuning entry
OrmQueryDetail tuneDetail = queryTuner.get(point.getKey());
if (tuneDetail == null) {
addToDocumentNewEntry(entry, point);
} else if (!tuneDetail.isAutoTuneEqual(profileDetail)) {
addToDocumentDiffEntry(entry, point, tuneDetail);
}
}
/**
* Add as a diff entry.
*/
private void addToDocumentDiffEntry(AutoTuneCollection.Entry entry, ObjectGraphOrigin point, OrmQueryDetail tuneDetail) {
diffCount++;
Origin origin = createOrigin(entry, point, tuneDetail.toString());
ProfileDiff diff = document.getProfileDiff();
if (diff == null) {
diff = new ProfileDiff();
document.setProfileDiff(diff);
}
diff.getOrigin().add(origin);
}
/**
* Add as a "new" entry.
*/
private void addToDocumentNewEntry(AutoTuneCollection.Entry entry, ObjectGraphOrigin point) {
newCount++;
ProfileNew profileNew = document.getProfileNew();
if (profileNew == null) {
profileNew = new ProfileNew();
document.setProfileNew(profileNew);
}
Origin origin = createOrigin(entry, point, entry.getOriginalQuery());
profileNew.getOrigin().add(origin);
}
/**
* Create the XML Origin bean for the given entry and ObjectGraphOrigin.
*/
private Origin createOrigin(AutoTuneCollection.Entry entry, ObjectGraphOrigin point, String query) {
Origin origin = new Origin();
origin.setKey(point.getKey());
origin.setBeanType(point.getBeanType());
origin.setDetail(entry.getDetail().toString());
origin.setCallStack(point.getCallStack().description("\n"));
origin.setOriginal(query);
if (updateTuning) {
queryTuner.put(origin);
}
return origin;
}
}
@@ -7,12 +7,29 @@ import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Simple writer for output of the AutoTune Profiling as an XML document.
*/
public class AutoTuneXmlWriter {
/**
* Write the document as xml file with the given prefix.
*/
public void write(Autotune document, String filePrefix) {
SortAutoTuneDocument.sort(document);
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss");
String now = df.format(new Date());
// write the file with serverName and now suffix as we can output the profiling many times
File file = new File(filePrefix + "-" + now + ".xml");
write(document, file);
}
/**
* Write Profiling to a file as xml.
*/
@@ -7,9 +7,11 @@ import com.avaje.ebean.config.AutoTuneMode;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.server.autotune.ProfilingListener;
import com.avaje.ebeaninternal.server.autotune.model.Origin;
import com.avaje.ebeaninternal.server.querydefn.OrmQueryDetail;
import javax.persistence.PersistenceException;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@@ -48,6 +50,21 @@ public class BaseQueryTuner {
this.skipAll = !queryTuning && !profiling;
}
/**
* Return all the current tuned query entries.
*/
public Collection<TunedQueryInfo> getAll() {
return tunedQueryInfoMap.values();
}
/**
* Put a query tuning entry.
*/
public void put(Origin origin) {
tunedQueryInfoMap.put(origin.getKey(), new TunedQueryInfo(origin));
}
/**
* Load the tuned query information.
*/
@@ -1,6 +1,5 @@
package com.avaje.ebeaninternal.server.autotune.service;
import com.avaje.ebean.bean.ObjectGraphOrigin;
import com.avaje.ebean.config.AutoTuneConfig;
import com.avaje.ebean.config.ServerConfig;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
@@ -9,24 +8,11 @@ import com.avaje.ebeaninternal.server.autotune.AutoTuneCollection;
import com.avaje.ebeaninternal.server.autotune.AutoTuneService;
import com.avaje.ebeaninternal.server.autotune.model.Autotune;
import com.avaje.ebeaninternal.server.autotune.model.Origin;
import com.avaje.ebeaninternal.server.autotune.model.ProfileDiff;
import com.avaje.ebeaninternal.server.autotune.model.ProfileEmpty;
import com.avaje.ebeaninternal.server.autotune.model.ProfileNew;
import com.avaje.ebeaninternal.server.querydefn.OrmQueryDetail;
import com.avaje.ebeaninternal.server.querydefn.OrmQueryDetailParser;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.TimeUnit;
/**
* Implementation of the AutoTuneService which is comprised of profiling and query tuning.
@@ -35,9 +21,13 @@ public class DefaultAutoTuneService implements AutoTuneService {
private static final Logger logger = LoggerFactory.getLogger(DefaultAutoTuneService.class);
private final SpiEbeanServer server;
private final long defaultGarbageCollectionWait;
private final boolean skipCollectionOnShutdown;
private final boolean skipGarbageCollectionOnShutdown;
private final boolean skipProfileReportingOnShutdown;
private final BaseQueryTuner queryTuner;
@@ -53,18 +43,25 @@ public class DefaultAutoTuneService implements AutoTuneService {
private final String serverName;
private final int profilingUpdateFrequency;
private long runtimeChangeCount;
public DefaultAutoTuneService(SpiEbeanServer server, ServerConfig serverConfig) {
AutoTuneConfig config = serverConfig.getAutoTuneConfig();
this.server = server;
this.queryTuning = config.isQueryTuning();
this.profiling = config.isProfiling();
this.tuningFile = config.getQueryTuningFile();
this.profilingFile = config.getProfilingFile();
this.profilingUpdateFrequency = config.getProfilingUpdateFrequency();
this.serverName = server.getName();
this.profileManager = new ProfileManager(config, server);
this.queryTuner = new BaseQueryTuner(config, server, profileManager);
this.skipCollectionOnShutdown = config.isSkipCollectionOnShutdown();
this.skipGarbageCollectionOnShutdown = config.isSkipGarbageCollectionOnShutdown();
this.skipProfileReportingOnShutdown = config.isSkipProfileReportingOnShutdown();
this.defaultGarbageCollectionWait = (long) config.getGarbageCollectionWait();
}
@@ -75,169 +72,122 @@ public class DefaultAutoTuneService implements AutoTuneService {
public void startup() {
if (queryTuning) {
File file = new File(tuningFile);
if (!file.exists()) {
logger.warn("AutoTune file {} not found - no automatic tuning will be applied", file.getAbsolutePath());
} else {
AutoTuneXmlReader reader = new AutoTuneXmlReader();
Autotune profiling = reader.read(file);
logger.info("AutoTune loading {} tuning entries", profiling.getOrigin().size());
for (Origin origin : profiling.getOrigin()) {
queryTuner.load(origin.getKey(), createTunedQueryInfo(origin));
}
loadTuningFile();
if (isRuntimeTuningUpdates()) {
// periodically gather and update query tuning
server.getBackgroundExecutor().executePeriodically(new ProfilingUpdate(), profilingUpdateFrequency, TimeUnit.SECONDS);
}
}
}
@NotNull
private TunedQueryInfo createTunedQueryInfo(Origin origin) {
OrmQueryDetail detail = new OrmQueryDetailParser(origin.getDetail()).parse();
return new TunedQueryInfo(detail);
/**
* Return true if the tuning should update periodically at runtime.
*/
private boolean isRuntimeTuningUpdates() {
return profilingUpdateFrequency > 0;
}
private void saveProfiling(boolean reset) {
private class ProfilingUpdate implements Runnable {
Autotune document = new Autotune();
AutoTuneCollection autoTuneCollection = profileManager.profilingCollection(reset);
List<AutoTuneCollection.Entry> entries = autoTuneCollection.getEntries();
// count "new" and "diff" profiling entries
AtomicInteger newCounter = new AtomicInteger();
AtomicInteger diffCounter = new AtomicInteger();
Set<String> profileKeys = new HashSet<String>();
for (AutoTuneCollection.Entry entry : entries) {
saveProfilingEntry(document, entry, newCounter, diffCounter);
profileKeys.add(entry.getOrigin().getKey());
@Override
public void run() {
runtimeTuningUpdate();
}
}
// report the origin keys that we didn't collect any profiling on
Set<String> tunerKeys = queryTuner.keySet();
for (String tuneKey : tunerKeys) {
if (!profileKeys.contains(tuneKey)) {
ProfileEmpty profileEmpty = document.getProfileEmpty();
if (profileEmpty == null) {
profileEmpty = new ProfileEmpty();
document.setProfileEmpty(profileEmpty);
}
Origin emptyOrigin = new Origin();
emptyOrigin.setKey(tuneKey);
profileEmpty.getOrigin().add(emptyOrigin);
}
}
int totalNew = newCounter.get();
int totalDiff = diffCounter.get();
if (totalNew == 0 && totalDiff == 0) {
logger.info("No new or diff entries for profiling server:{}", serverName);
/**
* Load tuning information from an existing tuning file.
*/
private void loadTuningFile() {
File file = new File(tuningFile);
if (!file.exists()) {
logger.warn("AutoTune file {} not found - no initial automatic query tuning", file.getAbsolutePath());
} else {
sortDocument(document);
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss");
String now = df.format(new Date());
// write the file with serverName and now suffix as we can output the profiling many times
File file = new File(profilingFile + "-" + serverName + "-" + now + ".xml");
AutoTuneXmlWriter writer = new AutoTuneXmlWriter();
writer.write(document, file);
logger.info("writing new:{} diff:{} profiling entries for server:{}", totalNew, totalDiff, serverName);
}
}
/**
* Set the diff and new entries by bean type followed by key.
*/
private void sortDocument(Autotune document) {
ProfileDiff profileDiff = document.getProfileDiff();
if (profileDiff != null) {
Collections.sort(profileDiff.getOrigin(), new OriginNameKeySort());
}
ProfileNew profileNew = document.getProfileNew();
if (profileNew != null) {
Collections.sort(profileNew.getOrigin(), new OriginNameKeySort());
}
ProfileEmpty profileEmpty = document.getProfileEmpty();
if (profileEmpty != null) {
Collections.sort(profileEmpty.getOrigin(), new OriginKeySort());
}
}
/**
* Comparator sort by bean type then key.
*/
class OriginNameKeySort implements Comparator<Origin> {
@Override
public int compare(Origin o1, Origin o2) {
int comp = o1.getBeanType().compareTo(o2.getBeanType());
if (comp == 0) {
comp = o1.getKey().compareTo(o2.getKey());
AutoTuneXmlReader reader = new AutoTuneXmlReader();
Autotune profiling = reader.read(file);
logger.info("AutoTune loading {} tuning entries", profiling.getOrigin().size());
for (Origin origin : profiling.getOrigin()) {
queryTuner.put(origin);
}
return comp;
}
}
/**
* Comparator sort by bean type then key.
* Collect profiling, check for new/diff to existing tuning and apply changes.
*/
class OriginKeySort implements Comparator<Origin> {
private void runtimeTuningUpdate() {
@Override
public int compare(Origin o1, Origin o2) {
return o1.getKey().compareTo(o2.getKey());
synchronized (this) {
try {
long start = System.currentTimeMillis();
AutoTuneCollection profiling = profileManager.profilingCollection(false);
AutoTuneDiffCollection event = new AutoTuneDiffCollection(profiling, queryTuner, true);
event.process();
if (event.isEmpty()) {
long exeMillis = System.currentTimeMillis() - start;
logger.debug("No query tuning updates for server:{} executionMillis:{}", serverName, exeMillis);
} else {
// report the query tuning changes that have been made
runtimeChangeCount += event.getChangeCount();
event.writeFile(profilingFile + "-" + serverName + "-update");
long exeMillis = System.currentTimeMillis() - start;
logger.info("query tuning updates - new:{} diff:{} for server:{} executionMillis:{}", event.getNewCount(), event.getDiffCount(), serverName, exeMillis);
}
} catch (Throwable e) {
logger.error("Error collecting or applying automatic query tuning", e);
}
}
}
private void saveProfilingEntry(Autotune document, AutoTuneCollection.Entry entry, AtomicInteger newCount, AtomicInteger diffCount) {
private void saveProfilingOnShutdown(boolean reset) {
ObjectGraphOrigin point = entry.getOrigin();
OrmQueryDetail profileDetail = entry.getDetail();
synchronized (this) {
if (isRuntimeTuningUpdates()) {
runtimeTuningUpdate();
outputAllTuning();
// compare with the existing query tuning entry
OrmQueryDetail tuneDetail = queryTuner.get(point.getKey());
if (tuneDetail == null) {
// New entry
newCount.incrementAndGet();
ProfileNew profileNew = document.getProfileNew();
if (profileNew == null) {
profileNew = new ProfileNew();
document.setProfileNew(profileNew);
} else {
AutoTuneCollection profiling = profileManager.profilingCollection(reset);
AutoTuneDiffCollection event = new AutoTuneDiffCollection(profiling, queryTuner, false);
event.process();
if (event.isEmpty()) {
logger.info("No new or diff entries for profiling server:{}", serverName);
} else {
event.writeFile(profilingFile + "-" + serverName);
logger.info("writing new:{} diff:{} profiling entries for server:{}", event.getNewCount(), event.getDiffCount(), serverName);
}
}
Origin origin = createOrigin(entry, point);
origin.setOriginal(entry.getOriginalQuery());
profileNew.getOrigin().add(origin);
} else if (!tuneDetail.isAutoTuneEqual(profileDetail)) {
// Diff entry
diffCount.incrementAndGet();
Origin origin = createOrigin(entry, point);
origin.setOriginal(tuneDetail.toString());
ProfileDiff diff = document.getProfileDiff();
if (diff == null) {
diff = new ProfileDiff();
document.setProfileDiff(diff);
}
diff.getOrigin().add(origin);
}
}
/**
* Create the XML Origin bean for the given entry and ObjectGraphOrigin.
* Output all the query tuning (the "all" file).
* <p>
* This is the originally loaded tuning plus any tuning changes picked up and applied at runtime.
* </p>
* <p>
* This "all" file can be used as the next "ebean-autotune.xml" file.
* </p>
*/
@NotNull
private Origin createOrigin(AutoTuneCollection.Entry entry, ObjectGraphOrigin point) {
Origin origin = new Origin();
origin.setKey(point.getKey());
origin.setBeanType(point.getBeanType());
origin.setDetail(entry.getDetail().toString());
origin.setCallStack(point.getCallStack().description("\n"));
return origin;
private void outputAllTuning() {
if (runtimeChangeCount == 0) {
logger.info("no runtime query tuning changes for server:{}", serverName);
} else {
AutoTuneAllCollection event = new AutoTuneAllCollection(queryTuner);
int size = event.size();
event.writeFile(profilingFile + "-" + serverName + "-all");
logger.info("query tuning detected [{}] changes, writing all [{}] tuning entries for server:{}", runtimeChangeCount, size, serverName);
}
}
/**
@@ -250,12 +200,29 @@ public class DefaultAutoTuneService implements AutoTuneService {
*/
@Override
public void shutdown() {
if (profiling && !skipCollectionOnShutdown) {
collectProfiling(-1);
saveProfiling(false);
if (profiling) {
if (!skipGarbageCollectionOnShutdown && !skipProfileReportingOnShutdown) {
// trigger GC to update profiling information on recently executed queries
collectProfiling(-1);
}
if (!skipProfileReportingOnShutdown) {
saveProfilingOnShutdown(false);
}
}
}
/**
* Output the profiling.
* <p>
* When profiling updates are applied to tuning at runtime this reports all tuning and profiling combined.
* When profiling is not applied at runtime then this reports the diff report with new and diff entries relative
* to the existing tuning.
* </p>
*/
public void reportProfiling() {
saveProfilingOnShutdown(false);
}
/**
* Ask for a System.gc() so that we gather node usage information.
* <p>
@@ -118,7 +118,6 @@ public class ProfileManager implements ProfilingListener {
AutoTuneCollection req = new AutoTuneCollection();
for (ProfileOrigin origin : profileMap.values()) {
BeanDescriptor<?> desc = server.getBeanDescriptorById(origin.getOrigin().getBeanType());
if (desc != null) {
origin.profilingCollection(desc, req, reset);
@@ -0,0 +1,71 @@
package com.avaje.ebeaninternal.server.autotune.service;
import com.avaje.ebeaninternal.server.autotune.model.Autotune;
import com.avaje.ebeaninternal.server.autotune.model.Origin;
import com.avaje.ebeaninternal.server.autotune.model.ProfileDiff;
import com.avaje.ebeaninternal.server.autotune.model.ProfileEmpty;
import com.avaje.ebeaninternal.server.autotune.model.ProfileNew;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* Sorts Autotune document by
*/
public class SortAutoTuneDocument {
/**
* Set the diff and new entries by bean type followed by key.
*/
public static void sort(Autotune document) {
ProfileDiff profileDiff = document.getProfileDiff();
if (profileDiff != null) {
Collections.sort(profileDiff.getOrigin(), NAME_KEY_SORT);
}
ProfileNew profileNew = document.getProfileNew();
if (profileNew != null) {
Collections.sort(profileNew.getOrigin(), NAME_KEY_SORT);
}
ProfileEmpty profileEmpty = document.getProfileEmpty();
if (profileEmpty != null) {
Collections.sort(profileEmpty.getOrigin(), KEY_SORT);
}
List<Origin> origins = document.getOrigin();
if (!origins.isEmpty()) {
Collections.sort(origins, NAME_KEY_SORT);
}
}
private static final OriginNameKeySort NAME_KEY_SORT = new OriginNameKeySort();
private static final OriginKeySort KEY_SORT = new OriginKeySort();
/**
* Comparator sort by bean type then key.
*/
private static class OriginNameKeySort implements Comparator<Origin> {
@Override
public int compare(Origin o1, Origin o2) {
int comp = o1.getBeanType().compareTo(o2.getBeanType());
if (comp == 0) {
comp = o1.getKey().compareTo(o2.getKey());
}
return comp;
}
}
/**
* Comparator sort by bean type then key.
*/
private static class OriginKeySort implements Comparator<Origin> {
@Override
public int compare(Origin o1, Origin o2) {
return o1.getKey().compareTo(o2.getKey());
}
}
}
@@ -1,7 +1,9 @@
package com.avaje.ebeaninternal.server.autotune.service;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.server.autotune.model.Origin;
import com.avaje.ebeaninternal.server.querydefn.OrmQueryDetail;
import com.avaje.ebeaninternal.server.querydefn.OrmQueryDetailParser;
import java.io.Serializable;
@@ -11,10 +13,20 @@ import java.io.Serializable;
*/
public class TunedQueryInfo implements Serializable {
private final Origin origin;
private final OrmQueryDetail tunedDetail;
public TunedQueryInfo(OrmQueryDetail tunedDetail) {
this.tunedDetail = tunedDetail;
public TunedQueryInfo(Origin origin) {
this.origin = origin;
this.tunedDetail = new OrmQueryDetailParser(origin.getDetail()).parse();
}
/**
* Return the origin entry (includes call stack and bean type).
*/
public Origin getOrigin() {
return origin;
}
/**
@@ -6,23 +6,20 @@ import com.avaje.ebean.bean.BeanCollection;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.bean.EntityBeanIntercept;
import com.avaje.ebean.bean.PersistenceContext;
import com.avaje.ebeaninternal.api.LoadBeanBuffer;
import com.avaje.ebeaninternal.api.LoadBeanRequest;
import com.avaje.ebeaninternal.api.LoadManyBuffer;
import com.avaje.ebeaninternal.api.LoadManyRequest;
import com.avaje.ebeaninternal.api.LoadRequest;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.api.SpiQuery.Mode;
import com.avaje.ebeaninternal.api.SpiTransaction;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor.EntityType;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import com.avaje.ebeaninternal.server.lib.util.StringHelper;
import com.avaje.ebeaninternal.server.transaction.DefaultPersistenceContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.persistence.EntityNotFoundException;
import java.util.ArrayList;
import java.util.List;
/**
@@ -86,89 +83,11 @@ public class DefaultBeanLoader {
int batchSize = getBatchSize(batch.size());
LoadManyBuffer ctx = loadRequest.getLoadContext();
BeanPropertyAssocMany<?> many = ctx.getBeanProperty();
SpiQuery<?> query = loadRequest.createQuery(server, batchSize);
PersistenceContext pc = ctx.getPersistenceContext();
executeQuery(loadRequest, query);
ArrayList<Object> idList = new ArrayList<Object>(batchSize);
for (int i = 0; i < batch.size(); i++) {
BeanCollection<?> bc = batch.get(i);
EntityBean ownerBean = bc.getOwnerBean();
Object id = many.getParentId(ownerBean);
idList.add(id);
}
int extraIds = batchSize - batch.size();
if (extraIds > 0) {
Object firstId = idList.get(0);
for (int i = 0; i < extraIds; i++) {
idList.add(firstId);
}
}
BeanDescriptor<?> desc = ctx.getBeanDescriptor();
SpiQuery<?> query = (SpiQuery<?>) server.createQuery(many.getTargetType());
String orderBy = many.getLazyFetchOrderBy();
if (orderBy != null) {
query.orderBy(orderBy);
}
String extraWhere = many.getExtraWhere();
if (extraWhere != null) {
// replace special ${ta} placeholder with the base table alias
// which is always t0 and add the extra where clause
String ew = StringHelper.replaceString(extraWhere, "${ta}", "t0");
query.where().raw(ew);
}
query.setLazyLoadForParents(many);
many.addWhereParentIdIn(query, idList);
query.setPersistenceContext(pc);
String mode = loadRequest.isLazy() ? "+lazy" : "+query";
query.setLoadDescription(mode, loadRequest.getDescription());
if (loadRequest.isLazy()) {
// cascade the batch size (if set) for further lazy loading
query.setLazyLoadBatchSize(loadRequest.getBatchSize());
}
// potentially changes the joins and selected properties
ctx.configureQuery(query);
if (loadRequest.isOnlyIds()) {
// override to just select the Id values
query.select(many.getTargetIdProperty());
}
if (onIterateUseExtraTxn && loadRequest.isParentFindIterate()) {
// MySql - we need a different transaction to execute the secondary query
SpiTransaction extraTxn = server.createQueryTransaction();
try {
server.findList(query, extraTxn);
} finally {
extraTxn.end();
}
} else {
server.findList(query, loadRequest.getTransaction());
}
// check for BeanCollection's that where never processed
// in the +query or +lazy load due to no rows (predicates)
for (int i = 0; i < batch.size(); i++) {
BeanCollection<?> bc = batch.get(i);
if (bc.checkEmptyLazyLoad()) {
if (logger.isDebugEnabled()) {
logger.debug("BeanCollection after load was empty. Owner:" + batch.get(i).getOwnerBean());
}
} else if (loadRequest.isLoadCache()) {
Object parentId = desc.getId(bc.getOwnerBean());
desc.cacheManyPropPut(many, bc, parentId);
}
}
loadRequest.postLoad();
// log the query (for testing secondary queries)
loadRequest.logSecondaryQuery(query);
@@ -274,96 +193,43 @@ public class DefaultBeanLoader {
public void loadBean(LoadBeanRequest loadRequest) {
List<EntityBeanIntercept> batch = loadRequest.getBatch();
if (batch.isEmpty()) {
throw new RuntimeException("Nothing in batch?");
}
int batchSize = getBatchSize(batch.size());
LoadBeanBuffer ctx = loadRequest.getLoadContext();
BeanDescriptor<?> desc = ctx.getBeanDescriptor();
Class<?> beanType = desc.getBeanType();
EntityBeanIntercept[] ebis = batch.toArray(new EntityBeanIntercept[batch.size()]);
ArrayList<Object> idList = new ArrayList<Object>(batchSize);
for (int i = 0; i < batch.size(); i++) {
EntityBeanIntercept ebi = batch.get(i);
EntityBean bean = ebi.getOwner();
Object id = desc.getId(bean);
idList.add(id);
}
List<Object> idList = loadRequest.getIdList(batchSize);
if (idList.isEmpty()) {
// everything was loaded from cache
return;
}
int extraIds = batchSize - batch.size();
if (extraIds > 0) {
// for performance make up the Id's to the batch size
// so we get the same query (for Ebean and the db)
Object firstId = idList.get(0);
for (int i = 0; i < extraIds; i++) {
// just add the first Id again
idList.add(firstId);
}
}
SpiQuery<?> query = (SpiQuery<?>) server.createQuery(loadRequest.getBeanType());
loadRequest.configureQuery(query, idList);
PersistenceContext persistenceContext = ctx.getPersistenceContext();
List<?> list = executeQuery(loadRequest, query);
loadRequest.postLoad(list);
SpiQuery<?> query = (SpiQuery<?>) server.createQuery(beanType);
// log the query (for testing secondary queries)
loadRequest.logSecondaryQuery(query);
}
query.setMode(Mode.LAZYLOAD_BEAN);
query.setPersistenceContext(persistenceContext);
String mode = loadRequest.isLazy() ? "+lazy" : "+query";
query.setLoadDescription(mode, loadRequest.getDescription());
if (loadRequest.isLazy()) {
// cascade the batch size (if set) for further lazy loading
query.setLazyLoadBatchSize(loadRequest.getBatchSize());
}
ctx.configureQuery(query, loadRequest.getLazyLoadProperty());
// make sure the query doesn't use the cache
// query.setUseCache(false);
if (idList.size() == 1) {
query.where().idEq(idList.get(0));
} else {
query.where().idIn(idList);
}
List<?> list;
/**
* Execute the lazy load query taking into account MySql transaction oddness.
*/
private List<?> executeQuery(LoadRequest loadRequest, SpiQuery<?> query) {
if (onIterateUseExtraTxn && loadRequest.isParentFindIterate()) {
// MySql - we need a different transaction to execute the secondary query
SpiTransaction extraTxn = server.createQueryTransaction();
try {
list = server.findList(query, extraTxn);
return server.findList(query, extraTxn);
} finally {
extraTxn.end();
}
} else {
list = server.findList(query, loadRequest.getTransaction());
return server.findList(query, loadRequest.getTransaction());
}
if (loadRequest.isLoadCache()) {
for (int i = 0; i < list.size(); i++) {
desc.cacheBeanPutData((EntityBean) list.get(i));
}
}
for (int i = 0; i < ebis.length; i++) {
// Check if the underlying row in DB was deleted. Mark this bean as 'failed' if
// necessary but allow processing to continue until it is accessed by client code
ebis[i].checkLazyLoadFailure();
}
// log the query (for testing secondary queries)
loadRequest.logSecondaryQuery(query);
}
public void refresh(EntityBean bean) {
@@ -2316,7 +2316,7 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
*/
public void appendOrderById(SpiQuery<T> query) {
if (idProperty != null) {
if (idProperty != null && !idProperty.isEmbedded()) {
OrderBy<T> orderBy = query.getOrderBy();
if (orderBy == null || orderBy.isEmpty()) {
query.order().asc(idProperty.getName());
@@ -85,8 +85,8 @@ public abstract class BeanPropertyAssoc<T> extends BeanProperty {
public void initialise() {
// this *MUST* execute after the BeanDescriptor is
// put into the map to stop infinite recursion
if (!isTransient){
targetDescriptor = descriptor.getBeanDescriptor(targetType);
targetDescriptor = descriptor.getBeanDescriptor(targetType);
if (!isTransient){
targetIdBinder = targetDescriptor.getIdBinder();
targetInheritInfo = targetDescriptor.getInheritInfo();
saveRecurseSkippable = targetDescriptor.isSaveRecurseSkippable();
@@ -887,7 +887,7 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> {
if (help != null) {
help.jsonWrite(ctx, name, value, include != null);
} else {
if (isTransient) {
if (isTransient && targetDescriptor == null) {
ctx.writeValueUsingObjectMapper(name, value);
} else {
Collection<?> collection = (Collection<?>)value;
@@ -101,14 +101,23 @@ public class BeanPropertyAssocOne<T> extends BeanPropertyAssoc<T> {
// no imported or exported information
} else if (!oneToOneExported) {
importedId = createImportedId(this, targetDescriptor, tableJoin);
if (importedId.isScalar()) {
// limit JoinColumn mapping to the @Id / primary key
TableJoinColumn[] columns = tableJoin.columns();
String foreignJoinColumn = columns[0].getForeignDbColumn();
String foreignIdColumn = targetDescriptor.getIdProperty().getDbColumn();
if (!foreignJoinColumn.equalsIgnoreCase(foreignIdColumn)) {
throw new PersistenceException("Mapping limitation - @JoinColumn on " + getFullBeanName() + " needs to map to a primary key as per Issue #529 "
+ " - joining to " + foreignJoinColumn + " and not " + foreignIdColumn);
}
}
} else {
exportedProperties = createExported();
String delStmt = "delete from " + targetDescriptor.getBaseTable() + " where ";
deleteByParentIdSql = delStmt + deriveWhereParentIdSql(false);
deleteByParentIdInSql = delStmt + deriveWhereParentIdSql(true);
}
}
}
@@ -14,6 +14,8 @@ import com.avaje.ebeaninternal.api.SpiExpressionValidation;
import com.avaje.ebeaninternal.server.core.OrmQueryRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import com.avaje.ebeaninternal.server.query.SplitName;
/**
* A "Query By Example" type of expression.
@@ -207,25 +209,41 @@ public class DefaultExampleExpression implements SpiExpression, ExampleExpressio
OrmQueryRequest<?> r = (OrmQueryRequest<?>) request;
BeanDescriptor<?> beanDescriptor = r.getBeanDescriptor();
for (BeanProperty beanProperty : beanDescriptor.propertiesAll()) {
String propName = beanProperty.getName();
Object value = beanProperty.getValue(entity);
addExpressions(list, beanDescriptor, entity, null);
if (beanProperty.isScalar() && value != null) {
if (value instanceof String) {
list.add(new LikeExpression(propName, (String) value, caseInsensitive, likeType));
} else {
// exclude the zero values typically to weed out
// primitive int and long that initialise to 0
if (includeZeros || !isZero(value)) {
list.add(new SimpleExpression(propName, Op.EQ, value));
return list;
}
/**
* Add expressions to the list for all the non-null properties (and do this recursively).
*/
private void addExpressions(ArrayList<SpiExpression> list, BeanDescriptor<?> beanDescriptor, EntityBean bean, String prefix) {
for (BeanProperty beanProperty : beanDescriptor.propertiesAll()) {
if (!beanProperty.isTransient()) {
Object value = beanProperty.getValue(bean);
if (value != null) {
String propName = SplitName.add(prefix, beanProperty.getName());
if (beanProperty.isScalar()) {
if (value instanceof String) {
list.add(new LikeExpression(propName, (String) value, caseInsensitive, likeType));
} else {
// exclude the zero values typically to weed out
// primitive int and long that initialise to 0
if (includeZeros || !isZero(value)) {
list.add(new SimpleExpression(propName, Op.EQ, value));
}
}
} else if ((beanProperty instanceof BeanPropertyAssocOne) && (value instanceof EntityBean)) {
BeanPropertyAssocOne assocOne = (BeanPropertyAssocOne)beanProperty;
BeanDescriptor targetDescriptor = assocOne.getTargetDescriptor();
addExpressions(list, targetDescriptor, (EntityBean)value, propName);
}
}
}
}
return list;
}
/**
@@ -603,7 +603,7 @@ public class SqlTreeBuilder {
// no extra join required for embedded beans
return null;
}
SqlTreeNodeExtraJoin extraJoin = new SqlTreeNodeExtraJoin(propertyName, assocProp);
SqlTreeNodeExtraJoin extraJoin = new SqlTreeNodeExtraJoin(propertyName, assocProp, elGetValue.containsMany());
joinRegister.put(propertyName, extraJoin);
return extraJoin;
}
@@ -29,11 +29,14 @@ public class SqlTreeNodeExtraJoin implements SqlTreeNode {
private final boolean manyJoin;
private final boolean pathContainsMany;
private List<SqlTreeNodeExtraJoin> children;
public SqlTreeNodeExtraJoin(String prefix, BeanPropertyAssoc<?> assocBeanProperty) {
public SqlTreeNodeExtraJoin(String prefix, BeanPropertyAssoc<?> assocBeanProperty, boolean pathContainsMany) {
this.prefix = prefix;
this.assocBeanProperty = assocBeanProperty;
this.pathContainsMany = pathContainsMany;
this.manyJoin = assocBeanProperty instanceof BeanPropertyAssocMany<?>;
}
@@ -95,13 +98,17 @@ public class SqlTreeNodeExtraJoin implements SqlTreeNode {
}
}
if (pathContainsMany) {
// "promote" to left outer as the path contains a many
joinType = SqlJoinType.OUTER;
}
if (!manyToMany) {
assocBeanProperty.addJoin(joinType, prefix, ctx);
}
if (children != null) {
if (manyJoin) {
if (manyJoin || pathContainsMany) {
// if AUTO then make all descendants use OUTER JOIN
joinType = joinType.autoToOuter();
}
@@ -2,11 +2,13 @@ package com.avaje.ebeaninternal.server.transaction;
import com.avaje.ebean.BackgroundExecutor;
import com.avaje.ebean.config.ServerConfig;
import com.avaje.ebean.config.dbplatform.DatabasePlatform;
import com.avaje.ebeaninternal.api.SpiTransaction;
import com.avaje.ebeaninternal.server.cluster.ClusterManager;
import com.avaje.ebeaninternal.server.core.BootupClasses;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager;
import javax.sql.DataSource;
import java.sql.Connection;
/**
@@ -29,4 +31,23 @@ public class ExplicitTransactionManager extends TransactionManager {
return new ExplicitJdbcTransaction(prefix + id, explicit, c, this);
}
/**
* Override the initialise of OnQueryOnly with the intention not to use CLOSE with ExplicitJdbcTransaction.
*/
@Override
protected DatabasePlatform.OnQueryOnly initOnQueryOnly(DatabasePlatform.OnQueryOnly dbPlatformOnQueryOnly, DataSource ds) {
// first check for a system property 'override'
String systemPropertyValue = System.getProperty("ebean.transaction.onqueryonly");
if (systemPropertyValue != null) {
return DatabasePlatform.OnQueryOnly.valueOf(systemPropertyValue.trim().toUpperCase());
}
if (DatabasePlatform.OnQueryOnly.CLOSE.equals(dbPlatformOnQueryOnly)) {
// Not using OnQueryOnly.CLOSE with ExplicitJdbcTransaction
return DatabasePlatform.OnQueryOnly.COMMIT;
}
// default to commit if not defined on the platform
return dbPlatformOnQueryOnly == null ? DatabasePlatform.OnQueryOnly.COMMIT : dbPlatformOnQueryOnly;
}
}
@@ -164,7 +164,7 @@ public class TransactionManager {
* just for queries do need to be committed or rollback after the query.
* </p>
*/
private OnQueryOnly initOnQueryOnly(OnQueryOnly dbPlatformOnQueryOnly, DataSource ds) {
protected OnQueryOnly initOnQueryOnly(OnQueryOnly dbPlatformOnQueryOnly, DataSource ds) {
// first check for a system property 'override'
String systemPropertyValue = System.getProperty("ebean.transaction.onqueryonly");
@@ -190,7 +190,7 @@ public class TransactionManager {
/**
* Return true if the isolation level is read committed.
*/
private boolean isReadCommittedIsolation(DataSource ds) {
protected boolean isReadCommittedIsolation(DataSource ds) {
if (DbOffline.isSet()) {
return true;
@@ -24,4 +24,9 @@ public class BaseTestCase {
SpiEbeanServer spi = (SpiEbeanServer)Ebean.getDefaultServer();
return spi.getDatabasePlatform().getName().startsWith("mssqlserver");
}
public boolean isH2() {
SpiEbeanServer spi = (SpiEbeanServer)Ebean.getDefaultServer();
return spi.getDatabasePlatform().getName().equals("h2");
}
}
@@ -30,7 +30,6 @@ public class PrimaryServerTest {
public void testLoadProperties() throws Exception {
Properties properties = PrimaryServer.getProperties();
System.out.println(properties);
assertTrue(properties.size() > 0);
}
}
@@ -2,25 +2,33 @@ package com.avaje.ebean.dbmigration.migrationreader;
import com.avaje.ebean.dbmigration.migration.Migration;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
public class MigrationXmlWriterTest {
import static org.assertj.core.api.Assertions.assertThat;
Logger logger = LoggerFactory.getLogger(MigrationXmlWriterTest.class);
public class MigrationXmlWriterTest {
@Test
public void testReadWrite() throws Exception {
Migration migration = MigrationXmlReader.read("/container/test-create-table.xml");
assertThat(migration.getChangeSet()).hasSize(1);
assertThat(migration.getChangeSet().get(0).getChangeSetChildren()).hasSize(3);
File temp = File.createTempFile("migrationWrite",".xml");
MigrationXmlWriter writer = new MigrationXmlWriter();
writer.write(migration, temp);
new MigrationXmlWriter("THIS IS A GENERATED FILE - DO NOT MODIFY").write(migration, temp);
logger.info("wrote migration file: "+temp.getAbsolutePath());
Migration migrationRead = MigrationXmlReader.read(temp);
assertThat(migrationRead.getChangeSet()).hasSize(1);
assertThat(migrationRead.getChangeSet().get(0).getChangeSetChildren()).hasSize(3);
temp = File.createTempFile("migrationWrite",".xml");
new MigrationXmlWriter(null).write(migration, temp);
Migration migrationRead2 = MigrationXmlReader.read(temp);
assertThat(migrationRead2.getChangeSet()).hasSize(1);
assertThat(migrationRead.getChangeSet().get(0).getChangeSetChildren()).hasSize(3);
}
}
@@ -61,11 +61,8 @@ public class TestBasicPush extends BaseTestCase {
generator.close();
String json = writer.toString();
System.out.println(json);
String response = post("http://localhost:9200/_bulk", json);
System.out.println(response);
post("http://localhost:9200/_bulk", json);
//curl -s -XPOST localhost:9200/_bulk
@@ -30,7 +30,7 @@ public class ChangeJsonBuilderTest extends BaseTestCase {
for (int i = 0; i < changes.size(); i++) {
StringWriter buffer = new StringWriter();
builder.writeBeanJson(buffer, changes.get(i), changeSet, i);
System.out.println(buffer.toString());
assertThat(buffer.toString()).contains("\"source\"");
assertThat(buffer.toString()).contains("\"userId\"");
assertThat(buffer.toString()).contains("\"userIpAddress\"");
@@ -19,8 +19,6 @@ public class DeployPropertyParserMapTests {
DeployPropertyParserMap parser = new DeployPropertyParserMap(map);
String output = parser.parse("(lower(customer.name) like ? escape'' or id > ?)");
System.out.println(output);
Assert.assertEquals("(lower(t1.name) like ? escape'' or t0.id > ?)", output);
}
@@ -0,0 +1,73 @@
package com.avaje.ebeaninternal.server.expression;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.LikeType;
import com.avaje.ebean.Query;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebeaninternal.api.HashQueryPlanBuilder;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.server.core.OrmQueryRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.tests.model.basic.Address;
import com.avaje.tests.model.basic.Customer;
import com.avaje.tests.model.basic.ResetBasicData;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class DefaultExampleExpressionTest extends BaseTestCase {
@Test
public void test() {
SpiEbeanServer server = (SpiEbeanServer)Ebean.getDefaultServer();
BeanDescriptor<Customer> desc = server.getBeanDescriptor(Customer.class);
SpiQuery<Customer> query = (SpiQuery<Customer>)server.find(Customer.class);
Address address = new Address();
address.setCity("billingAddress.city");
Customer customer = new Customer();
customer.setName("name");
customer.setBillingAddress(address);
DefaultExampleExpression expr = new DefaultExampleExpression((EntityBean)customer, false, LikeType.EQUAL_TO);
BeanQueryRequest<?> request = create(query, desc);
HashQueryPlanBuilder builder = new HashQueryPlanBuilder();
expr.queryPlanHash(request, builder);
TDSpiExpressionRequest req = new TDSpiExpressionRequest(desc);
expr.addBindValues(req);
assertThat(req.bindValues).contains("name", "billingAddress.city");
address.setCity("Auckland");
customer.setName("Rob");
ResetBasicData.reset();
Query<Customer> query1 = server.find(Customer.class)
.where().exampleLike(customer)
.query();
query1.findList();
assertThat(query1.getGeneratedSql()).contains("(t0.name like ? ");
assertThat(query1.getGeneratedSql()).contains(" and t1.city like ? ");
}
private <T> OrmQueryRequest<T> create(SpiQuery<T> query, BeanDescriptor<T> desc) {
return new OrmQueryRequest<T>(null, null, query, desc, null);
}
}
@@ -0,0 +1,78 @@
package com.avaje.ebeaninternal.server.expression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.core.JsonExpressionHandler;
import com.avaje.ebeaninternal.server.core.SpiOrmQueryRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import java.util.ArrayList;
import java.util.List;
/**
* Test double for testing with SpiExpressionRequest.
*/
public class TDSpiExpressionRequest implements SpiExpressionRequest {
List<Object> bindValues = new ArrayList<Object>();
final BeanDescriptor<?> descriptor;
public TDSpiExpressionRequest(BeanDescriptor<?> descriptor) {
this.descriptor = descriptor;
}
@Override
public JsonExpressionHandler getJsonHandler() {
return null;
}
@Override
public String parseDeploy(String logicalProp) {
return null;
}
@Override
public BeanDescriptor<?> getBeanDescriptor() {
return descriptor;
}
@Override
public SpiOrmQueryRequest<?> getQueryRequest() {
return null;
}
@Override
public SpiExpressionRequest append(String sql) {
return null;
}
@Override
public void addBindEncryptKey(Object encryptKey) {
}
@Override
public void addBindValue(Object bindValue) {
bindValues.add(bindValue);
}
@Override
public String getSql() {
return null;
}
@Override
public ArrayList<Object> getBindValues() {
return null;
}
@Override
public int nextParameter() {
return 0;
}
@Override
public void appendLike() {
}
}
@@ -49,8 +49,6 @@ public class TestDataSourceMax extends BaseTestCase {
bg.execute(new ConnRunner(pool, 4000, i));
}
System.out.println("main thread sleep ... " + pool.getStatus(false));
Thread.sleep(10000);
pool.getStatistics(true);
@@ -81,9 +79,7 @@ public class TestDataSourceMax extends BaseTestCase {
private void waitSomeTime(long count) {
try {
System.out.println(position+" sleep " + sleepMillis+" count:"+count);
Thread.sleep(sleepMillis);
System.out.println(position+" sleep done");
} catch (InterruptedException e){
throw new RuntimeException(e);
}
@@ -30,9 +30,6 @@ public class TestDataSourceMaxWithEntity extends BaseTestCase {
bg.execute(new ConnRunner(server, 4000, i));
}
System.out.println("main thread sleep ... ");
Thread.sleep(30000);
server.shutdown(true, false);
@@ -24,7 +24,6 @@ public class TestRawSqlParsing extends TestCase {
Sql rs = rawSql.getSql();
String s = rs.toString();
System.out.println(s);
assertTrue(s, s.contains("[order_id, sum"));
}
@@ -42,7 +42,6 @@ public class TestJsonWriteVisitor extends BaseTestCase {
options.setPathVisitor("contacts", new ContactVisitor());
String jsonContent = json.toJson(list, options);
System.out.println(jsonContent);
assertThat(jsonContent).contains("customerExtra");
assertThat(jsonContent).contains("contactExtra");
@@ -42,14 +42,11 @@ public class TestTextJsonBeanReadVisitor extends BaseTestCase {
options.addVisitor("shippingAddress", new ASVisitor());
String s = json.toJson(list);
System.out.println(s);
List<Customer> mList = json.toList(Customer.class, s, options);
System.out.println("VIA STRING: " + mList);
StringReader reader = new StringReader(s);
List<Customer> mList2 = json.toList(Customer.class, reader);
System.out.println("VIA READER: " + mList2);
assertEquals(mList.size(), mList2.size());
}
@@ -58,28 +55,28 @@ public class TestTextJsonBeanReadVisitor extends BaseTestCase {
@Override
public void visit(Customer bean, Map<String, Object> unmapped) {
System.out.println("visit customer: " + bean);
bean.getId();
}
}
private static class AVisitor implements JsonReadBeanVisitor<Address> {
public void visit(Address bean, Map<String, Object> unmapped) {
System.out.println("visit billing address: " + bean);
bean.getId();
}
}
private static class ASVisitor implements JsonReadBeanVisitor<Address> {
public void visit(Address bean, Map<String, Object> unmapped) {
System.out.println("visit shipping address: " + bean);
bean.getId();
}
}
private static class ContactVisitor implements JsonReadBeanVisitor<Contact> {
public void visit(Contact bean, Map<String, Object> unmapped) {
System.out.println("visit contact: " + bean);
bean.getId();
}
}
}
@@ -17,8 +17,6 @@ public class ScalarTypeLocalDateTest {
LocalDate date = LocalDate.of(2014, 5, 20);
long millis = type.convertToMillis(date);
System.out.println(date);
LocalDate parseDate = type.convertFromMillis(millis);
assertEquals(date, parseDate);
}
@@ -27,7 +27,8 @@ public class MainAutoQueryTune1 {
.findList();
for (Order order : list) {
System.out.println(order.getId() + " " + order.getOrderDate());
order.getId();
order.getOrderDate();
}
}
}
@@ -3,7 +3,9 @@ package com.avaje.tests.autofetch;
import java.util.List;
import java.util.Set;
import com.avaje.ebeaninternal.server.autotune.model.Origin;
import org.avaje.ebeantest.LoggedSqlCollector;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;
import org.junit.Test;
@@ -33,34 +35,6 @@ public class TunedQueryInfoTest extends BaseTestCase {
serverCacheManager.setCaching(Order.class, false);
}
@Test
public void withSelectNull() {
init();
OrmQueryDetail tunedDetail = new OrmQueryDetail();
tunedDetail.select(null);
TunedQueryInfo tunedInfo = new TunedQueryInfo(tunedDetail);
Query<Order> query = server.find(Order.class).setId(1);
tunedInfo.tuneQuery((SpiQuery<?>) query);
Order order = query.findUnique();
EntityBean eb = (EntityBean)order;
EntityBeanIntercept ebi = eb._ebean_getIntercept();
Assert.assertTrue(ebi.isFullyLoadedBean());
Set<String> loadedPropertyNames = ebi.getLoadedPropertyNames();
Assert.assertNull(loadedPropertyNames);
// invoke lazy loading
order.getCustomer();
}
@Test
public void withSelectEmpty() {
@@ -68,9 +42,9 @@ public class TunedQueryInfoTest extends BaseTestCase {
OrmQueryDetail tunedDetail = new OrmQueryDetail();
tunedDetail.select("");
TunedQueryInfo tunedInfo = new TunedQueryInfo(tunedDetail);
TunedQueryInfo tunedInfo = createTunedQueryInfo(tunedDetail);
Query<Order> query = server.find(Order.class).setId(1);
tunedInfo.tuneQuery((SpiQuery<?>) query);
@@ -95,9 +69,9 @@ public class TunedQueryInfoTest extends BaseTestCase {
OrmQueryDetail tunedDetail = new OrmQueryDetail();
tunedDetail.select("somethingThatDoesNotExist");
TunedQueryInfo tunedInfo = new TunedQueryInfo(tunedDetail);
TunedQueryInfo tunedInfo = createTunedQueryInfo(tunedDetail);
Query<Order> query = server.find(Order.class).setId(1);
tunedInfo.tuneQuery((SpiQuery<?>) query);
@@ -123,7 +97,14 @@ public class TunedQueryInfoTest extends BaseTestCase {
Assert.assertTrue(loggedSql.get(0).contains("select t0.id c0, t0.id c1 from o_order t0 where t0.id = ?"));
Assert.assertTrue(loggedSql.get(1).contains("select t0.id c0, t0.status c1,"));
}
@NotNull
private TunedQueryInfo createTunedQueryInfo(OrmQueryDetail tunedDetail) {
Origin origin = new Origin();
origin.setDetail(tunedDetail.toString());
return new TunedQueryInfo(origin);
}
@Test
public void withSelectSomeIncludeLazyLoaded() {
@@ -132,7 +113,7 @@ public class TunedQueryInfoTest extends BaseTestCase {
OrmQueryDetail tunedDetail = new OrmQueryDetail();
tunedDetail.select("status, customer");
TunedQueryInfo tunedInfo = new TunedQueryInfo(tunedDetail);
TunedQueryInfo tunedInfo = createTunedQueryInfo(tunedDetail);
Query<Order> query = server.find(Order.class).setId(1);
@@ -167,7 +148,7 @@ public class TunedQueryInfoTest extends BaseTestCase {
OrmQueryDetail tunedDetail = new OrmQueryDetail();
tunedDetail.select("status");
TunedQueryInfo tunedInfo = new TunedQueryInfo(tunedDetail);
TunedQueryInfo tunedInfo = createTunedQueryInfo(tunedDetail);
Query<Order> query = server.find(Order.class).setId(1);
@@ -33,6 +33,7 @@ public class TestBatchLazy extends BaseTestCase {
}
Ebean.getDefaultServer().getAutoTune().collectProfiling();
Ebean.getDefaultServer().getAutoTune().reportProfiling();
}
@@ -16,8 +16,7 @@ public class TestBatchLazyMany extends BaseTestCase {
Order order2 = Ebean.getReference(Order.class, 1);
order2.getOrderDate();
System.out.println("done");
// List<Order> list = Ebean.find(Order.class)
// //.join("details")
// //.join("details", "+fetchquery")
@@ -47,8 +47,6 @@ public class TestBeanReferenceRefresh extends BaseTestCase {
Status statusRefresh = order.getStatus();
Assert.assertEquals(status,statusRefresh);
System.out.println("done");
}
@@ -24,7 +24,6 @@ public class TestDeleteImportedPartial extends BaseTestCase {
// should delete file and fileContent
Ebean.delete(partialPfile);
System.out.println("finished delete");
PFile file1 = Ebean.find(PFile.class, id);
PFileContent content1 = Ebean.find(PFileContent.class, contentId);
@@ -26,7 +26,6 @@ public class TestDeleteOneToOneMultiple extends BaseTestCase {
// should delete file and fileContent
Ebean.delete(PFile.class, id);
System.out.println("finished delete");
PFile file1 = Ebean.find(PFile.class, id);
PFileContent content1 = Ebean.find(PFileContent.class, contentId);
@@ -36,14 +36,11 @@ public class TestFetchId extends BaseTestCase {
List<Object> partial = futureIds.getPartialIds();
// this is likely 0 or a small number
System.out.println("partial: " + partial.size());
// wait for all the id's to be fetched
List<Object> idList = futureIds.get();
Assert.assertTrue("same instance", partial == idList);
Assert.assertTrue("sz > 0", ids.size() > 0);
System.out.println("ids: " + partial);
}
}
@@ -2,6 +2,7 @@ package com.avaje.tests.basic;
import java.util.List;
import com.avaje.ebean.Query;
import org.junit.Test;
import com.avaje.ebean.BaseTestCase;
@@ -18,13 +19,7 @@ public class TestM2MCascadeOne extends BaseTestCase {
MUser u = new MUser();
u.setUserName("testM2M");
List<MRole> roles = u.getRoles();
if (roles != null) {
if (roles instanceof BeanList<?>) {
System.out.println("enhancement checkNullManyFields=true successful");
}
}
u.getRoles();
Ebean.save(u);
MRole r0 = new MRole();
@@ -42,4 +37,16 @@ public class TestM2MCascadeOne extends BaseTestCase {
Ebean.save(u1);
}
@Test
public void testRawPredicate_with_ManyToManyPath() {
Query<MUser> query = Ebean.find(MUser.class)
.select("userid")
.where().raw("roles.roleid in (?)", 24)
.query();
query.findList();
}
}
@@ -20,14 +20,13 @@ public class TestOrderTotalAmountReportBean extends BaseTestCase {
List<OrderAggregate> l0 = Ebean.find(OrderAggregate.class).findList();
for (OrderAggregate r0 : l0) {
System.out.println(r0);
r0.toString();
}
List<OrderAggregate> list = Ebean.createNamedQuery(OrderAggregate.class, "total.amount")
.where().gt("order.id", 0).having().gt("totalAmount", 50).findList();
for (OrderAggregate r1 : list) {
System.out.println(r1);
Assert.assertTrue(r1.getTotalAmount() > 20.50);
// partial object query without totalItems
// ... no lazy loading invoked on this type of bean
@@ -38,7 +37,6 @@ public class TestOrderTotalAmountReportBean extends BaseTestCase {
.having().lt("totalItems", 3).gt("totalAmount", 50).findList();
for (OrderAggregate r2 : l2) {
// System.out.println(r2);
Assert.assertTrue(r2.getTotalItems() < 3);
}
@@ -25,6 +25,11 @@ public class TestEncrypt extends BaseTestCase {
@Test
public void testQueryBind() {
if (!isH2()) {
// only run this on H2 - PGCrypto not happy on CI server
return;
}
LoggedSqlCollector.start();
Ebean.find(EBasicEncrypt.class)
.where().startsWith("description", "Rob")
@@ -67,14 +72,12 @@ public class TestEncrypt extends BaseTestCase {
q.setParameter("id", e.getId());
SqlRow row = q.findUnique();
String name = row.getString("name");
Object desc = row.get("description");
System.out.println("SqlRow: " + name + " " + desc);
row.getString("name");
row.get("description");
EBasicEncrypt e1 = Ebean.find(EBasicEncrypt.class, e.getId());
String desc1 = e1.getDescription();
System.out.println("Decrypted: " + desc1 + " " + e1.getDob());
e1.getDescription();
e1.setName("testmod");
e1.setDescription("moddesc");
@@ -83,8 +86,7 @@ public class TestEncrypt extends BaseTestCase {
EBasicEncrypt e2 = Ebean.find(EBasicEncrypt.class, e.getId());
String desc2 = e2.getDescription();
System.out.println("moddesc=" + desc2);
e2.getDescription();
SpiEbeanServer server = (SpiEbeanServer) Ebean.getServer(null);
DbEncrypt dbEncrypt = server.getDatabasePlatform().getDbEncrypt();
@@ -29,18 +29,16 @@ public class TestEncryptBinary extends BaseTestCase {
q.setParameter("id", e.getId());
SqlRow row = q.findUnique();
String name = row.getString("name");
Object data = row.get("data");
Object someTimeData = row.get("some_time");
System.out.println("SqlRow name:" + name + " data:" + data + " someTime:" + someTimeData);
row.getString("name");
row.get("data");
row.get("some_time");
EBasicEncryptBinary e1 = Ebean.find(EBasicEncryptBinary.class, e.getId());
Timestamp t1 = e1.getSomeTime();
byte[] data1 = e1.getData();
String s = new String(data1);
String desc1 = e1.getDescription();
System.out.println("Decrypted data:" + s + " desc:" + desc1);
e1.getData();
e1.getDescription();
Assert.assertEquals(t0, t1);
@@ -50,9 +48,7 @@ public class TestEncryptBinary extends BaseTestCase {
Ebean.save(e1);
EBasicEncryptBinary e2 = Ebean.find(EBasicEncryptBinary.class, e.getId());
String desc2 = e2.getDescription();
System.out.println("moddesc=" + desc2);
e2.getDescription();
}
}
@@ -27,9 +27,6 @@ public class TestSecondaryJoin extends BaseTestCase {
o0.setStatus(Status.APPROVED);
Ebean.save(o0);
System.out.println("done");
}
}
@@ -23,10 +23,6 @@ public class TestByteOnly extends BaseTestCase {
e2.setContent(content2);
Ebean.save(e2);
// Ebean.getServer(null).getAutoTune().collectProfiling();
// Ebean.getServer(null).getAutoTune().updateTunedQueryInfo();
System.out.println("done");
}
}
@@ -1,8 +1,7 @@
package com.avaje.tests.basic.one2one;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@@ -12,6 +11,7 @@ import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Version;
import java.util.List;
@Entity
@Table(name = "drel_booking")
@@ -19,9 +19,16 @@ public class Booking {
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
@Column(unique = true)
Long bookingUid;
@Version
private int version;
public Booking(Long bookingUid) {
this.bookingUid = bookingUid;
}
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "agent_invoice")
@@ -31,7 +38,7 @@ public class Booking {
@JoinColumn(name = "client_invoice")
private Invoice clientInvoice;
@OneToMany(mappedBy = "booking")//, cascade = CascadeType.ALL)
@OneToMany(mappedBy = "booking")
private List<Invoice> invoices;
public Long getId() {
@@ -41,8 +48,16 @@ public class Booking {
public void setId(Long id) {
this.id = id;
}
public int getVersion() {
public Long getBookingUid() {
return bookingUid;
}
public void setBookingUid(Long bookingUid) {
this.bookingUid = bookingUid;
}
public int getVersion() {
return version;
}
@@ -69,7 +84,7 @@ public class Booking {
public List<Invoice> getInvoices() {
return invoices;
}
public void setInvoices(List<Invoice> invoices) {
this.invoices= invoices;
}
@@ -21,7 +21,7 @@ public class Invoice {
private int version;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "booking")
@JoinColumn(name = "booking")//_xid", referencedColumnName = "booking_uid")
private Booking booking;
public Long getId() {
@@ -10,7 +10,9 @@ public class TestOne2OneBookingInvoice extends BaseTestCase {
@Test
public void test() {
Booking b = new Booking();
Booking b = new Booking(3000L);
Ebean.save(b);
Invoice ai = new Invoice();
Invoice ci = new Invoice();
@@ -23,6 +25,9 @@ public class TestOne2OneBookingInvoice extends BaseTestCase {
Ebean.save(b);
Invoice invoice = Ebean.find(Invoice.class, ai.getId());
Assert.assertEquals(b.getId(), invoice.getBooking().getId());
Booking b1 = Ebean.find(Booking.class, b.getId());
Invoice ai1 = b1.getAgentInvoice();
@@ -68,7 +68,7 @@ public class TestBatchLazyWithCacheHits extends BaseTestCase {
.findList();
for (UUOne uuOne : list) {
System.out.println(uuOne.getName());
uuOne.getName();
}
list.get(0).getName();
@@ -77,7 +77,7 @@ public class TestBatchLazyWithDeleted extends BaseTestCase {
list.get(2).getMaster().getName();
Assert.assertTrue(false);
} catch (EntityNotFoundException e) {
// this bean was deleted and lazy loading failed
// this bean was deleted and lazy loading failed
Assert.assertTrue(true);
}
@@ -34,8 +34,6 @@ public class TestLazyLoadEmptyCollection extends BaseTestCase {
for (Contact contact : contacts) {
contact.getNotes();
}
System.out.println(customer);
System.out.println(contacts);
}
}
@@ -0,0 +1,31 @@
package com.avaje.tests.batchload;
import java.util.UUID;
import javax.persistence.EntityNotFoundException;
import org.junit.Assert;
import org.junit.Test;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import com.avaje.tests.model.basic.UUOne;
public class TestLazyLoadNonExistentBean extends BaseTestCase {
@Test
public void testSimple() {
UUID uuid = UUID.randomUUID();
UUOne one = Ebean.getReference(UUOne.class, uuid);
try {
// invoke lazy loading
one.getName();
Assert.assertTrue(false);
} catch (EntityNotFoundException e) {
// expecting this
Assert.assertTrue(true);
}
}
}
@@ -71,10 +71,6 @@ public class TestCacheCollectionIds extends BaseTestCase {
int currentNumContacts2 = fetchCustomer(customer.getId());
Assert.assertEquals(currentNumContacts + 1, currentNumContacts2);
System.out.println("custCache:" + custCache.getStatistics(false));
System.out.println("contactCache:" + contactCache.getStatistics(false));
System.out.println("custManyIdsCache:" + custManyIdsCache.getStatistics(false));
}
private int fetchCustomer(Integer id) {
@@ -1,5 +1,7 @@
package com.avaje.tests.cache;
import com.avaje.ebeaninternal.server.autotune.model.Origin;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;
import org.junit.Test;
@@ -14,6 +16,13 @@ import com.avaje.tests.model.basic.FeatureDescription;
public class TestL2CacheWithSharedBean extends BaseTestCase {
@NotNull
private TunedQueryInfo createTunedQueryInfo(OrmQueryDetail tunedDetail) {
Origin origin = new Origin();
origin.setDetail(tunedDetail.toString());
return new TunedQueryInfo(origin);
}
@Test
public void test() {
@@ -28,7 +37,7 @@ public class TestL2CacheWithSharedBean extends BaseTestCase {
OrmQueryDetail tunedDetail = new OrmQueryDetail();
tunedDetail.select("name");
TunedQueryInfo tunedInfo = new TunedQueryInfo(tunedDetail);
TunedQueryInfo tunedInfo = createTunedQueryInfo(tunedDetail);
Query<FeatureDescription> query = Ebean.find(FeatureDescription.class).setId(f1.getId());
@@ -102,9 +102,6 @@ public class TestChangeLog extends BaseTestCase {
this.changes = changes;
try {
String json = objectMapper.writeValueAsString(changes);
logger.info(json);
ChangeSet changes1 = objectMapper.readValue(json, ChangeSet.class);
assertEquals(changes.getTxnId(), changes1.getTxnId());
@@ -3,6 +3,7 @@ package com.avaje.tests.compositekeys;
import java.util.ArrayList;
import java.util.List;
import com.avaje.ebean.PagedList;
import org.junit.Assert;
import org.junit.Test;
@@ -51,6 +52,8 @@ public class TestCKeyLazyLoad extends BaseTestCase {
Ebean.save(p2);
exerciseMaxRowsQuery_with_embeddedId();
CKeyParentId searchId = new CKeyParentId(1, "one");
CKeyParent found = Ebean.find(CKeyParent.class).where().idEq(searchId).findUnique();
@@ -79,4 +82,15 @@ public class TestCKeyLazyLoad extends BaseTestCase {
Assert.assertTrue(idInTestList.size() == 2);
}
/**
* Exercise paging/maxRows type query with EmbeddedId.
*/
private void exerciseMaxRowsQuery_with_embeddedId() {
PagedList<CKeyParent> siteUserPage = Ebean.find(CKeyParent.class).where()
.orderBy("name asc")
.findPagedList(0, 10);
siteUserPage.getList();
}
}
@@ -2,7 +2,8 @@ package com.avaje.tests.inheritance;
import java.util.List;
import org.junit.Assert;
import com.avaje.tests.model.basic.CarAccessory;
import com.avaje.tests.model.basic.CarFuse;
import org.junit.Test;
import com.avaje.ebean.BaseTestCase;
@@ -13,6 +14,11 @@ import com.avaje.tests.model.basic.Truck;
import com.avaje.tests.model.basic.Vehicle;
import com.avaje.tests.model.basic.VehicleDriver;
import static org.assertj.core.api.StrictAssertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class TestInheritInsert extends BaseTestCase {
@Test
@@ -25,11 +31,11 @@ public class TestInheritInsert extends BaseTestCase {
Vehicle v = Ebean.find(Vehicle.class, t.getId());
if (v instanceof Truck) {
Truck t0 = (Truck) v;
Assert.assertEquals(Double.valueOf(10d), t0.getCapacity());
Assert.assertEquals(Double.valueOf(10d), ((Truck) v).getCapacity());
Assert.assertNotNull(t0.getId());
assertEquals(Double.valueOf(10d), t0.getCapacity());
assertEquals(Double.valueOf(10d), ((Truck) v).getCapacity());
assertNotNull(t0.getId());
} else {
Assert.assertTrue("v not a Truck?", false);
assertTrue("v not a Truck?", false);
}
VehicleDriver driver = new VehicleDriver();
@@ -42,17 +48,17 @@ public class TestInheritInsert extends BaseTestCase {
v = d1.getVehicle();
if (v instanceof Truck) {
Double capacity = ((Truck) v).getCapacity();
Assert.assertEquals(Double.valueOf(10d), capacity);
Assert.assertNotNull(v.getId());
assertEquals(Double.valueOf(10d), capacity);
assertNotNull(v.getId());
} else {
Assert.assertTrue("v not a Truck?", false);
assertTrue("v not a Truck?", false);
}
List<VehicleDriver> list = Ebean.find(VehicleDriver.class).findList();
for (VehicleDriver vehicleDriver : list) {
if (vehicleDriver.getVehicle() instanceof Truck) {
Double capacity = ((Truck) vehicleDriver.getVehicle()).getCapacity();
Assert.assertEquals(Double.valueOf(10d), capacity);
assertEquals(Double.valueOf(10d), capacity);
}
}
}
@@ -73,12 +79,12 @@ public class TestInheritInsert extends BaseTestCase {
query.where().eq("vehicle.licenseNumber", "MARIOS_CAR_LICENSE");
List<VehicleDriver> drivers = query.findList();
Assert.assertNotNull(drivers);
Assert.assertEquals(1, drivers.size());
Assert.assertNotNull(drivers.get(0));
assertNotNull(drivers);
assertEquals(1, drivers.size());
assertNotNull(drivers.get(0));
Assert.assertEquals("Mario", drivers.get(0).getName());
Assert.assertEquals("MARIOS_CAR_LICENSE", drivers.get(0).getVehicle().getLicenseNumber());
assertEquals("Mario", drivers.get(0).getName());
assertEquals("MARIOS_CAR_LICENSE", drivers.get(0).getVehicle().getLicenseNumber());
Vehicle car2 = Ebean.find(Vehicle.class, car.getId());
@@ -86,4 +92,34 @@ public class TestInheritInsert extends BaseTestCase {
Ebean.save(car);
}
@Test
public void test_AtOrderBy_on_ChildOfChild() {
Car car = new Car();
car.setLicenseNumber("ABC");
Ebean.save(car);
CarFuse fuse = new CarFuse();
fuse.setLocationCode("xdfg");
Ebean.save(fuse);
CarAccessory accessory = new CarAccessory(car, fuse);
Ebean.save(accessory);
Query<Car> query = Ebean.find(Car.class)
.fetch("accessories")
.where()
.eq("id", car.getId())
.query();
Car result = query.findUnique();
assertThat(query.getGeneratedSql()).contains("order by t0.id, t2.location_code");
assertThat(query.getGeneratedSql()).contains("left outer join car_fuse t2 on t2.id = t1.fuse_id");
assertNotNull(result);
}
}
@@ -0,0 +1,41 @@
package com.avaje.tests.inheritance;
import com.avaje.ebean.Ebean;
import com.avaje.tests.model.basic.Car;
import com.avaje.tests.model.basic.Truck;
import com.avaje.tests.model.basic.Vehicle;
import org.junit.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class TestInheritanceBatchLazyLoad {
@Test
public void lazyLoadProperty_when_propertyNotOnAllInheritanceTypes() {
Car c = new Car();
c.setLicenseNumber("VZVZ1");
c.setDriver("CarDriver");
Ebean.save(c);
Truck t = new Truck();
t.setLicenseNumber("VZVZ2");
t.setCapacity(20D);
Ebean.save(t);
List<Vehicle> list = Ebean.find(Vehicle.class)
.select("licenseNumber")
.where().startsWith("licenseNumber","VZVZ")
.order().asc("licenseNumber")
.findList();
assertThat(list).hasSize(2);
Car car = (Car)list.get(0);
car.getNotes();
}
}
@@ -2,12 +2,15 @@ package com.avaje.tests.json.include;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.config.JsonConfig;
import com.avaje.ebean.text.PathProperties;
import com.avaje.ebean.text.json.JsonWriteOptions;
import com.avaje.tests.json.transientproperties.EJsonTransientEntityList;
import com.avaje.tests.json.transientproperties.EJsonTransientList;
import org.junit.Test;
import java.util.ArrayList;
import static org.assertj.core.api.StrictAssertions.assertThat;
import static org.junit.Assert.assertEquals;
public class TestJsonExcludeTransientEmptyList {
@@ -45,4 +48,18 @@ public class TestJsonExcludeTransientEmptyList {
assertEquals(expectedJson, asJson);
}
@Test
public void testToJson_with_transientExcludeFromPathProperties() throws Exception {
EJsonTransientEntityList bean = new EJsonTransientEntityList();
bean.setId(99L);
bean.setName("John");
PathProperties pathProps = PathProperties.parse("id,name");
String asJson = Ebean.json().toJson(bean, pathProps);
assertThat(asJson).isEqualTo("{\"id\":99,\"name\":\"John\"}");
}
}
@@ -0,0 +1,57 @@
package com.avaje.tests.json.transientproperties;
import com.avaje.ebean.annotation.Sql;
import com.avaje.tests.model.basic.Order;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;
import java.util.List;
@Sql
@Entity
public class EJsonTransientEntityList {
@Id
private Long id;
private String name;
@Transient
private Boolean basic;
@Transient
private List<Order> orders;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getBasic() {
return basic;
}
public void setBasic(Boolean basic) {
this.basic = basic;
}
public List<Order> getOrders() {
return orders;
}
public void setOrders(List<Order> orders) {
this.orders = orders;
}
}
@@ -0,0 +1,46 @@
package com.avaje.tests.json.transientproperties;
import com.avaje.ebean.annotation.Sql;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;
import java.util.List;
@Sql
@Entity
public class ModelA {
@Id
int id;
String a;
// transient mapping to an entity bean
@Transient
List<ModelB> list;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public List<ModelB> getList() {
return list;
}
public void setList(List<ModelB> list) {
this.list = list;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
@@ -0,0 +1,31 @@
package com.avaje.tests.json.transientproperties;
import com.avaje.ebean.annotation.Sql;
import javax.persistence.Entity;
@Sql
@Entity
public class ModelB {
Integer oneField;
Integer twoField;
public Integer getOneField() {
return oneField;
}
public void setOneField(Integer oneField) {
this.oneField = oneField;
}
public Integer getTwoField() {
return twoField;
}
public void setTwoField(Integer twoField) {
this.twoField = twoField;
}
}
@@ -0,0 +1,34 @@
package com.avaje.tests.json.transientproperties;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.text.PathProperties;
import org.junit.Test;
import java.util.ArrayList;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class TestModelAJson {
@Test
public void test() {
ModelA a = new ModelA();
a.setId(1);
a.setA("a");
ModelB b = new ModelB();
b.setOneField(1);
b.setTwoField(1);
a.setList(new ArrayList<ModelB>());
a.getList().add(b);
PathProperties pathProperties = PathProperties.parse("(a,list(oneField))");
String json = Ebean.json().toJson(a, pathProperties);
assertThat(json).isEqualTo("{\"a\":\"a\",\"list\":[{\"oneField\":1}]}");
}
}
@@ -1,51 +1,61 @@
package com.avaje.tests.model.basic;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import java.util.HashSet;
import java.util.Set;
@Entity
@Inheritance
@DiscriminatorValue("C")
public class Car extends Vehicle {
private static final long serialVersionUID = 4716705779684333446L;
private static final long serialVersionUID = 4716705779684333446L;
private String driver;
private String driver;
@ManyToOne
TruckRef carRef;
@OneToMany(mappedBy="car")
private Set<CarAccessory> accessories = new HashSet<CarAccessory>();
public String getDriver() {
return driver;
}
@ManyToOne
private TruckRef carRef;
public void setDriver(String driver) {
this.driver = driver;
}
@OneToMany(mappedBy = "car")
@OrderBy("fuse.locationCode")
private Set<CarAccessory> accessories = new HashSet<CarAccessory>();
public TruckRef getCarRef() {
return carRef;
}
private String notes;
public void setCarRef(TruckRef carRef) {
this.carRef = carRef;
}
public String getDriver() {
return driver;
}
public Set<CarAccessory> getAccessories() {
return accessories;
}
public void setDriver(String driver) {
this.driver = driver;
}
public void setAccessories(Set<CarAccessory> accessories) {
this.accessories = accessories;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public TruckRef getCarRef() {
return carRef;
}
public void setCarRef(TruckRef carRef) {
this.carRef = carRef;
}
public Set<CarAccessory> getAccessories() {
return accessories;
}
public void setAccessories(Set<CarAccessory> accessories) {
this.accessories = accessories;
}
}
@@ -4,28 +4,44 @@ import javax.persistence.Entity;
import javax.persistence.ManyToOne;
@Entity
public class CarAccessory extends BasicDomain{
private static final long serialVersionUID = 1L;
public class CarAccessory extends BasicDomain {
private String name;
@ManyToOne
private Car car;
private static final long serialVersionUID = 1L;
public String getName() {
return name;
}
private String name;
public void setName(String name) {
this.name = name;
}
@ManyToOne(optional = false)
private CarFuse fuse;
public Car getCar() {
return car;
}
@ManyToOne
private Car car;
public void setCar(Car car) {
this.car = car;
}
public CarAccessory(Car car, CarFuse fuse) {
this.car = car;
this.fuse = fuse;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public CarFuse getFuse() {
return fuse;
}
public void setFuse(CarFuse fuse) {
this.fuse = fuse;
}
}
@@ -0,0 +1,29 @@
package com.avaje.tests.model.basic;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class CarFuse {
@Id
Long id;
String locationCode;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLocationCode() {
return locationCode;
}
public void setLocationCode(String locationCode) {
this.locationCode = locationCode;
}
}
@@ -1,95 +1,88 @@
package com.avaje.tests.model.basic;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import java.util.List;
@Entity
@Table(name = "mrole")
public class MRole {
@Id
Integer roleid;
String roleName;
@Id
Integer roleid;
@ManyToMany(cascade=CascadeType.ALL
// @JoinTable(name="myint_table"//,
// joinColumns={
// @JoinColumn(name="mroleid", referencedColumnName="roleid")
// }//,
// inverseJoinColumns={
// @JoinColumn(name = "muserid", referencedColumnName="userid")
// }
)
List<MUser> users;
public MRole() {
String roleName;
@ManyToMany(cascade = CascadeType.ALL)
List<MUser> users;
public MRole() {
}
public MRole(String roleName) {
this.roleName = roleName;
}
public Integer getRoleid() {
return roleid;
}
public void setRoleid(Integer roleid) {
this.roleid = roleid;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public List<MUser> getUsers() {
return users;
}
public void setUsers(List<MUser> users) {
this.users = users;
}
@Override
public String toString() {
return "MRole [roleName=" + roleName + "]";
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
public MRole(String roleName) {
this.roleName = roleName;
// Make sure other is not null and has the same class as this
if (other != null && getClass().equals(other.getClass())) {
final MRole rhs = (MRole) other;
if (roleid.equals(rhs.roleid)) {
if (roleid == 0) {
return false;
} else {
return true;
}
}
}
public Integer getRoleid() {
return roleid;
}
return false;
}
public void setRoleid(Integer roleid) {
this.roleid = roleid;
}
@Override
public int hashCode() {
if (roleid != null && roleid != 0) {
int rid = roleid;
return (int) (rid ^ (rid >>> 32));
}
return super.hashCode();
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public List<MUser> getUsers() {
return users;
}
public void setUsers(List<MUser> users) {
this.users = users;
}
@Override
public String toString() {
return "MRole [roleName=" + roleName + "]";
}
@Override
public boolean equals(Object other) {
if (this == other){
return true;
}
// Make sure other is not null and has the same class as this
if (other != null && getClass().equals(other.getClass())){
final MRole rhs = (MRole)other;
if ( roleid.equals(rhs.roleid)){
if (roleid == 0){
return false;
}else{
return true;
}
}
}
return false;
}
@Override
public int hashCode() {
if (roleid != null && roleid != 0){
int rid = roleid;
return (int)( rid ^ (rid >>> 32) );
}
return super.hashCode();
}
}
@@ -6,6 +6,7 @@ import java.util.List;
import javax.persistence.*;
@Entity
@Table(name="muser")
public class MUser {
@Id
@@ -2,8 +2,10 @@ package com.avaje.tests.model.basic;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="muser_type")
public class MUserType {
@Id
@@ -21,7 +21,7 @@ public class OrderDetail implements Serializable {
@Id
Integer id;
@ManyToOne
@ManyToOne(optional = false)
Order order;
Integer orderQty;
@@ -0,0 +1,21 @@
package com.avaje.tests.model.onetoone;
import com.avaje.tests.model.basic.BasicDomain;
import javax.persistence.Column;
import javax.persistence.Entity;
@Entity
public class OCompany extends BasicDomain {
@Column(length = 50, unique = true)
public String corpId;
public String getCorpId() {
return corpId;
}
public void setCorpId(String corpId) {
this.corpId = corpId;
}
}
@@ -0,0 +1,24 @@
package com.avaje.tests.model.onetoone;
import com.avaje.tests.model.basic.BasicDomain;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
public class ORoadShowMsg extends BasicDomain {
@OneToOne(cascade = CascadeType.ALL, optional = false)
@JoinColumn()//(name = "corp_id", nullable = false, referencedColumnName = "corp_id")
public OCompany company;
public OCompany getCompany() {
return company;
}
public void setCompany(OCompany company) {
this.company = company;
}
}
@@ -0,0 +1,20 @@
package com.avaje.tests.model.onetoone;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import org.junit.Test;
public class TestOneToOneJoinColumn extends BaseTestCase {
@Test
public void test() {
ORoadShowMsg msg = new ORoadShowMsg();
OCompany company = new OCompany();
company.setCorpId("corp_id_1000000");
msg.setCompany(company);
Ebean.save(msg);
Ebean.find(ORoadShowMsg.class, msg.getId());
}
}
@@ -53,8 +53,6 @@ public class TestAutofetchTuneWithJoin extends BaseTestCase {
ObjectGraphNode parentNode = sq.getParentNode();
ObjectGraphOrigin origin = parentNode.getOriginQueryPoint();
System.out.println("Origin:" + origin.getKey());
// MetaAutoFetchStatistic metaAutoFetchStatistic =
// ((DefaultOrmQuery<?>)q).getMetaAutoFetchStatistic();
// if (metaAutoFetchStatistic != null) {
@@ -65,9 +63,6 @@ public class TestAutofetchTuneWithJoin extends BaseTestCase {
// System.out.println(queryStats);
// }
if (q.isAutoTuned()) {
System.out.println("TUNED...");
}
}
private static void collectUsage() {
@@ -51,8 +51,7 @@ public class TestManyWhereJoinM2M extends BaseTestCase {
// of a fetch join (if there is a fetch join)
.where().eq("roles.roleName", "role2special").query();
List<MUser> list = query.findList();
System.out.println(list);
query.findList();
String sql = query.getGeneratedSql();
Assert.assertTrue(sql.contains("select distinct"));
@@ -30,9 +30,8 @@ public class TestOneToManyCorrectGrouping extends BaseTestCase {
int count = 0;
while (customerQueryIterator.hasNext()) {
Customer customer = customerQueryIterator.next();
System.out.println(String.format("Customer id %s", customer.getId()));
for (Order order : customer.getOrders()) {
System.out.println(String.format("--> Order id %s", order.getId()));
order.getId();
}
count++;
}
@@ -32,10 +32,9 @@ public class TestQueryFetchSkipPath extends BaseTestCase {
Customer customer = order.getCustomer();
List<Contact> contacts = customer.getContacts();
for (Contact contact : contacts) {
System.out.println("contact:" + contact);
List<ContactNote> notes = contact.getNotes();
for (ContactNote contactNote : notes) {
System.out.println("note:" + contactNote.getTitle());
contactNote.getTitle();
}
}
}
@@ -30,9 +30,7 @@ public class TestQueryFilterManySimple extends BaseTestCase {
.filterMany("orders").eq("status", Order.Status.NEW).gt("orderDate", lastWeek)
.filterMany("contacts").isNotNull("firstName").findList();
System.out.println(list);
// invoke lazy loading
list.get(0).getOrders().size();
System.out.println(list.get(0).getOrders());
}
}
@@ -61,7 +61,10 @@ public class TestQueryFindIterate extends BaseTestCase {
Order order = queryIterator.next();
Customer customer = order.getCustomer();
// invoke lazy loading on customer, order details and order shipments
System.out.println("order: " + order.getId() + " customerName:" + customer.getName()+" details:"+order.getDetails().size()+" shipments:"+order.getShipments().size());
order.getId();
customer.getName();
order.getDetails().size();
order.getShipments().size();
}
} finally {
@@ -89,7 +92,9 @@ public class TestQueryFindIterate extends BaseTestCase {
while (queryIterator.hasNext()) {
Order order = queryIterator.next();
Customer customer = order.getCustomer();
System.out.println("order: " + order.getId() + " customerName:" + customer.getName()+" details:"+order.getDetails().size()+" shipments:"+order.getShipments().size());
customer.getName();
order.getDetails().size();
order.getShipments().size();
}
} finally {
@@ -132,7 +137,10 @@ public class TestQueryFindIterate extends BaseTestCase {
while (queryIterator.hasNext()) {
Order order = queryIterator.next();
Customer customer = order.getCustomer();
System.out.println("order: " + order.getId() + " customerName:" + customer.getName()+" details:"+order.getDetails().size()+" shipments:"+order.getShipments().size());
order.getId();
customer.getName();
order.getDetails().size();
order.getShipments().size();
}
} finally {
@@ -22,7 +22,6 @@ public class TestQueryJoinMulipleMany extends BaseTestCase {
.gt("id", 0).findList();
Assert.assertNotNull(list);
System.out.println(list);
for (Order order : list) {
List<Contact> contacts = order.getCustomer().getContacts();
@@ -22,10 +22,8 @@ public class TestRowCount extends BaseTestCase {
.gt("details.id", 1).order("id desc");
int rc = query.findRowCount();
System.out.println("rc:" + rc);
List<Object> ids = query.findIds();
System.out.println("ids:" + ids);
List<Order> list = query.findList();
System.out.println(list);

Some files were not shown because too many files have changed in this diff Show More