You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@polygene.apache.org by pa...@apache.org on 2017/03/13 15:28:39 UTC

[16/48] polygene-java git commit: New (de)serialization API and SPI & new implementations

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/SQLMapEntityStoreMixin.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/SQLMapEntityStoreMixin.java b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/SQLMapEntityStoreMixin.java
new file mode 100644
index 0000000..95c9c5e
--- /dev/null
+++ b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/SQLMapEntityStoreMixin.java
@@ -0,0 +1,237 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.polygene.entitystore.sql;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Stream;
+import javax.json.Json;
+import javax.sql.DataSource;
+import org.apache.polygene.api.common.Optional;
+import org.apache.polygene.api.configuration.Configuration;
+import org.apache.polygene.api.entity.EntityDescriptor;
+import org.apache.polygene.api.entity.EntityReference;
+import org.apache.polygene.api.injection.scope.Service;
+import org.apache.polygene.api.injection.scope.This;
+import org.apache.polygene.api.injection.scope.Uses;
+import org.apache.polygene.api.service.ServiceActivation;
+import org.apache.polygene.api.service.ServiceDescriptor;
+import org.apache.polygene.library.sql.common.SQLConfiguration;
+import org.apache.polygene.spi.entitystore.EntityNotFoundException;
+import org.apache.polygene.spi.entitystore.EntityStoreException;
+import org.apache.polygene.spi.entitystore.helpers.JSONKeys;
+import org.apache.polygene.spi.entitystore.helpers.MapEntityStore;
+import org.jooq.DSLContext;
+import org.jooq.Field;
+import org.jooq.Query;
+import org.jooq.Record;
+import org.jooq.SQLDialect;
+import org.jooq.Schema;
+import org.jooq.Table;
+import org.jooq.conf.Settings;
+import org.jooq.impl.DSL;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+// TODO Implement optimistic locking! Maybe as a SPI helper
+public class SQLMapEntityStoreMixin
+    implements ServiceActivation, MapEntityStore
+{
+    private static final Logger LOGGER = LoggerFactory.getLogger( SQLMapEntityStoreService.class );
+
+    @Service
+    private DataSource dataSource;
+
+    @Uses
+    private ServiceDescriptor descriptor;
+
+    @This
+    @Optional
+    private Configuration<SQLConfiguration> configuration;
+
+    private Schema schema;
+    private Table<Record> table;
+    private Field<String> identityColumn;
+    private Field<String> versionColumn;
+    private Field<String> stateColumn;
+    private DSLContext dsl;
+
+    @Override
+    public void activateService() throws Exception
+    {
+        SQLDialect dialect = descriptor.metaInfo( SQLDialect.class );
+        Settings settings = descriptor.metaInfo( Settings.class );
+        SQLMapEntityStoreMapping mapping = descriptor.metaInfo( SQLMapEntityStoreMapping.class );
+        String schemaName = getConfiguredSchemaName( mapping.defaultSchemaName() );
+        if( schemaName == null )
+        {
+            throw new EntityStoreException( "Schema name must not be null." );
+        }
+        schema = DSL.schema( DSL.name( schemaName.toUpperCase() ) );
+        table = DSL.table(
+            dialect.equals( SQLDialect.SQLITE )
+            ? DSL.name( mapping.tableName() )
+            : DSL.name( schema.getName(), mapping.tableName() )
+        );
+        identityColumn = DSL.field( mapping.identityColumnName(), String.class );
+        versionColumn = DSL.field( mapping.versionColumnName(), String.class );
+        stateColumn = DSL.field( mapping.stateColumnName(), String.class );
+
+        dsl = DSL.using( dataSource, dialect, settings );
+
+        if( !dialect.equals( SQLDialect.SQLITE )
+            && dsl.meta().getSchemas().stream().noneMatch( s -> schema.getName().equals( s.getName() ) ) )
+        {
+            dsl.createSchema( schema ).execute();
+        }
+
+        if( dsl.meta().getTables().stream().noneMatch( t -> table.getName().equals( t.getName() ) ) )
+        {
+            dsl.createTable( table )
+               .column( identityColumn, mapping.identityDataType().nullable( false ) )
+               .column( versionColumn, mapping.versionDataType().nullable( false ) )
+               .column( stateColumn, mapping.stateDataType().nullable( false ) )
+               .constraint( DSL.constraint( "ENTITY_IDENTITY_CONSTRAINT" ).primaryKey( identityColumn ) )
+               .execute();
+        }
+    }
+
+    @Override
+    public void passivateService() throws Exception
+    {
+        dsl = null;
+        schema = null;
+        table = null;
+        identityColumn = null;
+        versionColumn = null;
+        stateColumn = null;
+    }
+
+    @Override
+    public Reader get( EntityReference entityReference )
+    {
+        String state = dsl.select( stateColumn )
+                          .from( table )
+                          .where( identityColumn.equal( entityReference.identity().toString() ) )
+                          .fetchOptional( stateColumn )
+                          .orElseThrow( () -> new EntityNotFoundException( entityReference ) );
+        return new StringReader( state );
+    }
+
+    @Override
+    public Stream<Reader> entityStates()
+    {
+        return dsl.select( stateColumn )
+                  .from( table )
+                  .fetch( stateColumn )
+                  .stream()
+                  .map( StringReader::new );
+    }
+
+    @Override
+    public void applyChanges( MapChanges changes ) throws Exception
+    {
+        List<Query> operations = new ArrayList<>();
+        changes.visitMap( new MapChanger()
+        {
+            @Override
+            public Writer newEntity( EntityReference ref, EntityDescriptor entityDescriptor )
+            {
+                return new StringWriter( 1000 )
+                {
+                    @Override
+                    public void close() throws IOException
+                    {
+                        super.close();
+                        String state = toString();
+                        String version = Json.createReader( new StringReader( state ) ).readObject()
+                                             .getString( JSONKeys.VERSION );
+                        operations.add(
+                            dsl.insertInto( table )
+                               .columns( identityColumn, versionColumn, stateColumn )
+                               .values( ref.identity().toString(), version, state )
+                        );
+                    }
+                };
+            }
+
+            @Override
+            public Writer updateEntity( MapChange mapChange )
+            {
+                return new StringWriter( 1000 )
+                {
+                    @Override
+                    public void close() throws IOException
+                    {
+                        super.close();
+                        String state = toString();
+                        operations.add(
+                            dsl.update( table )
+                               .set( versionColumn, mapChange.newVersion() )
+                               .set( stateColumn, state )
+                               .where( identityColumn.equal( mapChange.reference().identity().toString() ) )
+                               .and( versionColumn.equal( mapChange.previousVersion() ) )
+                        );
+                    }
+                };
+            }
+
+            @Override
+            public void removeEntity( EntityReference ref, EntityDescriptor entityDescriptor )
+            {
+                operations.add(
+                    dsl.deleteFrom( table )
+                       .where( identityColumn.equal( ref.identity().toString() ) )
+                );
+            }
+        } );
+        dsl.batch( operations ).execute();
+    }
+
+
+    /**
+     * Configuration is optional at both assembly and runtime.
+     */
+    protected String getConfiguredSchemaName( String defaultSchemaName )
+    {
+        if( configuration == null )
+        {
+            Objects.requireNonNull( defaultSchemaName, "default schema name" );
+            LOGGER.debug( "No configuration, will use default schema name: '{}'", defaultSchemaName );
+            return defaultSchemaName;
+        }
+        String result = configuration.get().schemaName().get();
+        if( result == null )
+        {
+            Objects.requireNonNull( defaultSchemaName, "default schema name" );
+            result = defaultSchemaName;
+            LOGGER.debug( "No database schema name in configuration, will use default: '{}'", defaultSchemaName );
+        }
+        else
+        {
+            LOGGER.debug( "Will use configured database schema name: '{}'", result );
+        }
+        return result;
+    }
+}

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/SQLMapEntityStoreService.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/SQLMapEntityStoreService.java b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/SQLMapEntityStoreService.java
new file mode 100644
index 0000000..0ff1e59
--- /dev/null
+++ b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/SQLMapEntityStoreService.java
@@ -0,0 +1,45 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.polygene.entitystore.sql;
+
+import org.apache.polygene.api.concern.Concerns;
+import org.apache.polygene.api.configuration.Configuration;
+import org.apache.polygene.api.mixin.Mixins;
+import org.apache.polygene.api.service.ServiceActivation;
+import org.apache.polygene.api.service.ServiceComposite;
+import org.apache.polygene.spi.entitystore.ConcurrentModificationCheckConcern;
+import org.apache.polygene.spi.entitystore.EntityStateVersions;
+import org.apache.polygene.spi.entitystore.EntityStore;
+import org.apache.polygene.spi.entitystore.StateChangeNotificationConcern;
+import org.apache.polygene.spi.entitystore.helpers.JSONMapEntityStoreActivation;
+import org.apache.polygene.spi.entitystore.helpers.JSONMapEntityStoreMixin;
+
+/**
+ * SQL EntityStore service.
+ */
+@Concerns( { StateChangeNotificationConcern.class, ConcurrentModificationCheckConcern.class } )
+@Mixins( { JSONMapEntityStoreMixin.class, SQLMapEntityStoreMixin.class } )
+public interface SQLMapEntityStoreService
+    extends ServiceActivation,
+    JSONMapEntityStoreActivation,
+    EntityStore,
+    EntityStateVersions,
+    ServiceComposite,
+    Configuration
+{
+}

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/AbstractSQLEntityStoreAssembler.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/AbstractSQLEntityStoreAssembler.java b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/AbstractSQLEntityStoreAssembler.java
deleted file mode 100644
index 1713300..0000000
--- a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/AbstractSQLEntityStoreAssembler.java
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- *  Licensed to the Apache Software Foundation (ASF) under one
- *  or more contributor license agreements.  See the NOTICE file
- *  distributed with this work for additional information
- *  regarding copyright ownership.  The ASF licenses this file
- *  to you under the Apache License, Version 2.0 (the
- *  "License"); you may not use this file except in compliance
- *  with the License.  You may obtain a copy of the License at
- *
- *       http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *
- */
-package org.apache.polygene.entitystore.sql.assembly;
-
-import java.io.IOException;
-import org.apache.polygene.api.common.Visibility;
-import org.apache.polygene.api.identity.Identity;
-import org.apache.polygene.api.identity.StringIdentity;
-import org.apache.polygene.bootstrap.Assemblers;
-import org.apache.polygene.bootstrap.AssemblyException;
-import org.apache.polygene.bootstrap.ModuleAssembly;
-import org.apache.polygene.entitystore.sql.SQLEntityStoreService;
-import org.apache.polygene.entitystore.sql.internal.DatabaseSQLService.DatabaseSQLServiceComposite;
-import org.apache.polygene.entitystore.sql.internal.DatabaseSQLServiceCoreMixin;
-import org.apache.polygene.entitystore.sql.internal.DatabaseSQLServiceSpi;
-import org.apache.polygene.entitystore.sql.internal.DatabaseSQLServiceStatementsMixin;
-import org.apache.polygene.entitystore.sql.internal.DatabaseSQLStringsBuilder;
-import org.apache.polygene.library.sql.common.SQLConfiguration;
-import org.sql.generation.api.vendor.SQLVendor;
-import org.sql.generation.api.vendor.SQLVendorProvider;
-
-/**
- * Base SQL EntityStore assembly.
- */
-@SuppressWarnings( "unchecked" )
-abstract class AbstractSQLEntityStoreAssembler<AssemblerType>
-    extends Assemblers.VisibilityIdentityConfig<AssemblerType>
-{
-
-    public static final Identity DEFAULT_ENTITYSTORE_IDENTITY = new StringIdentity( "entitystore-sql" );
-
-    protected SQLVendor getSQLVendor()
-        throws IOException
-    {
-        return SQLVendorProvider.createVendor( SQLVendor.class );
-    }
-
-    protected Class<?> getDatabaseStringBuilderMixin()
-    {
-        return DatabaseSQLStringsBuilder.CommonMixin.class;
-    }
-
-    protected abstract Class<?> getDatabaseSQLServiceSpecializationMixin();
-
-    @Override
-    public final void assemble( ModuleAssembly module )
-        throws AssemblyException
-    {
-        try
-        {
-            SQLVendor sqlVendor = this.getSQLVendor();
-            if( sqlVendor == null )
-            {
-                throw new AssemblyException( "SQL Vendor could not be determined." );
-            }
-            module.services( DatabaseSQLServiceComposite.class )
-                    .withMixins( DatabaseSQLServiceCoreMixin.class,
-                            DatabaseSQLServiceSpi.CommonMixin.class,
-                            getDatabaseStringBuilderMixin(),
-                            DatabaseSQLServiceStatementsMixin.class,
-                            getDatabaseSQLServiceSpecializationMixin() )
-                    .identifiedBy( ( hasIdentity() ? identity().toString() : DEFAULT_ENTITYSTORE_IDENTITY ).toString() ).
-                visibleIn( Visibility.module ).
-                setMetaInfo( sqlVendor );
-        }
-        catch( IOException ioe )
-        {
-            throw new AssemblyException( ioe );
-        }
-        module.services( SQLEntityStoreService.class ).visibleIn( visibility() );
-        if( hasConfig() )
-        {
-            configModule().entities( SQLConfiguration.class ).visibleIn( configVisibility() );
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/AbstractSQLMapEntityStoreAssembler.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/AbstractSQLMapEntityStoreAssembler.java b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/AbstractSQLMapEntityStoreAssembler.java
new file mode 100644
index 0000000..4e205d1
--- /dev/null
+++ b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/AbstractSQLMapEntityStoreAssembler.java
@@ -0,0 +1,85 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.polygene.entitystore.sql.assembly;
+
+import org.apache.polygene.api.identity.Identity;
+import org.apache.polygene.api.identity.StringIdentity;
+import org.apache.polygene.bootstrap.Assemblers;
+import org.apache.polygene.bootstrap.AssemblyException;
+import org.apache.polygene.bootstrap.ModuleAssembly;
+import org.apache.polygene.entitystore.sql.SQLMapEntityStoreMapping;
+import org.apache.polygene.entitystore.sql.SQLMapEntityStoreService;
+import org.apache.polygene.library.sql.common.SQLConfiguration;
+import org.jooq.SQLDialect;
+import org.jooq.conf.Settings;
+
+/**
+ * Base SQL EntityStore assembly.
+ */
+public abstract class AbstractSQLMapEntityStoreAssembler<AssemblerType>
+    extends Assemblers.VisibilityIdentityConfig<AssemblerType>
+{
+    public static final Identity DEFAULT_ENTITYSTORE_IDENTITY = new StringIdentity( "entitystore-sql" );
+
+    @Override
+    public void assemble( ModuleAssembly module )
+    {
+        SQLDialect dialect = getSQLDialect();
+        if( dialect == null )
+        {
+            throw new AssemblyException( "SQLDialect must not be null" );
+        }
+        Settings settings = getSettings();
+        if( settings == null )
+        {
+            throw new AssemblyException( "Settings must not be null" );
+        }
+        SQLMapEntityStoreMapping mapping = getMapping();
+        if( settings == null )
+        {
+            throw new AssemblyException( "SQLMapEntityStoreSchema must not be null" );
+        }
+
+        module.services( SQLMapEntityStoreService.class )
+              .identifiedBy( ( hasIdentity() ? identity() : DEFAULT_ENTITYSTORE_IDENTITY ).toString() )
+              .visibleIn( visibility() )
+              .setMetaInfo( dialect )
+              .setMetaInfo( settings )
+              .setMetaInfo( mapping );
+
+        if( hasConfig() )
+        {
+            configModule().entities( SQLConfiguration.class ).visibleIn( configVisibility() );
+        }
+    }
+
+    protected Settings getSettings()
+    {
+        return new Settings();
+    }
+
+    protected SQLDialect getSQLDialect()
+    {
+        return SQLDialect.DEFAULT;
+    }
+
+    protected SQLMapEntityStoreMapping getMapping()
+    {
+        return new SQLMapEntityStoreMapping() {};
+    }
+}

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/DerbySQLEntityStoreAssembler.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/DerbySQLEntityStoreAssembler.java b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/DerbySQLEntityStoreAssembler.java
index dc91d76..6288d85 100644
--- a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/DerbySQLEntityStoreAssembler.java
+++ b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/DerbySQLEntityStoreAssembler.java
@@ -19,30 +19,17 @@
  */
 package org.apache.polygene.entitystore.sql.assembly;
 
-import java.io.IOException;
-import org.apache.polygene.entitystore.sql.internal.DerbySQLDatabaseSQLServiceMixin;
-import org.sql.generation.api.vendor.DerbyVendor;
-import org.sql.generation.api.vendor.SQLVendor;
-import org.sql.generation.api.vendor.SQLVendorProvider;
+import org.jooq.SQLDialect;
 
 /**
  * Derby EntityStore assembly.
  */
 public class DerbySQLEntityStoreAssembler
-        extends AbstractSQLEntityStoreAssembler<DerbySQLEntityStoreAssembler>
+    extends AbstractSQLMapEntityStoreAssembler<DerbySQLEntityStoreAssembler>
 {
-
-    @Override
-    protected Class<?> getDatabaseSQLServiceSpecializationMixin()
-    {
-        return DerbySQLDatabaseSQLServiceMixin.class;
-    }
-
     @Override
-    protected SQLVendor getSQLVendor()
-            throws IOException
+    protected SQLDialect getSQLDialect()
     {
-        return SQLVendorProvider.createVendor( DerbyVendor.class );
+        return SQLDialect.DERBY;
     }
-
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/H2SQLEntityStoreAssembler.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/H2SQLEntityStoreAssembler.java b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/H2SQLEntityStoreAssembler.java
index e596fba..018251e 100644
--- a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/H2SQLEntityStoreAssembler.java
+++ b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/H2SQLEntityStoreAssembler.java
@@ -19,30 +19,17 @@
  */
 package org.apache.polygene.entitystore.sql.assembly;
 
-import java.io.IOException;
-import org.apache.polygene.entitystore.sql.internal.H2SQLDatabaseSQLServiceMixin;
-import org.sql.generation.api.vendor.H2Vendor;
-import org.sql.generation.api.vendor.SQLVendor;
-import org.sql.generation.api.vendor.SQLVendorProvider;
+import org.jooq.SQLDialect;
 
 /**
  * H2 EntityStore assembly.
  */
 public class H2SQLEntityStoreAssembler
-        extends AbstractSQLEntityStoreAssembler<H2SQLEntityStoreAssembler>
+    extends AbstractSQLMapEntityStoreAssembler<H2SQLEntityStoreAssembler>
 {
-
-    @Override
-    protected Class<?> getDatabaseSQLServiceSpecializationMixin()
-    {
-        return H2SQLDatabaseSQLServiceMixin.class;
-    }
-
     @Override
-    protected SQLVendor getSQLVendor()
-            throws IOException
+    protected SQLDialect getSQLDialect()
     {
-        return SQLVendorProvider.createVendor( H2Vendor.class );
+        return SQLDialect.H2;
     }
-
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/MySQLEntityStoreAssembler.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/MySQLEntityStoreAssembler.java b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/MySQLEntityStoreAssembler.java
index e3ea6d1..ff46c82 100644
--- a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/MySQLEntityStoreAssembler.java
+++ b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/MySQLEntityStoreAssembler.java
@@ -19,30 +19,17 @@
  */
 package org.apache.polygene.entitystore.sql.assembly;
 
-import java.io.IOException;
-import org.apache.polygene.entitystore.sql.internal.MySQLDatabaseSQLServiceMixin;
-import org.sql.generation.api.vendor.MySQLVendor;
-import org.sql.generation.api.vendor.SQLVendor;
-import org.sql.generation.api.vendor.SQLVendorProvider;
+import org.jooq.SQLDialect;
 
 /**
  * MySQL EntityStore assembly.
  */
 public class MySQLEntityStoreAssembler
-        extends AbstractSQLEntityStoreAssembler<MySQLEntityStoreAssembler>
+    extends AbstractSQLMapEntityStoreAssembler<MySQLEntityStoreAssembler>
 {
-
-    @Override
-    protected Class<?> getDatabaseSQLServiceSpecializationMixin()
-    {
-        return MySQLDatabaseSQLServiceMixin.class;
-    }
-
     @Override
-    protected SQLVendor getSQLVendor()
-            throws IOException
+    protected SQLDialect getSQLDialect()
     {
-        return SQLVendorProvider.createVendor( MySQLVendor.class );
+        return SQLDialect.MYSQL;
     }
-
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/PostgreSQLEntityStoreAssembler.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/PostgreSQLEntityStoreAssembler.java b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/PostgreSQLEntityStoreAssembler.java
index a7f0a23..4400b8e 100644
--- a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/PostgreSQLEntityStoreAssembler.java
+++ b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/PostgreSQLEntityStoreAssembler.java
@@ -19,37 +19,17 @@
  */
 package org.apache.polygene.entitystore.sql.assembly;
 
-import java.io.IOException;
-import org.apache.polygene.entitystore.sql.internal.PostgreSQLDatabaseSQLServiceMixin;
-import org.apache.polygene.entitystore.sql.internal.PostgreSQLStringBuilderMixin;
-import org.sql.generation.api.vendor.PostgreSQLVendor;
-import org.sql.generation.api.vendor.SQLVendor;
-import org.sql.generation.api.vendor.SQLVendorProvider;
+import org.jooq.SQLDialect;
 
 /**
  * PostgreSQL EntityStore assembly.
  */
 public class PostgreSQLEntityStoreAssembler
-        extends AbstractSQLEntityStoreAssembler<PostgreSQLEntityStoreAssembler>
+    extends AbstractSQLMapEntityStoreAssembler<PostgreSQLEntityStoreAssembler>
 {
-
-    @Override
-    protected Class<?> getDatabaseSQLServiceSpecializationMixin()
-    {
-        return PostgreSQLDatabaseSQLServiceMixin.class;
-    }
-
     @Override
-    protected Class<?> getDatabaseStringBuilderMixin()
+    protected SQLDialect getSQLDialect()
     {
-        return PostgreSQLStringBuilderMixin.class;
+        return SQLDialect.POSTGRES;
     }
-
-    @Override
-    protected SQLVendor getSQLVendor()
-            throws IOException
-    {
-        return SQLVendorProvider.createVendor( PostgreSQLVendor.class );
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/SQLiteEntityStoreAssembler.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/SQLiteEntityStoreAssembler.java b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/SQLiteEntityStoreAssembler.java
index 7c043e7..1b7fa5e 100644
--- a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/SQLiteEntityStoreAssembler.java
+++ b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/assembly/SQLiteEntityStoreAssembler.java
@@ -19,30 +19,17 @@
  */
 package org.apache.polygene.entitystore.sql.assembly;
 
-import java.io.IOException;
-import org.apache.polygene.entitystore.sql.internal.SQLiteDatabaseSQLServiceMixin;
-import org.sql.generation.api.vendor.SQLVendor;
-import org.sql.generation.api.vendor.SQLVendorProvider;
-import org.sql.generation.api.vendor.SQLiteVendor;
+import org.jooq.SQLDialect;
 
 /**
  * SQLite EntityStore assembly.
  */
 public class SQLiteEntityStoreAssembler
-        extends AbstractSQLEntityStoreAssembler<SQLiteEntityStoreAssembler>
+    extends AbstractSQLMapEntityStoreAssembler<SQLiteEntityStoreAssembler>
 {
-
-    @Override
-    protected Class<?> getDatabaseSQLServiceSpecializationMixin()
-    {
-        return SQLiteDatabaseSQLServiceMixin.class;
-    }
-
     @Override
-    protected SQLVendor getSQLVendor()
-            throws IOException
+    protected SQLDialect getSQLDialect()
     {
-        return SQLVendorProvider.createVendor( SQLiteVendor.class );
+        return SQLDialect.SQLITE;
     }
-
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DatabaseSQLService.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DatabaseSQLService.java b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DatabaseSQLService.java
index 03ce525..fddb19f 100644
--- a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DatabaseSQLService.java
+++ b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DatabaseSQLService.java
@@ -24,59 +24,32 @@ import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
-import java.time.Instant;
 import org.apache.polygene.api.entity.EntityReference;
 import org.apache.polygene.api.service.ServiceComposite;
 
 @SuppressWarnings( "PublicInnerClass" )
 public interface DatabaseSQLService
 {
-
-    public interface DatabaseSQLServiceComposite
-        extends DatabaseSQLService, ServiceComposite
+    interface DatabaseSQLServiceComposite extends DatabaseSQLService, ServiceComposite
     {
     }
 
-    public final class EntityValueResult
+    final class EntityValueResult
     {
-
-        private final Long entityPK;
-
-        private final Long entityOptimisticLock;
-
         private final Reader reader;
 
-        public EntityValueResult( Long entityPK, Long entityOptimisticLock, Reader reader )
+        EntityValueResult( Reader reader )
         {
-            this.entityPK = entityPK;
-            this.entityOptimisticLock = entityOptimisticLock;
             this.reader = reader;
         }
 
         /**
-         * @return the entityPK
-         */
-        public Long getEntityPK()
-        {
-            return entityPK;
-        }
-
-        /**
-         * @return the entityOptimisticLock
-         */
-        public Long getEntityOptimisticLock()
-        {
-            return entityOptimisticLock;
-        }
-
-        /**
          * @return the reader
          */
         public Reader getReader()
         {
             return reader;
         }
-
     }
 
     void startDatabase()
@@ -109,16 +82,15 @@ public interface DatabaseSQLService
     void populateGetAllEntitiesStatement( PreparedStatement ps )
         throws SQLException;
 
-    void populateInsertEntityStatement( PreparedStatement ps, EntityReference ref, String entity, Instant lastModified )
+    void populateInsertEntityStatement( PreparedStatement ps, EntityReference ref, String entity )
         throws SQLException;
 
-    void populateUpdateEntityStatement( PreparedStatement ps, Long entityPK, Long entityOptimisticLock, EntityReference ref, String entity, Instant lastModified )
+    void populateUpdateEntityStatement( PreparedStatement ps, EntityReference ref, String entity )
         throws SQLException;
 
-    void populateRemoveEntityStatement( PreparedStatement ps, Long entityPK, EntityReference ref )
+    void populateRemoveEntityStatement( PreparedStatement ps, EntityReference ref )
         throws SQLException;
 
-    EntityValueResult getEntityValue( ResultSet rs )
+    Reader getEntityStateReader( ResultSet rs )
         throws SQLException;
-
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DatabaseSQLServiceState.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DatabaseSQLServiceState.java b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DatabaseSQLServiceState.java
index 31c4751..3e3926d 100644
--- a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DatabaseSQLServiceState.java
+++ b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DatabaseSQLServiceState.java
@@ -25,11 +25,9 @@ import org.sql.generation.api.vendor.SQLVendor;
 
 public interface DatabaseSQLServiceState
 {
-
     @Optional
-    public Property<String> schemaName();
+    Property<String> schemaName();
 
     @Optional
-    public Property<SQLVendor> vendor();
-
+    Property<SQLVendor> vendor();
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DatabaseSQLServiceStatementsMixin.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DatabaseSQLServiceStatementsMixin.java b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DatabaseSQLServiceStatementsMixin.java
index 8406e05..cce4568 100644
--- a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DatabaseSQLServiceStatementsMixin.java
+++ b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DatabaseSQLServiceStatementsMixin.java
@@ -22,7 +22,6 @@ package org.apache.polygene.entitystore.sql.internal;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
-import java.time.Instant;
 import org.apache.polygene.api.entity.EntityReference;
 import org.apache.polygene.api.injection.scope.This;
 
@@ -89,30 +88,26 @@ public abstract class DatabaseSQLServiceStatementsMixin
     }
 
     @Override
-    public void populateInsertEntityStatement( PreparedStatement ps, EntityReference ref, String entity, Instant lastModified )
+    public void populateInsertEntityStatement( PreparedStatement ps, EntityReference ref, String entity )
             throws SQLException
     {
         ps.setString( 1, ref.identity().toString() );
         ps.setString( 2, entity );
-        ps.setLong( 3, lastModified.toEpochMilli() );
     }
 
     @Override
-    public void populateRemoveEntityStatement( PreparedStatement ps, Long entityPK, EntityReference ref )
+    public void populateRemoveEntityStatement( PreparedStatement ps, EntityReference ref )
             throws SQLException
     {
-        ps.setLong( 1, entityPK );
+        ps.setString( 1, ref.identity().toString() );
     }
 
     @Override
-    public void populateUpdateEntityStatement( PreparedStatement ps, Long entityPK, Long entityOptimisticLock, EntityReference ref, String entity, Instant lastModified )
+    public void populateUpdateEntityStatement( PreparedStatement ps, EntityReference ref, String entity )
             throws SQLException
     {
-        ps.setLong( 1, entityOptimisticLock + 1 );
+        ps.setString( 1, ref.identity().toString() );
         ps.setString( 2, entity );
-        ps.setLong( 3, lastModified.toEpochMilli() );
-        ps.setLong( 4, entityPK );
-        ps.setLong( 5, entityOptimisticLock );
     }
 
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DatabaseSQLStringsBuilder.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DatabaseSQLStringsBuilder.java b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DatabaseSQLStringsBuilder.java
index f75ba5a..6fb04cb 100644
--- a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DatabaseSQLStringsBuilder.java
+++ b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DatabaseSQLStringsBuilder.java
@@ -1,10 +1,10 @@
 /*
  *  Licensed to the Apache Software Foundation (ASF) under one
  *  or more contributor license agreements.  See the NOTICE file
- *  distributed with this work for additional information
- *  regarding copyright ownership.  The ASF licenses this file
+ *  distributed with work for additional information
+ *  regarding copyright ownership.  The ASF licenses file
  *  to you under the Apache License, Version 2.0 (the
- *  "License"); you may not use this file except in compliance
+ *  "License"); you may not use file except in compliance
  *  with the License.  You may obtain a copy of the License at
  *
  *       http://www.apache.org/licenses/LICENSE-2.0
@@ -29,7 +29,6 @@ import org.sql.generation.api.grammar.builders.modification.DeleteBySearchBuilde
 import org.sql.generation.api.grammar.builders.modification.UpdateBySearchBuilder;
 import org.sql.generation.api.grammar.common.SQLStatement;
 import org.sql.generation.api.grammar.common.datatypes.SQLDataType;
-import org.sql.generation.api.grammar.definition.table.AutoGenerationPolicy;
 import org.sql.generation.api.grammar.definition.table.UniqueSpecification;
 import org.sql.generation.api.grammar.factories.BooleanFactory;
 import org.sql.generation.api.grammar.factories.ColumnsFactory;
@@ -60,91 +59,68 @@ public interface DatabaseSQLStringsBuilder
 
     String buildSQLForRemoveEntityStatement();
 
-    @SuppressWarnings("PublicInnerClass")
     abstract class CommonMixin
         implements DatabaseSQLStringsBuilder
     {
-
         private static final Logger LOGGER = LoggerFactory.getLogger( DatabaseSQLStringsBuilder.class );
 
         @This
-        private DatabaseSQLServiceState _state;
+        private DatabaseSQLServiceState dbState;
 
         private SQLVendor vendor;
-
         private String schemaName;
 
         private String[] schemaCreationSQLs;
-
         private String[] indexCreationSQLs;
-
         private String[] tableCreationSQLs;
 
         private String selectAllEntitiesSQL;
-
         private String selectEntitySQL;
-
         private String insertEntitySQL;
-
         private String updateEntitySQL;
-
         private String removeEntitySQL;
 
         @Override
         public void init()
         {
-            this.vendor = this._state.vendor().get();
-
-            this.schemaName = this._state.schemaName().get();
+            vendor = dbState.vendor().get();
+            schemaName = dbState.schemaName().get();
+            schemaCreationSQLs = toString( createSchemaStatements( vendor ) );
+            indexCreationSQLs = toString( createIndicesStatements( vendor ) );
+            tableCreationSQLs = toString( createTableStatements( vendor ) );
+            selectAllEntitiesSQL = vendor.toString( createSelectAllEntitiesStatement( vendor ) );
+            selectEntitySQL = vendor.toString( createSelectEntityStatement( vendor ) );
+            insertEntitySQL = vendor.toString( createInsertEntityStatement( vendor ) );
+            updateEntitySQL = vendor.toString( createUpdateEntityStatement( vendor ) );
+            removeEntitySQL = vendor.toString( createRemoveEntityStatement( vendor ) );
 
-            this.schemaCreationSQLs = this.toString( this.createSchemaStatements( this.vendor ) );
             if( LOGGER.isTraceEnabled() )
             {
-                LOGGER.trace( "SQL for schema creation: {}", Arrays.asList( this.schemaCreationSQLs ) );
+                LOGGER.trace( "SQL for schema creation: {}", Arrays.asList( schemaCreationSQLs ) );
+                LOGGER.trace( "SQL for index creation: {}", Arrays.asList( indexCreationSQLs ) );
+                LOGGER.trace( "SQL for table creation: {}", Arrays.asList( tableCreationSQLs ) );
+                LOGGER.trace( "SQL for select all entities: {}", selectAllEntitiesSQL );
+                LOGGER.trace( "SQL for select entity: {}", selectEntitySQL );
+                LOGGER.trace( "SQL for insert entity: {}", insertEntitySQL );
+                LOGGER.trace( "SQL for update entity: {}", updateEntitySQL );
+                LOGGER.trace( "SQL for remove entity: {}", removeEntitySQL );
             }
-
-            this.indexCreationSQLs = this.toString( this.createIndicesStatements( this.vendor ) );
-            if( LOGGER.isTraceEnabled() )
-            {
-                LOGGER.trace( "SQL for index creation: {}", Arrays.asList( this.indexCreationSQLs ) );
-            }
-
-            this.tableCreationSQLs = this.toString( this.createTableStatements( this.vendor ) );
-            if( LOGGER.isTraceEnabled() )
-            {
-                LOGGER.trace( "SQL for table creation: {}", Arrays.asList( this.tableCreationSQLs ) );
-            }
-
-            this.selectAllEntitiesSQL = this.vendor.toString( this.createSelectAllEntitiesStatement( this.vendor ) );
-            LOGGER.trace( "SQL for select all entities: {}", this.selectAllEntitiesSQL );
-
-            this.selectEntitySQL = this.vendor.toString( this.createSelectEntityStatement( this.vendor ) );
-            LOGGER.trace( "SQL for select entity: {}", this.selectEntitySQL );
-
-            this.insertEntitySQL = this.vendor.toString( this.createInsertEntityStatement( this.vendor ) );
-            LOGGER.trace( "SQL for insert entity: {}", this.insertEntitySQL );
-
-            this.updateEntitySQL = this.vendor.toString( this.createUpdateEntityStatement( this.vendor ) );
-            LOGGER.trace( "SQL for update entity: {}", this.updateEntitySQL );
-
-            this.removeEntitySQL = this.vendor.toString( this.createRemoveEntityStatement( this.vendor ) );
-            LOGGER.trace( "SQL for remove entity: {}", this.removeEntitySQL );
         }
 
         protected String[] toString( SQLStatement[] stmts )
         {
-            List<String> result = new ArrayList<String>();
+            List<String> result = new ArrayList<>();
             if( stmts != null )
             {
                 for( Integer idx = 0; idx < stmts.length; ++idx )
                 {
-                    SQLStatement statement = stmts[idx];
+                    SQLStatement statement = stmts[ idx ];
                     if( statement != null )
                     {
-                        String stringStatement = this.vendor.toString( statement );
+                        String stringStatement = vendor.toString( statement );
                         if( stringStatement != null && stringStatement.length() > 0 )
                         {
-                            result.add( this.vendor.toString( statement ) );
+                            result.add( vendor.toString( statement ) );
                         }
                     }
                 }
@@ -154,29 +130,24 @@ public interface DatabaseSQLStringsBuilder
 
         protected SQLVendor getVendor()
         {
-            return this.vendor;
+            return vendor;
         }
 
         protected String getSchemaName()
         {
-            return this.schemaName;
+            return schemaName;
         }
 
         protected SQLStatement[] createSchemaStatements( SQLVendor vendor )
         {
-            // @formatter:off
-            return new SQLStatement[]
-            {
+            return new SQLStatement[] {
                 vendor.getDefinitionFactory().createSchemaDefinitionBuilder()
-                .setSchemaName( this.schemaName )
-                .createExpression()
+                      .setSchemaName( schemaName ).createExpression()
             };
-            // @formatter:on
         }
 
         protected SQLStatement[] createIndicesStatements( SQLVendor vendor )
         {
-            // TODO
             return new SQLStatement[] {};
         }
 
@@ -185,31 +156,25 @@ public interface DatabaseSQLStringsBuilder
             DefinitionFactory d = vendor.getDefinitionFactory();
             TableReferenceFactory t = vendor.getTableReferenceFactory();
 
-
-            // @formatter:off
-            return new SQLStatement[]
-            {
+            return new SQLStatement[] {
                 d.createTableDefinitionBuilder()
-                    .setTableName( t.tableName( this.getSchemaName(), SQLs.TABLE_NAME ) )
-                    .setTableContentsSource( d.createTableElementListBuilder()
-                        .addTableElement( d.createColumnDefinition( SQLs.ENTITY_PK_COLUMN_NAME, this.getPKType(), false, AutoGenerationPolicy.BY_DEFAULT ) )
-                        .addTableElement( d.createColumnDefinition( SQLs.ENTITY_OPTIMISTIC_LOCK_COLUMN_NAME, this.getOptimisticLockType(), false ) )
-                        .addTableElement( d.createColumnDefinition( SQLs.ENTITY_IDENTITY_COLUMN_NAME, this.getIDType(), false ) )
-                        .addTableElement( d.createColumnDefinition( SQLs.ENTITY_STATE_COLUMN_NAME, this.getStateType(), false ) )
-                        .addTableElement( d.createColumnDefinition( SQLs.ENTITY_LAST_MODIFIED_COLUMN_NAME, this.getLastModifiedType(), false ) )
-                        .addTableElement( d.createTableConstraintDefinition( d.createUniqueConstraintBuilder()
-                            .setUniqueness( UniqueSpecification.PRIMARY_KEY )
-                            .addColumns( SQLs.ENTITY_PK_COLUMN_NAME )
-                            .createExpression() ) )
-                        .addTableElement( d.createTableConstraintDefinition( d.createUniqueConstraintBuilder()
-                            .setUniqueness( UniqueSpecification.UNIQUE )
-                            .addColumns( SQLs.ENTITY_IDENTITY_COLUMN_NAME )
-                            .createExpression() ) )
-                        .createExpression()
-                        )
-                   .createExpression()
+                 .setTableName( t.tableName( getSchemaName(), SQLs.TABLE_NAME ) )
+                 .setTableContentsSource(
+                     d.createTableElementListBuilder()
+                      .addTableElement( d.createColumnDefinition( SQLs.ENTITY_IDENTITY_COLUMN_NAME,
+                                                                  getIDType(), false ) )
+                      .addTableElement( d.createColumnDefinition( SQLs.ENTITY_VERSION_COLUMN_NAME,
+                                                                  getVersionType(), false ) )
+                      .addTableElement( d.createColumnDefinition( SQLs.ENTITY_STATE_COLUMN_NAME,
+                                                                  getStateType(), false ) )
+                      .addTableElement( d.createTableConstraintDefinition(
+                          d.createUniqueConstraintBuilder()
+                           .setUniqueness( UniqueSpecification.PRIMARY_KEY )
+                           .addColumns( SQLs.ENTITY_IDENTITY_COLUMN_NAME )
+                           .createExpression() )
+                      ).createExpression()
+                 ).createExpression()
             };
-            // @formatter:on
         }
 
         protected SQLStatement createSelectAllEntitiesStatement( SQLVendor vendor )
@@ -217,12 +182,10 @@ public interface DatabaseSQLStringsBuilder
             QueryFactory q = vendor.getQueryFactory();
             TableReferenceFactory t = vendor.getTableReferenceFactory();
 
-            // @formatter:off
             return q.simpleQueryBuilder()
-                .select( SQLs.ENTITY_PK_COLUMN_NAME, SQLs.ENTITY_OPTIMISTIC_LOCK_COLUMN_NAME, SQLs.ENTITY_STATE_COLUMN_NAME )
-                .from( t.tableName( this.schemaName, SQLs.TABLE_NAME ) )
-                .createExpression();
-            // @formatter:on
+                    .select( SQLs.ENTITY_STATE_COLUMN_NAME )
+                    .from( t.tableName( schemaName, SQLs.TABLE_NAME ) )
+                    .createExpression();
         }
 
         protected SQLStatement createSelectEntityStatement( SQLVendor vendor )
@@ -233,13 +196,11 @@ public interface DatabaseSQLStringsBuilder
             ColumnsFactory c = vendor.getColumnsFactory();
             LiteralFactory l = vendor.getLiteralFactory();
 
-            // @formatter:off
             return q.simpleQueryBuilder()
-                .select( SQLs.ENTITY_PK_COLUMN_NAME, SQLs.ENTITY_OPTIMISTIC_LOCK_COLUMN_NAME, SQLs.ENTITY_STATE_COLUMN_NAME )
-                .from( t.tableName( this.schemaName, SQLs.TABLE_NAME ) )
-                .where( b.eq( c.colName( SQLs.ENTITY_IDENTITY_COLUMN_NAME ), l.param() ) )
-                .createExpression();
-            // @formatter:on
+                    .select( SQLs.ENTITY_STATE_COLUMN_NAME )
+                    .from( t.tableName( schemaName, SQLs.TABLE_NAME ) )
+                    .where( b.eq( c.colName( SQLs.ENTITY_IDENTITY_COLUMN_NAME ), l.param() ) )
+                    .createExpression();
         }
 
         protected SQLStatement createInsertEntityStatement( SQLVendor vendor )
@@ -248,26 +209,15 @@ public interface DatabaseSQLStringsBuilder
             TableReferenceFactory t = vendor.getTableReferenceFactory();
             LiteralFactory l = vendor.getLiteralFactory();
 
-            // @formatter:off
             return m.insert()
-                .setTableName( t.tableName( this.schemaName, SQLs.TABLE_NAME ) )
-                .setColumnSource( m.columnSourceByValues()
-                    .addColumnNames(
-                        SQLs.ENTITY_OPTIMISTIC_LOCK_COLUMN_NAME,
-                        SQLs.ENTITY_IDENTITY_COLUMN_NAME,
-                        SQLs.ENTITY_STATE_COLUMN_NAME,
-                        SQLs.ENTITY_LAST_MODIFIED_COLUMN_NAME
-                        )
-                    .addValues(
-                        l.n( 0 ),
-                        l.param(),
-                        l.param(),
-                        l.param()
-                        )
-                    .createExpression()
-                    )
-                 .createExpression();
-            // @formatter:on
+                    .setTableName( t.tableName( schemaName, SQLs.TABLE_NAME ) )
+                    .setColumnSource( m.columnSourceByValues()
+                                       .addColumnNames( SQLs.ENTITY_IDENTITY_COLUMN_NAME,
+                                                        SQLs.ENTITY_STATE_COLUMN_NAME )
+                                       .addValues( l.param(),
+                                                   l.param() )
+                                       .createExpression()
+                    ).createExpression();
         }
 
         protected SQLStatement createUpdateEntityStatement( SQLVendor vendor )
@@ -278,20 +228,18 @@ public interface DatabaseSQLStringsBuilder
             BooleanFactory b = vendor.getBooleanFactory();
             ColumnsFactory c = vendor.getColumnsFactory();
 
-            // @formatter:off
-            UpdateBySearchBuilder builder = m.updateBySearch()
-                .setTargetTable( m.createTargetTable( t.tableName( this.schemaName, SQLs.TABLE_NAME ) ) )
-                .addSetClauses(
-                    m.setClause( SQLs.ENTITY_OPTIMISTIC_LOCK_COLUMN_NAME, m.updateSourceByExp( l.param() ) ),
-                    m.setClause( SQLs.ENTITY_STATE_COLUMN_NAME, m.updateSourceByExp( l.param() ) ),
-                    m.setClause( SQLs.ENTITY_LAST_MODIFIED_COLUMN_NAME, m.updateSourceByExp( l.param() ) )
-                    );
-            builder
-                .getWhereBuilder()
-                    .reset( b.eq( c.colName( SQLs.ENTITY_PK_COLUMN_NAME ), l.param() ) )
-                    .and( b.eq( c.colName( SQLs.ENTITY_OPTIMISTIC_LOCK_COLUMN_NAME ), l.param() ) );
+            UpdateBySearchBuilder builder = m.updateBySearch().setTargetTable(
+                m.createTargetTable( t.tableName( schemaName, SQLs.TABLE_NAME ) )
+            ).addSetClauses(
+                m.setClause( SQLs.ENTITY_VERSION_COLUMN_NAME, m.updateSourceByExp( l.param() ) ),
+                m.setClause( SQLs.ENTITY_STATE_COLUMN_NAME, m.updateSourceByExp( l.param() ) )
+            );
+            builder.getWhereBuilder().reset(
+                b.eq( c.colName( SQLs.ENTITY_IDENTITY_COLUMN_NAME ), l.param() )
+            ).and(
+                b.eq( c.colName( SQLs.ENTITY_VERSION_COLUMN_NAME ), l.param() )
+            );
             return builder.createExpression();
-            // @formatter:on
         }
 
         protected SQLStatement createRemoveEntityStatement( SQLVendor vendor )
@@ -302,88 +250,76 @@ public interface DatabaseSQLStringsBuilder
             BooleanFactory b = vendor.getBooleanFactory();
             ColumnsFactory c = vendor.getColumnsFactory();
 
-            // @formatter:off
-            DeleteBySearchBuilder builder = m.deleteBySearch()
-                .setTargetTable( m.createTargetTable( t.tableName( this.schemaName, SQLs.TABLE_NAME ) ) );
-            builder.getWhere()
-                .reset( b.eq( c.colName( SQLs.ENTITY_PK_COLUMN_NAME ), l.param() ) );
+            DeleteBySearchBuilder builder = m.deleteBySearch().setTargetTable(
+                m.createTargetTable( t.tableName( schemaName, SQLs.TABLE_NAME ) )
+            );
+            builder.getWhere().reset(
+                b.eq( c.colName( SQLs.ENTITY_IDENTITY_COLUMN_NAME ), l.param() )
+            );
             return builder.createExpression();
-            // @formatter:on
-        }
-
-        protected SQLDataType getPKType()
-        {
-            return this.vendor.getDataTypeFactory().bigInt();
-        }
-
-        protected SQLDataType getOptimisticLockType()
-        {
-            return this.vendor.getDataTypeFactory().bigInt();
         }
 
         protected SQLDataType getIDType()
         {
-            return this.vendor.getDataTypeFactory().sqlVarChar( 64 );
+            return vendor.getDataTypeFactory().sqlVarChar( 64 );
         }
 
-        protected SQLDataType getStateType()
+        protected SQLDataType getVersionType()
         {
-            return this.vendor.getDataTypeFactory().sqlVarChar( 10000 );
+            return vendor.getDataTypeFactory().sqlVarChar( 64 );
         }
 
-        protected SQLDataType getLastModifiedType()
+        protected SQLDataType getStateType()
         {
-            return this.vendor.getDataTypeFactory().bigInt();
+            return vendor.getDataTypeFactory().sqlVarChar( 10000 );
         }
 
         @Override
         public String[] buildSQLForSchemaCreation()
         {
-            return this.schemaCreationSQLs;
+            return schemaCreationSQLs;
         }
 
         @Override
         public String[] buildSQLForIndexCreation()
         {
-            return this.indexCreationSQLs;
+            return indexCreationSQLs;
         }
 
         @Override
         public String buildSQLForSelectAllEntitiesStatement()
         {
-            return this.selectAllEntitiesSQL;
+            return selectAllEntitiesSQL;
         }
 
         @Override
         public String buildSQLForSelectEntityStatement()
         {
-            return this.selectEntitySQL;
+            return selectEntitySQL;
         }
 
         @Override
         public String buildSQLForInsertEntityStatement()
         {
-            return this.insertEntitySQL;
+            return insertEntitySQL;
         }
 
         @Override
         public String buildSQLForUpdateEntityStatement()
         {
-            return this.updateEntitySQL;
+            return updateEntitySQL;
         }
 
         @Override
         public String buildSQLForRemoveEntityStatement()
         {
-            return this.removeEntitySQL;
+            return removeEntitySQL;
         }
 
         @Override
         public String[] buildSQLForTableCreation()
         {
-            return this.tableCreationSQLs;
+            return tableCreationSQLs;
         }
-
     }
-
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DerbySQLDatabaseSQLServiceMixin.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DerbySQLDatabaseSQLServiceMixin.java b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DerbySQLDatabaseSQLServiceMixin.java
index 527cdab..083d79e 100644
--- a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DerbySQLDatabaseSQLServiceMixin.java
+++ b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/DerbySQLDatabaseSQLServiceMixin.java
@@ -19,6 +19,7 @@
  */
 package org.apache.polygene.entitystore.sql.internal;
 
+import java.io.Reader;
 import java.io.StringReader;
 import java.sql.Connection;
 import java.sql.ResultSet;
@@ -28,11 +29,10 @@ import org.apache.polygene.library.sql.common.SQLUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@SuppressWarnings("ProtectedField")
+@SuppressWarnings( "ProtectedField" )
 public abstract class DerbySQLDatabaseSQLServiceMixin
     implements DatabaseSQLService, DatabaseSQLStringsBuilder, DatabaseSQLServiceSpi
 {
-
     private static final Logger LOGGER = LoggerFactory.getLogger( DerbySQLDatabaseSQLServiceMixin.class );
 
     @This
@@ -46,10 +46,8 @@ public abstract class DerbySQLDatabaseSQLServiceMixin
         try
         {
             String tableNameForQuery = SQLs.TABLE_NAME.toUpperCase();
-            rs = connection.getMetaData().getTables( null, null, tableNameForQuery, new String[]
-            {
-                "TABLE"
-            } );
+            rs = connection.getMetaData().getTables( null, null, tableNameForQuery,
+                                                     new String[] { "TABLE" } );
             boolean tableExists = rs.next();
             LOGGER.trace( "Found table {}? {}", tableNameForQuery, tableExists );
             return tableExists;
@@ -61,12 +59,9 @@ public abstract class DerbySQLDatabaseSQLServiceMixin
     }
 
     @Override
-    public EntityValueResult getEntityValue( ResultSet rs )
+    public Reader getEntityStateReader( ResultSet rs )
         throws SQLException
     {
-        return new EntityValueResult( rs.getLong( SQLs.ENTITY_PK_COLUMN_NAME ),
-            rs.getLong( SQLs.ENTITY_OPTIMISTIC_LOCK_COLUMN_NAME ), new StringReader(
-                rs.getString( SQLs.ENTITY_STATE_COLUMN_NAME ) ) );
+        return new StringReader( rs.getString( SQLs.ENTITY_STATE_COLUMN_NAME ) );
     }
-
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/H2SQLDatabaseSQLServiceMixin.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/H2SQLDatabaseSQLServiceMixin.java b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/H2SQLDatabaseSQLServiceMixin.java
index ba34b36..3b870c6 100644
--- a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/H2SQLDatabaseSQLServiceMixin.java
+++ b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/H2SQLDatabaseSQLServiceMixin.java
@@ -19,6 +19,7 @@
  */
 package org.apache.polygene.entitystore.sql.internal;
 
+import java.io.Reader;
 import java.io.StringReader;
 import java.sql.Connection;
 import java.sql.ResultSet;
@@ -29,35 +30,35 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public abstract class H2SQLDatabaseSQLServiceMixin
-        implements DatabaseSQLService, DatabaseSQLStringsBuilder, DatabaseSQLServiceSpi
+    implements DatabaseSQLService, DatabaseSQLStringsBuilder, DatabaseSQLServiceSpi
 {
-
     private static final Logger LOGGER = LoggerFactory.getLogger( H2SQLDatabaseSQLServiceMixin.class );
 
     @This
     protected DatabaseSQLServiceSpi spi;
 
     public boolean tableExists( Connection connection )
-            throws SQLException
+        throws SQLException
     {
         ResultSet rs = null;
-        try {
+        try
+        {
             String tableNameForQuery = SQLs.TABLE_NAME.toUpperCase();
-            rs = connection.getMetaData().getTables( null, null, tableNameForQuery, new String[]{ "TABLE" } );
+            rs = connection.getMetaData().getTables( null, null, tableNameForQuery,
+                                                     new String[] { "TABLE" } );
             boolean tableExists = rs.next();
             LOGGER.trace( "Found table {}? {}", tableNameForQuery, tableExists );
             return tableExists;
-        } finally {
+        }
+        finally
+        {
             SQLUtil.closeQuietly( rs );
         }
     }
 
-    public EntityValueResult getEntityValue( ResultSet rs )
-            throws SQLException
+    public Reader getEntityStateReader( ResultSet rs )
+        throws SQLException
     {
-        return new EntityValueResult( rs.getLong( SQLs.ENTITY_PK_COLUMN_NAME ),
-                                      rs.getLong( SQLs.ENTITY_OPTIMISTIC_LOCK_COLUMN_NAME ),
-                                      new StringReader( rs.getString( SQLs.ENTITY_STATE_COLUMN_NAME ) ) );
+        return new StringReader( rs.getString( SQLs.ENTITY_STATE_COLUMN_NAME ) );
     }
-
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/MySQLDatabaseSQLServiceMixin.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/MySQLDatabaseSQLServiceMixin.java b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/MySQLDatabaseSQLServiceMixin.java
index da3479d..1c6c534 100644
--- a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/MySQLDatabaseSQLServiceMixin.java
+++ b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/MySQLDatabaseSQLServiceMixin.java
@@ -19,6 +19,7 @@
  */
 package org.apache.polygene.entitystore.sql.internal;
 
+import java.io.Reader;
 import java.io.StringReader;
 import java.sql.Connection;
 import java.sql.ResultSet;
@@ -28,7 +29,7 @@ import org.apache.polygene.library.sql.common.SQLUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@SuppressWarnings("ProtectedField")
+@SuppressWarnings( "ProtectedField" )
 public abstract class MySQLDatabaseSQLServiceMixin
     implements DatabaseSQLService, DatabaseSQLStringsBuilder, DatabaseSQLServiceSpi
 {
@@ -45,11 +46,9 @@ public abstract class MySQLDatabaseSQLServiceMixin
         ResultSet rs = null;
         try
         {
-            String tableNameForQuery = SQLs.TABLE_NAME;
-            rs = connection.getMetaData().getTables( null, null, tableNameForQuery, new String[]
-            {
-                "TABLE"
-            } );
+            String tableNameForQuery = SQLs.TABLE_NAME.toUpperCase();
+            rs = connection.getMetaData().getTables( null, null, tableNameForQuery,
+                                                     new String[] { "TABLE" } );
             boolean tableExists = rs.next();
             LOGGER.trace( "Found table {}? {}", tableNameForQuery, tableExists );
             return tableExists;
@@ -61,12 +60,9 @@ public abstract class MySQLDatabaseSQLServiceMixin
     }
 
     @Override
-    public EntityValueResult getEntityValue( ResultSet rs )
+    public Reader getEntityStateReader( ResultSet rs )
         throws SQLException
     {
-        return new EntityValueResult( rs.getLong( SQLs.ENTITY_PK_COLUMN_NAME ),
-            rs.getLong( SQLs.ENTITY_OPTIMISTIC_LOCK_COLUMN_NAME ), new StringReader(
-                rs.getString( SQLs.ENTITY_STATE_COLUMN_NAME ) ) );
+        return new StringReader( rs.getString( SQLs.ENTITY_STATE_COLUMN_NAME ) );
     }
-
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/PostgreSQLDatabaseSQLServiceMixin.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/PostgreSQLDatabaseSQLServiceMixin.java b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/PostgreSQLDatabaseSQLServiceMixin.java
index 9052146..a4cf014 100644
--- a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/PostgreSQLDatabaseSQLServiceMixin.java
+++ b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/PostgreSQLDatabaseSQLServiceMixin.java
@@ -19,6 +19,7 @@
  */
 package org.apache.polygene.entitystore.sql.internal;
 
+import java.io.Reader;
 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
@@ -28,9 +29,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public abstract class PostgreSQLDatabaseSQLServiceMixin
-        implements DatabaseSQLServiceSpi, DatabaseSQLStringsBuilder, DatabaseSQLService
+    implements DatabaseSQLServiceSpi, DatabaseSQLStringsBuilder, DatabaseSQLService
 {
-
     private static final Logger LOGGER = LoggerFactory.getLogger( PostgreSQLDatabaseSQLServiceMixin.class );
 
     @This
@@ -38,31 +38,27 @@ public abstract class PostgreSQLDatabaseSQLServiceMixin
 
     @Override
     public boolean tableExists( Connection connection )
-            throws SQLException
+        throws SQLException
     {
         ResultSet rs = null;
-        try {
-
-            rs = connection.getMetaData().getTables( null,
-                                                     this.spi.getCurrentSchemaName(),
-                                                     SQLs.TABLE_NAME,
-                                                     new String[]{ "TABLE" } );
+        try
+        {
+            rs = connection.getMetaData().getTables( null, spi.getCurrentSchemaName(), SQLs.TABLE_NAME,
+                                                     new String[] { "TABLE" } );
             boolean tableExists = rs.next();
             LOGGER.trace( "Found table {}? {}", SQLs.TABLE_NAME, tableExists );
             return tableExists;
-
-        } finally {
+        }
+        finally
+        {
             SQLUtil.closeQuietly( rs );
         }
     }
 
     @Override
-    public EntityValueResult getEntityValue( ResultSet rs )
-            throws SQLException
+    public Reader getEntityStateReader( ResultSet rs )
+        throws SQLException
     {
-        return new EntityValueResult( rs.getLong( SQLs.ENTITY_PK_COLUMN_NAME ),
-                                      rs.getLong( SQLs.ENTITY_OPTIMISTIC_LOCK_COLUMN_NAME ),
-                                      rs.getCharacterStream( SQLs.ENTITY_STATE_COLUMN_NAME ) );
+        return rs.getCharacterStream( SQLs.ENTITY_STATE_COLUMN_NAME );
     }
-
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/SQLiteDatabaseSQLServiceMixin.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/SQLiteDatabaseSQLServiceMixin.java b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/SQLiteDatabaseSQLServiceMixin.java
index 2715a00..d762f8b 100644
--- a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/SQLiteDatabaseSQLServiceMixin.java
+++ b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/SQLiteDatabaseSQLServiceMixin.java
@@ -19,6 +19,7 @@
  */
 package org.apache.polygene.entitystore.sql.internal;
 
+import java.io.Reader;
 import java.io.StringReader;
 import java.sql.Connection;
 import java.sql.ResultSet;
@@ -29,9 +30,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public abstract class SQLiteDatabaseSQLServiceMixin
-        implements DatabaseSQLService, DatabaseSQLStringsBuilder, DatabaseSQLServiceSpi
+    implements DatabaseSQLService, DatabaseSQLStringsBuilder, DatabaseSQLServiceSpi
 {
-
     private static final Logger LOGGER = LoggerFactory.getLogger( SQLiteDatabaseSQLServiceMixin.class );
 
     @This
@@ -39,27 +39,28 @@ public abstract class SQLiteDatabaseSQLServiceMixin
 
     @Override
     public boolean tableExists( Connection connection )
-            throws SQLException
+        throws SQLException
     {
         ResultSet rs = null;
-        try {
+        try
+        {
             String tableNameForQuery = SQLs.TABLE_NAME.toUpperCase();
-            rs = connection.getMetaData().getTables( null, null, tableNameForQuery, new String[]{ "TABLE" } );
+            rs = connection.getMetaData().getTables( null, null, tableNameForQuery,
+                                                     new String[] { "TABLE" } );
             boolean tableExists = rs.next();
             LOGGER.trace( "Found table {}? {}", tableNameForQuery, tableExists );
             return tableExists;
-        } finally {
+        }
+        finally
+        {
             SQLUtil.closeQuietly( rs );
         }
     }
 
     @Override
-    public EntityValueResult getEntityValue( ResultSet rs )
-            throws SQLException
+    public Reader getEntityStateReader( ResultSet rs )
+        throws SQLException
     {
-        return new EntityValueResult( rs.getLong( SQLs.ENTITY_PK_COLUMN_NAME ),
-                                      rs.getLong( SQLs.ENTITY_OPTIMISTIC_LOCK_COLUMN_NAME ),
-                                      new StringReader( rs.getString( SQLs.ENTITY_STATE_COLUMN_NAME ) ) );
+        return new StringReader( rs.getString( SQLs.ENTITY_STATE_COLUMN_NAME ) );
     }
-
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/SQLs.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/SQLs.java b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/SQLs.java
index bcadde1..8555a5d 100644
--- a/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/SQLs.java
+++ b/extensions/entitystore-sql/src/main/java/org/apache/polygene/entitystore/sql/internal/SQLs.java
@@ -21,19 +21,9 @@ package org.apache.polygene.entitystore.sql.internal;
 
 public interface SQLs
 {
-
-    String DEFAULT_SCHEMA_NAME = "polygene_es";
-
-    String TABLE_NAME = "polygene_entities";
-
-    String ENTITY_PK_COLUMN_NAME = "entity_pk";
-
-    String ENTITY_IDENTITY_COLUMN_NAME = "entity_id";
-
-    String ENTITY_STATE_COLUMN_NAME = "entity_state";
-
-    String ENTITY_OPTIMISTIC_LOCK_COLUMN_NAME = "entity_optimistic_lock";
-
-    String ENTITY_LAST_MODIFIED_COLUMN_NAME = "entity_last_modified";
-
+    String DEFAULT_SCHEMA_NAME = "POLYGENE_ES";
+    String TABLE_NAME = "POLYGENE_ENTITIES";
+    String ENTITY_IDENTITY_COLUMN_NAME = "ENTITY_IDENTITY";
+    String ENTITY_VERSION_COLUMN_NAME = "ENTITY_VERSION";
+    String ENTITY_STATE_COLUMN_NAME = "ENTITY_STATE";
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/test/java/org/apache/polygene/entitystore/sql/DerbySQLEntityStoreTest.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/test/java/org/apache/polygene/entitystore/sql/DerbySQLEntityStoreTest.java b/extensions/entitystore-sql/src/test/java/org/apache/polygene/entitystore/sql/DerbySQLEntityStoreTest.java
index 498fb41..6d849cc 100644
--- a/extensions/entitystore-sql/src/test/java/org/apache/polygene/entitystore/sql/DerbySQLEntityStoreTest.java
+++ b/extensions/entitystore-sql/src/test/java/org/apache/polygene/entitystore/sql/DerbySQLEntityStoreTest.java
@@ -19,11 +19,9 @@
  */
 package org.apache.polygene.entitystore.sql;
 
-import java.io.File;
 import java.sql.Connection;
 import java.sql.Statement;
 import javax.sql.DataSource;
-import org.apache.derby.iapi.services.io.FileUtil;
 import org.apache.polygene.api.common.Visibility;
 import org.apache.polygene.api.unitofwork.UnitOfWork;
 import org.apache.polygene.api.usecase.UsecaseBuilder;
@@ -36,7 +34,6 @@ import org.apache.polygene.library.sql.common.SQLConfiguration;
 import org.apache.polygene.library.sql.dbcp.DBCPDataSourceServiceAssembler;
 import org.apache.polygene.test.EntityTestAssembler;
 import org.apache.polygene.test.entity.AbstractEntityStoreTest;
-import org.apache.polygene.valueserialization.orgjson.OrgJsonValueSerializationAssembler;
 
 public class DerbySQLEntityStoreTest
     extends AbstractEntityStoreTest
@@ -50,29 +47,28 @@ public class DerbySQLEntityStoreTest
         super.assemble( module );
         ModuleAssembly config = module.layer().module( "config" );
         new EntityTestAssembler().assemble( config );
-        new OrgJsonValueSerializationAssembler().assemble( module );
 
         // START SNIPPET: assembly
         // DataSourceService
-        new DBCPDataSourceServiceAssembler().
-            identifiedBy( "derby-datasource-service" ).
-            visibleIn( Visibility.module ).
-            withConfig( config, Visibility.layer ).
-            assemble( module );
+        new DBCPDataSourceServiceAssembler()
+            .identifiedBy( "derby-datasource-service" )
+            .visibleIn( Visibility.module )
+            .withConfig( config, Visibility.layer )
+            .assemble( module );
 
         // DataSource
-        new DataSourceAssembler().
-            withDataSourceServiceIdentity( "derby-datasource-service" ).
-            identifiedBy( "derby-datasource" ).
-            visibleIn( Visibility.module ).
-            withCircuitBreaker().
-            assemble( module );
+        new DataSourceAssembler()
+            .withDataSourceServiceIdentity( "derby-datasource-service" )
+            .identifiedBy( "derby-datasource" )
+            .visibleIn( Visibility.module )
+            .withCircuitBreaker()
+            .assemble( module );
 
         // SQL EntityStore
-        new DerbySQLEntityStoreAssembler().
-            visibleIn( Visibility.application ).
-            withConfig( config, Visibility.layer ).
-            assemble( module );
+        new DerbySQLEntityStoreAssembler()
+            .visibleIn( Visibility.application )
+            .withConfig( config, Visibility.layer )
+            .assemble( module );
     }
     // END SNIPPET: assembly
 
@@ -84,7 +80,8 @@ public class DerbySQLEntityStoreTest
             "Delete " + getClass().getSimpleName() + " test data" ) );
         try
         {
-            SQLConfiguration config = uow.get( SQLConfiguration.class, DerbySQLEntityStoreAssembler.DEFAULT_ENTITYSTORE_IDENTITY );
+            SQLConfiguration config = uow.get( SQLConfiguration.class,
+                                               DerbySQLEntityStoreAssembler.DEFAULT_ENTITYSTORE_IDENTITY );
             Connection connection = serviceFinder.findService( DataSource.class ).get().getConnection();
             connection.setAutoCommit( false );
             String schemaName = config.schemaName().get();
@@ -94,10 +91,9 @@ public class DerbySQLEntityStoreTest
             }
             try( Statement stmt = connection.createStatement() )
             {
-                stmt.execute( String.format( "DELETE FROM %s." + SQLs.TABLE_NAME, schemaName ) );
+                stmt.execute( String.format( "DELETE FROM %s.%s", schemaName, SQLs.TABLE_NAME ) );
                 connection.commit();
             }
-            FileUtil.removeDirectory( new File( "target/polygene-data" ) );
         }
         finally
         {
@@ -105,5 +101,4 @@ public class DerbySQLEntityStoreTest
             super.tearDown();
         }
     }
-
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/test/java/org/apache/polygene/entitystore/sql/H2SQLEntityStoreTest.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/test/java/org/apache/polygene/entitystore/sql/H2SQLEntityStoreTest.java b/extensions/entitystore-sql/src/test/java/org/apache/polygene/entitystore/sql/H2SQLEntityStoreTest.java
index 8b54b3d..1aed71f 100644
--- a/extensions/entitystore-sql/src/test/java/org/apache/polygene/entitystore/sql/H2SQLEntityStoreTest.java
+++ b/extensions/entitystore-sql/src/test/java/org/apache/polygene/entitystore/sql/H2SQLEntityStoreTest.java
@@ -19,8 +19,6 @@
  */
 package org.apache.polygene.entitystore.sql;
 
-import java.io.File;
-import org.apache.derby.iapi.services.io.FileUtil;
 import org.apache.polygene.api.common.Visibility;
 import org.apache.polygene.bootstrap.AssemblyException;
 import org.apache.polygene.bootstrap.ModuleAssembly;
@@ -29,7 +27,6 @@ import org.apache.polygene.library.sql.assembly.DataSourceAssembler;
 import org.apache.polygene.library.sql.dbcp.DBCPDataSourceServiceAssembler;
 import org.apache.polygene.test.EntityTestAssembler;
 import org.apache.polygene.test.entity.AbstractEntityStoreTest;
-import org.apache.polygene.valueserialization.orgjson.OrgJsonValueSerializationAssembler;
 
 public class H2SQLEntityStoreTest
     extends AbstractEntityStoreTest
@@ -43,44 +40,28 @@ public class H2SQLEntityStoreTest
         super.assemble( module );
         ModuleAssembly config = module.layer().module( "config" );
         new EntityTestAssembler().assemble( config );
-        new OrgJsonValueSerializationAssembler().assemble( module );
 
         // START SNIPPET: assembly
         // DataSourceService
-        new DBCPDataSourceServiceAssembler().
-            identifiedBy( "h2-datasource-service" ).
-            visibleIn( Visibility.module ).
-            withConfig( config, Visibility.layer ).
-            assemble( module );
+        new DBCPDataSourceServiceAssembler()
+            .identifiedBy( "h2-datasource-service" )
+            .visibleIn( Visibility.module )
+            .withConfig( config, Visibility.layer )
+            .assemble( module );
 
         // DataSource
-        new DataSourceAssembler().
-            withDataSourceServiceIdentity( "h2-datasource-service" ).
-            identifiedBy( "h2-datasource" ).
-            visibleIn( Visibility.module ).
-            withCircuitBreaker().
-            assemble( module );
+        new DataSourceAssembler()
+            .withDataSourceServiceIdentity( "h2-datasource-service" )
+            .identifiedBy( "h2-datasource" )
+            .visibleIn( Visibility.module )
+            .withCircuitBreaker()
+            .assemble( module );
 
         // SQL EntityStore
-        new H2SQLEntityStoreAssembler().
-            visibleIn( Visibility.application ).
-            withConfig( config, Visibility.layer ).
-            assemble( module );
+        new H2SQLEntityStoreAssembler()
+            .visibleIn( Visibility.application )
+            .withConfig( config, Visibility.layer )
+            .assemble( module );
     }
     // END SNIPPET: assembly
-
-    @Override
-    public void tearDown()
-        throws Exception
-    {
-        try
-        {
-            FileUtil.removeDirectory( new File( "target/polygene-data" ) );
-        }
-        finally
-        {
-            super.tearDown();
-        }
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/test/java/org/apache/polygene/entitystore/sql/MySQLEntityStoreTest.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/test/java/org/apache/polygene/entitystore/sql/MySQLEntityStoreTest.java b/extensions/entitystore-sql/src/test/java/org/apache/polygene/entitystore/sql/MySQLEntityStoreTest.java
index 0718ddb..059301c 100644
--- a/extensions/entitystore-sql/src/test/java/org/apache/polygene/entitystore/sql/MySQLEntityStoreTest.java
+++ b/extensions/entitystore-sql/src/test/java/org/apache/polygene/entitystore/sql/MySQLEntityStoreTest.java
@@ -36,7 +36,6 @@ import org.apache.polygene.library.sql.datasource.DataSourceConfiguration;
 import org.apache.polygene.library.sql.dbcp.DBCPDataSourceServiceAssembler;
 import org.apache.polygene.test.EntityTestAssembler;
 import org.apache.polygene.test.entity.AbstractEntityStoreTest;
-import org.apache.polygene.valueserialization.orgjson.OrgJsonValueSerializationAssembler;
 import org.junit.ClassRule;
 
 public class MySQLEntityStoreTest
@@ -64,29 +63,28 @@ public class MySQLEntityStoreTest
         super.assemble( module );
         ModuleAssembly config = module.layer().module( "config" );
         new EntityTestAssembler().assemble( config );
-        new OrgJsonValueSerializationAssembler().assemble( module );
 
         // START SNIPPET: assembly
         // DataSourceService
-        new DBCPDataSourceServiceAssembler().
-            identifiedBy( "mysql-datasource-service" ).
-            visibleIn( Visibility.module ).
-            withConfig( config, Visibility.layer ).
-            assemble( module );
+        new DBCPDataSourceServiceAssembler()
+            .identifiedBy( "mysql-datasource-service" )
+            .visibleIn( Visibility.module )
+            .withConfig( config, Visibility.layer )
+            .assemble( module );
 
         // DataSource
-        new DataSourceAssembler().
-            withDataSourceServiceIdentity( "mysql-datasource-service" ).
-            identifiedBy( "mysql-datasource" ).
-            visibleIn( Visibility.module ).
-            withCircuitBreaker().
-            assemble( module );
+        new DataSourceAssembler()
+            .withDataSourceServiceIdentity( "mysql-datasource-service" )
+            .identifiedBy( "mysql-datasource" )
+            .visibleIn( Visibility.module )
+            .withCircuitBreaker()
+            .assemble( module );
 
         // SQL EntityStore
-        new MySQLEntityStoreAssembler().
-            visibleIn( Visibility.application ).
-            withConfig( config, Visibility.layer ).
-            assemble( module );
+        new MySQLEntityStoreAssembler()
+            .visibleIn( Visibility.application )
+            .withConfig( config, Visibility.layer )
+            .assemble( module );
         // END SNIPPET: assembly
         String mysqlHost = DOCKER.getDockerHost();
         int mysqlPort = DOCKER.getExposedContainerPort( "3306/tcp" );

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/7c2814ee/extensions/entitystore-sql/src/test/java/org/apache/polygene/entitystore/sql/PostgreSQLEntityStoreTest.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/test/java/org/apache/polygene/entitystore/sql/PostgreSQLEntityStoreTest.java b/extensions/entitystore-sql/src/test/java/org/apache/polygene/entitystore/sql/PostgreSQLEntityStoreTest.java
index 49d5a13..c6dd48c 100644
--- a/extensions/entitystore-sql/src/test/java/org/apache/polygene/entitystore/sql/PostgreSQLEntityStoreTest.java
+++ b/extensions/entitystore-sql/src/test/java/org/apache/polygene/entitystore/sql/PostgreSQLEntityStoreTest.java
@@ -36,7 +36,6 @@ import org.apache.polygene.library.sql.datasource.DataSourceConfiguration;
 import org.apache.polygene.library.sql.dbcp.DBCPDataSourceServiceAssembler;
 import org.apache.polygene.test.EntityTestAssembler;
 import org.apache.polygene.test.entity.AbstractEntityStoreTest;
-import org.apache.polygene.valueserialization.orgjson.OrgJsonValueSerializationAssembler;
 import org.junit.ClassRule;
 
 /**
@@ -88,29 +87,28 @@ public class PostgreSQLEntityStoreTest
         super.assemble( module );
         ModuleAssembly config = module.layer().module( "config" );
         new EntityTestAssembler().assemble( config );
-        new OrgJsonValueSerializationAssembler().assemble( module );
 
         // START SNIPPET: assembly
         // DataSourceService
-        new DBCPDataSourceServiceAssembler().
-            identifiedBy( "postgresql-datasource-service" ).
-            visibleIn( Visibility.module ).
-            withConfig( config, Visibility.layer ).
-            assemble( module );
+        new DBCPDataSourceServiceAssembler()
+            .identifiedBy( "postgresql-datasource-service" )
+            .visibleIn( Visibility.module )
+            .withConfig( config, Visibility.layer )
+            .assemble( module );
 
         // DataSource
-        new DataSourceAssembler().
-            withDataSourceServiceIdentity( "postgresql-datasource-service" ).
-            identifiedBy( "postgresql-datasource" ).
-            visibleIn( Visibility.module ).
-            withCircuitBreaker().
-            assemble( module );
+        new DataSourceAssembler()
+            .withDataSourceServiceIdentity( "postgresql-datasource-service" )
+            .identifiedBy( "postgresql-datasource" )
+            .visibleIn( Visibility.module )
+            .withCircuitBreaker()
+            .assemble( module );
 
         // SQL EntityStore
-        new PostgreSQLEntityStoreAssembler().
-            visibleIn( Visibility.application ).
-            withConfig( config, Visibility.layer ).
-            assemble( module );
+        new PostgreSQLEntityStoreAssembler()
+            .visibleIn( Visibility.application )
+            .withConfig( config, Visibility.layer )
+            .assemble( module );
         // END SNIPPET: assembly
         String host = DOCKER.getDockerHost();
         int port = DOCKER.getExposedContainerPort( "5432/tcp" );
@@ -129,7 +127,8 @@ public class PostgreSQLEntityStoreTest
         );
         try
         {
-            SQLConfiguration config = uow.get( SQLConfiguration.class, PostgreSQLEntityStoreAssembler.DEFAULT_ENTITYSTORE_IDENTITY );
+            SQLConfiguration config = uow.get( SQLConfiguration.class,
+                                               PostgreSQLEntityStoreAssembler.DEFAULT_ENTITYSTORE_IDENTITY );
             Connection connection = serviceFinder.findService( DataSource.class ).get().getConnection();
             connection.setAutoCommit( false );
             String schemaName = config.schemaName().get();
@@ -139,7 +138,7 @@ public class PostgreSQLEntityStoreTest
             }
             try( Statement stmt = connection.createStatement() )
             {
-                stmt.execute( String.format( "DELETE FROM %s." + SQLs.TABLE_NAME, schemaName ) );
+                stmt.execute( String.format( "DROP SCHEMA \"%s\" CASCADE", schemaName ) );
                 connection.commit();
             }
         }
@@ -149,5 +148,4 @@ public class PostgreSQLEntityStoreTest
             super.tearDown();
         }
     }
-
 }