Bladeren bron

AMBARI-16242. Hive View and Pig View : one user overriding job details of other user in database (Nitiraj Rathore via pallavkul)

Pallav Kulshreshtha 9 jaren geleden
bovenliggende
commit
008c821967

+ 1 - 1
ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog240.java

@@ -390,7 +390,7 @@ public class UpgradeCatalog240 extends AbstractUpgradeCatalog {
    * query each dynamic table to find the max of id
    * insert into ambari_sequence name and counter for each item
    */
-  private void updateSequenceForView() {
+  protected void updateSequenceForView() {
     LOG.info("updateSequenceForView called.");
     EntityManager entityManager = getEntityManagerProvider().get();
     TypedQuery<ViewEntityEntity> viewEntityQuery = entityManager.createQuery("SELECT vee FROM ViewEntityEntity vee", ViewEntityEntity.class);

+ 3 - 0
ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog240Test.java

@@ -451,6 +451,7 @@ public class UpgradeCatalog240Test {
     Method updateHIVEConfigs = UpgradeCatalog240.class.getDeclaredMethod("updateHIVEConfigs");
     Method updateAmsConfigs = UpgradeCatalog240.class.getDeclaredMethod("updateAMSConfigs");
     Method updateClusterEnv = UpgradeCatalog240.class.getDeclaredMethod("updateClusterEnv");
+    Method updateSequenceForView = UpgradeCatalog240.class.getDeclaredMethod("updateSequenceForView");
     Method updateHostRoleCommandTableDML = UpgradeCatalog240.class.getDeclaredMethod("updateHostRoleCommandTableDML");
     Method updateKerberosEnv = UpgradeCatalog240.class.getDeclaredMethod("updateKerberosConfigs");
     Method updateYarnEnv = UpgradeCatalog240.class.getDeclaredMethod("updateYarnEnv");
@@ -481,6 +482,7 @@ public class UpgradeCatalog240Test {
             .addMockedMethod(updateHIVEConfigs)
             .addMockedMethod(updateAmsConfigs)
             .addMockedMethod(updateClusterEnv)
+            .addMockedMethod(updateSequenceForView)
             .addMockedMethod(updateHostRoleCommandTableDML)
             .addMockedMethod(updateKerberosEnv)
             .addMockedMethod(updateYarnEnv)
@@ -506,6 +508,7 @@ public class UpgradeCatalog240Test {
     upgradeCatalog240.updateHIVEConfigs();
     upgradeCatalog240.updateAMSConfigs();
     upgradeCatalog240.updateClusterEnv();
+    upgradeCatalog240.updateSequenceForView();
     upgradeCatalog240.updateHostRoleCommandTableDML();
     upgradeCatalog240.updateKerberosConfigs();
     upgradeCatalog240.updateYarnEnv();

+ 246 - 89
ambari-server/src/test/java/org/apache/ambari/server/view/persistence/DataStoreImplTest.java

@@ -7,7 +7,7 @@
  * "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
+ * 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,
@@ -33,14 +33,27 @@ import org.apache.ambari.server.view.configuration.ViewConfig;
 import org.apache.ambari.server.view.configuration.ViewConfigTest;
 import org.apache.ambari.view.PersistenceException;
 import org.easymock.Capture;
+import org.easymock.EasyMock;
+import org.easymock.IAnswer;
+import org.easymock.IExpectationSetters;
 import org.eclipse.persistence.dynamic.DynamicClassLoader;
 import org.eclipse.persistence.dynamic.DynamicEntity;
 import org.eclipse.persistence.dynamic.DynamicType;
+import org.eclipse.persistence.jpa.JpaEntityManager;
+import org.eclipse.persistence.jpa.JpaHelper;
 import org.eclipse.persistence.jpa.dynamic.JPADynamicHelper;
+import org.eclipse.persistence.sequencing.Sequence;
+import org.eclipse.persistence.sessions.DatabaseLogin;
 import org.eclipse.persistence.sessions.DatabaseSession;
+import org.eclipse.persistence.sessions.Session;
+import org.eclipse.persistence.sessions.server.ServerSession;
 import org.eclipse.persistence.tools.schemaframework.SchemaManager;
 import org.junit.Assert;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.powermock.api.easymock.PowerMock;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
 
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
@@ -53,6 +66,7 @@ import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
 
+import static org.easymock.EasyMock.anyObject;
 import static org.easymock.EasyMock.capture;
 import static org.easymock.EasyMock.createMock;
 import static org.easymock.EasyMock.createNiceMock;
@@ -64,25 +78,27 @@ import static org.easymock.EasyMock.verify;
 /**
  * DataStoreImpl tests.
  */
+@RunWith(PowerMockRunner.class)               // Allow mocking static methods
+@PrepareForTest(JpaHelper.class)
 public class DataStoreImplTest {
   private final static String xml = "<view>\n" +
-      "    <name>MY_VIEW</name>\n" +
-      "    <label>My View!</label>\n" +
-      "    <version>1.0.0</version>\n" +
-      "    <instance>\n" +
-      "        <name>INSTANCE1</name>\n" +
-      "    </instance>\n" +
-      "    <persistence>\n" +
-      "      <entity>\n" +
-      "        <class>org.apache.ambari.server.view.persistence.DataStoreImplTest$TestEntity</class>\n" +
-      "        <id-property>id</id-property>\n" +
-      "      </entity>\n" +
-      "      <entity>\n" +
-      "        <class>org.apache.ambari.server.view.persistence.DataStoreImplTest$TestSubEntity</class>\n" +
-      "        <id-property>name</id-property>\n" +
-      "      </entity>\n" +
-      "    </persistence>" +
-      "</view>";
+    "    <name>MY_VIEW</name>\n" +
+    "    <label>My View!</label>\n" +
+    "    <version>1.0.0</version>\n" +
+    "    <instance>\n" +
+    "        <name>INSTANCE1</name>\n" +
+    "    </instance>\n" +
+    "    <persistence>\n" +
+    "      <entity>\n" +
+    "        <class>org.apache.ambari.server.view.persistence.DataStoreImplTest$TestEntity</class>\n" +
+    "        <id-property>id</id-property>\n" +
+    "      </entity>\n" +
+    "      <entity>\n" +
+    "        <class>org.apache.ambari.server.view.persistence.DataStoreImplTest$TestSubEntity</class>\n" +
+    "        <id-property>id</id-property>\n" +
+    "      </entity>\n" +
+    "    </persistence>" +
+    "</view>";
 
   @Test
   public void testStore_create() throws Exception {
@@ -90,30 +106,52 @@ public class DataStoreImplTest {
 
     // create mocks
     EntityManagerFactory entityManagerFactory = createMock(EntityManagerFactory.class);
+    JpaEntityManager jpaEntityManager = createMock(JpaEntityManager.class);
+    ServerSession session = createMock(ServerSession.class);
+    DatabaseLogin databaseLogin = createMock(DatabaseLogin.class);
     EntityManager entityManager = createMock(EntityManager.class);
     JPADynamicHelper jpaDynamicHelper = createNiceMock(JPADynamicHelper.class);
     SchemaManager schemaManager = createNiceMock(SchemaManager.class);
     EntityTransaction transaction = createMock(EntityTransaction.class);
 
     // set expectations
-    Capture<DynamicType> typeCapture = new Capture<DynamicType>();
-    Capture<DynamicType> typeCapture2 = new Capture<DynamicType>();
-    jpaDynamicHelper.addTypes(eq(true), eq(true), capture(typeCapture), capture(typeCapture2));
-
-    expect(entityManagerFactory.createEntityManager()).andReturn(entityManager);
-    expect(entityManager.getTransaction()).andReturn(transaction).anyTimes();
-
-    Capture<Class> entityClassCapture = new Capture<Class>();
-    expect(entityManager.find(capture(entityClassCapture), eq("bar"))).andReturn(null);
-
-    Capture<Class> entityClassCapture2 = new Capture<Class>();
-    expect(entityManager.find(capture(entityClassCapture2), eq(99))).andReturn(null);
+    PowerMock.mockStatic(JpaHelper.class);
+    expect(JpaHelper.getEntityManager(entityManager)).andReturn(jpaEntityManager).anyTimes();
+    PowerMock.replay(JpaHelper.class);
+    expect(jpaEntityManager.getServerSession()).andReturn(session).anyTimes();
+    expect(session.getLogin()).andReturn(databaseLogin).anyTimes();
+    Capture<Sequence> sequenceCapture = new Capture<Sequence>();
+    databaseLogin.addSequence(capture(sequenceCapture));
+    EasyMock.expectLastCall().anyTimes();
 
     Capture<DynamicEntity> entityCapture = new Capture<DynamicEntity>();
     entityManager.persist(capture(entityCapture));
+    EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
+      @Override
+      public Object answer() throws Throwable {
+        ((DynamicEntity) EasyMock.getCurrentArguments()[0])
+          .set("DS_id", 99); // for TestSubEntity
+        return null;
+      }
+    });
 
     Capture<DynamicEntity> entityCapture2 = new Capture<DynamicEntity>();
     entityManager.persist(capture(entityCapture2));
+    EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
+      @Override
+      public Object answer() throws Throwable {
+        ((DynamicEntity) EasyMock.getCurrentArguments()[0])
+          .set("DS_id", 100); // for TestEntity
+        return null;
+      }
+    });
+
+    Capture<DynamicType> typeCapture = new Capture<DynamicType>();
+    Capture<DynamicType> typeCapture2 = new Capture<DynamicType>();
+    jpaDynamicHelper.addTypes(eq(true), eq(true), capture(typeCapture), capture(typeCapture2));
+
+    expect(entityManagerFactory.createEntityManager()).andReturn(entityManager).anyTimes();
+    expect(entityManager.getTransaction()).andReturn(transaction).anyTimes();
 
     entityManager.close();
 
@@ -121,22 +159,20 @@ public class DataStoreImplTest {
     transaction.commit();
 
     // replay mocks
-    replay(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager);
+    replay(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, jpaEntityManager, session, databaseLogin);
 
     DataStoreImpl dataStore = getDataStore(entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager);
 
-    dataStore.store(new TestEntity(99, "foo", new TestSubEntity("bar")));
-
-    Assert.assertEquals(entityClassCapture.getValue(), typeCapture.getValue().getJavaClass());
-    Assert.assertEquals(entityClassCapture2.getValue(), typeCapture2.getValue().getJavaClass());
+    dataStore.store(new TestEntity("foo", new TestSubEntity("bar")));
 
     Assert.assertEquals("bar", entityCapture.getValue().get("DS_name"));
+    Assert.assertEquals(99, entityCapture.getValue().get("DS_id"));
 
-    Assert.assertEquals(99, entityCapture2.getValue().get("DS_id"));
+    Assert.assertEquals(100, entityCapture2.getValue().get("DS_id"));
     Assert.assertEquals("foo", entityCapture2.getValue().get("DS_name"));
 
     // verify mocks
-    verify(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager);
+    verify(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, jpaEntityManager, session, databaseLogin);
   }
 
   @Test
@@ -144,6 +180,9 @@ public class DataStoreImplTest {
     DynamicClassLoader classLoader = new DynamicClassLoader(DataStoreImplTest.class.getClassLoader());
 
     // create mocks
+    JpaEntityManager jpaEntityManager = createMock(JpaEntityManager.class);
+    ServerSession session = createMock(ServerSession.class);
+    DatabaseLogin databaseLogin = createMock(DatabaseLogin.class);
     EntityManagerFactory entityManagerFactory = createMock(EntityManagerFactory.class);
     EntityManager entityManager = createMock(EntityManager.class);
     JPADynamicHelper jpaDynamicHelper = createNiceMock(JPADynamicHelper.class);
@@ -151,16 +190,22 @@ public class DataStoreImplTest {
     EntityTransaction transaction = createMock(EntityTransaction.class);
 
     // set expectations
+    PowerMock.mockStatic(JpaHelper.class);
+    expect(JpaHelper.getEntityManager(entityManager)).andReturn(jpaEntityManager).anyTimes();
+    PowerMock.replay(JpaHelper.class);
+    expect(jpaEntityManager.getServerSession()).andReturn(session).anyTimes();
+    expect(session.getLogin()).andReturn(databaseLogin).anyTimes();
+    Capture<Sequence> sequenceCapture = new Capture<Sequence>();
+    databaseLogin.addSequence(capture(sequenceCapture));
+    EasyMock.expectLastCall().anyTimes();
+
     Capture<DynamicType> typeCapture = new Capture<DynamicType>();
     Capture<DynamicType> typeCapture2 = new Capture<DynamicType>();
     jpaDynamicHelper.addTypes(eq(true), eq(true), capture(typeCapture), capture(typeCapture2));
 
-    expect(entityManagerFactory.createEntityManager()).andReturn(entityManager);
+    expect(entityManagerFactory.createEntityManager()).andReturn(entityManager).anyTimes();
     expect(entityManager.getTransaction()).andReturn(transaction).anyTimes();
 
-    Capture<Class> entityClassCapture2 = new Capture<Class>();
-    expect(entityManager.find(capture(entityClassCapture2), eq(99))).andReturn(null);
-
     entityManager.close();
 
     transaction.begin();
@@ -168,7 +213,7 @@ public class DataStoreImplTest {
     transaction.rollback();
 
     // replay mocks
-    replay(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager);
+    replay(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, jpaEntityManager, session, databaseLogin);
 
     DataStoreImpl dataStore = getDataStore(entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager);
 
@@ -179,13 +224,13 @@ public class DataStoreImplTest {
     String longString = sb.toString();
 
     try {
-      dataStore.store(new TestEntity(99, longString, new TestSubEntity("bar")));
+      dataStore.store(new TestEntity(longString, new TestSubEntity("bar")));
       Assert.fail("Expected PersistenceException.");
     } catch (PersistenceException e) {
       // expected
     }
     // verify mocks
-    verify(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager);
+    verify(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, jpaEntityManager, session, databaseLogin);
   }
 
   @Test
@@ -193,6 +238,10 @@ public class DataStoreImplTest {
     DynamicClassLoader classLoader = new DynamicClassLoader(DataStoreImplTest.class.getClassLoader());
 
     // create mocks
+    JpaEntityManager jpaEntityManager = createMock(JpaEntityManager.class);
+    ServerSession session = createMock(ServerSession.class);
+    DatabaseLogin databaseLogin = createMock(DatabaseLogin.class);
+
     EntityManagerFactory entityManagerFactory = createMock(EntityManagerFactory.class);
     EntityManager entityManager = createMock(EntityManager.class);
     JPADynamicHelper jpaDynamicHelper = createNiceMock(JPADynamicHelper.class);
@@ -200,11 +249,20 @@ public class DataStoreImplTest {
     EntityTransaction transaction = createMock(EntityTransaction.class);
 
     // set expectations
+    PowerMock.mockStatic(JpaHelper.class);
+    expect(JpaHelper.getEntityManager(entityManager)).andReturn(jpaEntityManager).anyTimes();
+    PowerMock.replay(JpaHelper.class);
+    expect(jpaEntityManager.getServerSession()).andReturn(session).anyTimes();
+    expect(session.getLogin()).andReturn(databaseLogin).anyTimes();
+    Capture<Sequence> sequenceCapture = new Capture<Sequence>();
+    databaseLogin.addSequence(capture(sequenceCapture));
+    EasyMock.expectLastCall().anyTimes();
+
     Capture<DynamicType> typeCapture = new Capture<DynamicType>();
     Capture<DynamicType> typeCapture2 = new Capture<DynamicType>();
     jpaDynamicHelper.addTypes(eq(true), eq(true), capture(typeCapture), capture(typeCapture2));
 
-    expect(entityManagerFactory.createEntityManager()).andReturn(entityManager);
+    expect(entityManagerFactory.createEntityManager()).andReturn(entityManager).anyTimes();
     expect(entityManager.getTransaction()).andReturn(transaction).anyTimes();
 
     entityManager.close();
@@ -214,18 +272,18 @@ public class DataStoreImplTest {
     transaction.rollback();
 
     // replay mocks
-    replay(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager);
+    replay(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, jpaEntityManager, session, databaseLogin);
 
     DataStoreImpl dataStore = getDataStore(entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager);
 
     try {
-      dataStore.store(new TestLargeEntity(99));
+      dataStore.store(new TestLargeEntity());
       Assert.fail("Expected PersistenceException.");
     } catch (PersistenceException e) {
       // expected
     }
     // verify mocks
-    verify(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager);
+    verify(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, jpaEntityManager, session, databaseLogin);
   }
 
   @Test
@@ -233,53 +291,74 @@ public class DataStoreImplTest {
     DynamicClassLoader classLoader = new DynamicClassLoader(DataStoreImplTest.class.getClassLoader());
 
     // create mocks
+    JpaEntityManager jpaEntityManager = createMock(JpaEntityManager.class);
+    ServerSession session = createMock(ServerSession.class);
+    DatabaseLogin databaseLogin = createMock(DatabaseLogin.class);
     EntityManagerFactory entityManagerFactory = createMock(EntityManagerFactory.class);
     EntityManager entityManager = createMock(EntityManager.class);
     JPADynamicHelper jpaDynamicHelper = createNiceMock(JPADynamicHelper.class);
     SchemaManager schemaManager = createNiceMock(SchemaManager.class);
     EntityTransaction transaction = createMock(EntityTransaction.class);
     DynamicEntity dynamicEntity = createMock(DynamicEntity.class);
+    DynamicEntity dynamicSubEntity = createMock(DynamicEntity.class);
 
     // set expectations
+    PowerMock.mockStatic(JpaHelper.class);
+    expect(JpaHelper.getEntityManager(entityManager)).andReturn(jpaEntityManager).anyTimes();
+    PowerMock.replay(JpaHelper.class);
+    expect(jpaEntityManager.getServerSession()).andReturn(session).anyTimes();
+    expect(session.getLogin()).andReturn(databaseLogin).anyTimes();
+    Capture<Sequence> sequenceCapture = new Capture<Sequence>();
+    databaseLogin.addSequence(capture(sequenceCapture));
+    EasyMock.expectLastCall().anyTimes();
+
     Capture<DynamicType> typeCapture = new Capture<DynamicType>();
     Capture<DynamicType> typeCapture2 = new Capture<DynamicType>();
     jpaDynamicHelper.addTypes(eq(true), eq(true), capture(typeCapture), capture(typeCapture2));
 
-    expect(entityManagerFactory.createEntityManager()).andReturn(entityManager);
+    expect(entityManagerFactory.createEntityManager()).andReturn(entityManager).anyTimes();
     expect(entityManager.getTransaction()).andReturn(transaction).anyTimes();
 
     Capture<Class> entityClassCapture = new Capture<Class>();
-    expect(entityManager.find(capture(entityClassCapture), eq("bar"))).andReturn(null);
+    expect(entityManager.find(capture(entityClassCapture), eq(100))).andReturn(dynamicEntity);
 
     Capture<Class> entityClassCapture2 = new Capture<Class>();
-    expect(entityManager.find(capture(entityClassCapture2), eq(99))).andReturn(dynamicEntity);
-
-    Capture<DynamicEntity> entityCapture = new Capture<DynamicEntity>();
-    entityManager.persist(capture(entityCapture));
+    expect(entityManager.find(capture(entityClassCapture2), eq(99))).andReturn(dynamicSubEntity);
 
     entityManager.close();
 
-    expect(dynamicEntity.set("DS_id", 99)).andReturn(dynamicEntity);
+    expect(dynamicEntity.set("DS_id", 100)).andReturn(dynamicEntity);
     expect(dynamicEntity.set("DS_name", "foo")).andReturn(dynamicEntity);
 
+    expect(dynamicSubEntity.set("DS_id", 99)).andReturn(dynamicSubEntity);
+    expect(dynamicSubEntity.set("DS_name", "bar")).andReturn(dynamicSubEntity);
+
     Capture<DynamicEntity> subEntityCapture = new Capture<DynamicEntity>();
-    expect(dynamicEntity.set(eq("DS_subEntity"), capture(subEntityCapture))).andReturn(dynamicEntity);
+    expect(dynamicEntity.set(eq("DS_subEntity"), capture(subEntityCapture))).andReturn(dynamicSubEntity);
+
+    expect(dynamicEntity.get("DS_id")).andReturn(100);
+    expect(dynamicEntity.get("DS_name")).andReturn("foo");
+    expect(dynamicEntity.get("DS_subEntity")).andReturn(dynamicSubEntity);
+    expect(dynamicEntity.get("DS_class")).andReturn(dynamicEntity.getClass());
+
+    expect(dynamicSubEntity.get("DS_id")).andReturn(99);
+    expect(dynamicSubEntity.get("DS_name")).andReturn("bar");
 
     transaction.begin();
     transaction.commit();
 
     // replay mocks
-    replay(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, dynamicEntity);
+    replay(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, dynamicEntity, jpaEntityManager, session, databaseLogin, dynamicSubEntity);
 
     DataStoreImpl dataStore = getDataStore(entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager);
 
-    dataStore.store(new TestEntity(99, "foo", new TestSubEntity("bar")));
+    dataStore.store(new TestEntity(100, "foo", new TestSubEntity(99, "bar")));
 
     Assert.assertEquals(entityClassCapture.getValue(), typeCapture.getValue().getJavaClass());
     Assert.assertEquals(entityClassCapture2.getValue(), typeCapture2.getValue().getJavaClass());
 
     // verify mocks
-    verify(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, dynamicEntity);
+    verify(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, dynamicEntity, jpaEntityManager, session, databaseLogin, dynamicSubEntity);
   }
 
   @Test
@@ -287,6 +366,9 @@ public class DataStoreImplTest {
     DynamicClassLoader classLoader = new DynamicClassLoader(DataStoreImplTest.class.getClassLoader());
 
     // create mocks
+    JpaEntityManager jpaEntityManager = createMock(JpaEntityManager.class);
+    ServerSession session = createMock(ServerSession.class);
+    DatabaseLogin databaseLogin = createMock(DatabaseLogin.class);
     EntityManagerFactory entityManagerFactory = createMock(EntityManagerFactory.class);
     EntityManager entityManager = createMock(EntityManager.class);
     JPADynamicHelper jpaDynamicHelper = createNiceMock(JPADynamicHelper.class);
@@ -295,11 +377,20 @@ public class DataStoreImplTest {
     DynamicEntity dynamicEntity = createMock(DynamicEntity.class);
 
     // set expectations
+    PowerMock.mockStatic(JpaHelper.class);
+    expect(JpaHelper.getEntityManager(entityManager)).andReturn(jpaEntityManager).anyTimes();
+    PowerMock.replay(JpaHelper.class);
+    expect(jpaEntityManager.getServerSession()).andReturn(session).anyTimes();
+    expect(session.getLogin()).andReturn(databaseLogin).anyTimes();
+    Capture<Sequence> sequenceCapture = new Capture<Sequence>();
+    databaseLogin.addSequence(capture(sequenceCapture));
+    EasyMock.expectLastCall().anyTimes();
+
     Capture<DynamicType> typeCapture = new Capture<DynamicType>();
     Capture<DynamicType> typeCapture2 = new Capture<DynamicType>();
     jpaDynamicHelper.addTypes(eq(true), eq(true), capture(typeCapture), capture(typeCapture2));
 
-    expect(entityManagerFactory.createEntityManager()).andReturn(entityManager);
+    expect(entityManagerFactory.createEntityManager()).andReturn(entityManager).anyTimes();
     expect(entityManager.getTransaction()).andReturn(transaction).anyTimes();
 
     Capture<Class> entityClassCapture2 = new Capture<Class>();
@@ -313,14 +404,14 @@ public class DataStoreImplTest {
     }
     String longString = sb.toString();
 
-    expect(dynamicEntity.set("DS_id", 99)).andReturn(dynamicEntity).times(0, 1);
+    expect(dynamicEntity.set("DS_id", 99)).andReturn(dynamicEntity).once();
 
     transaction.begin();
     expect(transaction.isActive()).andReturn(true).anyTimes();
     transaction.rollback();
 
     // replay mocks
-    replay(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, dynamicEntity);
+    replay(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, dynamicEntity, jpaEntityManager, session, databaseLogin);
 
     DataStoreImpl dataStore = getDataStore(entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager);
 
@@ -332,7 +423,7 @@ public class DataStoreImplTest {
     }
 
     // verify mocks
-    verify(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, dynamicEntity);
+    verify(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, dynamicEntity, jpaEntityManager, session, databaseLogin);
   }
 
   @Test
@@ -340,6 +431,9 @@ public class DataStoreImplTest {
     DynamicClassLoader classLoader = new DynamicClassLoader(DataStoreImplTest.class.getClassLoader());
 
     // create mocks
+    JpaEntityManager jpaEntityManager = createMock(JpaEntityManager.class);
+    ServerSession session = createMock(ServerSession.class);
+    DatabaseLogin databaseLogin = createMock(DatabaseLogin.class);
     EntityManagerFactory entityManagerFactory = createMock(EntityManagerFactory.class);
     EntityManager entityManager = createMock(EntityManager.class);
     JPADynamicHelper jpaDynamicHelper = createNiceMock(JPADynamicHelper.class);
@@ -348,11 +442,20 @@ public class DataStoreImplTest {
     DynamicEntity dynamicEntity = createMock(DynamicEntity.class);
 
     // set expectations
+    PowerMock.mockStatic(JpaHelper.class);
+    expect(JpaHelper.getEntityManager(entityManager)).andReturn(jpaEntityManager).anyTimes();
+    PowerMock.replay(JpaHelper.class);
+    expect(jpaEntityManager.getServerSession()).andReturn(session).anyTimes();
+    expect(session.getLogin()).andReturn(databaseLogin).anyTimes();
+    Capture<Sequence> sequenceCapture = new Capture<Sequence>();
+    databaseLogin.addSequence(capture(sequenceCapture));
+    EasyMock.expectLastCall().anyTimes();
+
     Capture<DynamicType> typeCapture = new Capture<DynamicType>();
     Capture<DynamicType> typeCapture2 = new Capture<DynamicType>();
     jpaDynamicHelper.addTypes(eq(true), eq(true), capture(typeCapture), capture(typeCapture2));
 
-    expect(entityManagerFactory.createEntityManager()).andReturn(entityManager);
+    expect(entityManagerFactory.createEntityManager()).andReturn(entityManager).anyTimes();
     expect(entityManager.getTransaction()).andReturn(transaction).anyTimes();
     Capture<Class> entityClassCapture = new Capture<Class>();
     expect(entityManager.getReference(capture(entityClassCapture), eq(99))).andReturn(dynamicEntity);
@@ -363,16 +466,16 @@ public class DataStoreImplTest {
     transaction.commit();
 
     // replay mocks
-    replay(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, dynamicEntity);
+    replay(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, dynamicEntity, jpaEntityManager, session, databaseLogin);
 
     DataStoreImpl dataStore = getDataStore(entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager);
 
     dataStore.remove(new TestEntity(99, "foo", new TestSubEntity("bar")));
 
-    Assert.assertEquals(entityClassCapture.getValue(), typeCapture2.getValue().getJavaClass());
+    Assert.assertEquals(entityClassCapture.getValue(), typeCapture.getValue().getJavaClass());
 
     // verify mocks
-    verify(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, dynamicEntity);
+    verify(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, dynamicEntity, jpaEntityManager, session, databaseLogin);
   }
 
   @Test
@@ -380,6 +483,9 @@ public class DataStoreImplTest {
     DynamicClassLoader classLoader = new DynamicClassLoader(DataStoreImplTest.class.getClassLoader());
 
     // create mocks
+    JpaEntityManager jpaEntityManager = createMock(JpaEntityManager.class);
+    ServerSession session = createMock(ServerSession.class);
+    DatabaseLogin databaseLogin = createMock(DatabaseLogin.class);
     EntityManagerFactory entityManagerFactory = createMock(EntityManagerFactory.class);
     EntityManager entityManager = createMock(EntityManager.class);
     JPADynamicHelper jpaDynamicHelper = createNiceMock(JPADynamicHelper.class);
@@ -387,11 +493,20 @@ public class DataStoreImplTest {
     DynamicEntity dynamicEntity = createMock(DynamicEntity.class);
 
     // set expectations
+    PowerMock.mockStatic(JpaHelper.class);
+    expect(JpaHelper.getEntityManager(entityManager)).andReturn(jpaEntityManager).anyTimes();
+    PowerMock.replay(JpaHelper.class);
+    expect(jpaEntityManager.getServerSession()).andReturn(session).anyTimes();
+    expect(session.getLogin()).andReturn(databaseLogin).anyTimes();
+    Capture<Sequence> sequenceCapture = new Capture<Sequence>();
+    databaseLogin.addSequence(capture(sequenceCapture));
+    EasyMock.expectLastCall().anyTimes();
+
     Capture<DynamicType> typeCapture = new Capture<DynamicType>();
     Capture<DynamicType> typeCapture2 = new Capture<DynamicType>();
     jpaDynamicHelper.addTypes(eq(true), eq(true), capture(typeCapture), capture(typeCapture2));
 
-    expect(entityManagerFactory.createEntityManager()).andReturn(entityManager);
+    expect(entityManagerFactory.createEntityManager()).andReturn(entityManager).anyTimes();
     Capture<Class> entityClassCapture = new Capture<Class>();
     expect(entityManager.find(capture(entityClassCapture), eq(99))).andReturn(dynamicEntity);
     entityManager.close();
@@ -402,18 +517,18 @@ public class DataStoreImplTest {
     expect(dynamicEntity.get("DS_subEntity")).andReturn(subEntity);
 
     // replay mocks
-    replay(entityManagerFactory, entityManager, jpaDynamicHelper, dynamicEntity, schemaManager);
+    replay(entityManagerFactory, entityManager, jpaDynamicHelper, dynamicEntity, schemaManager, jpaEntityManager, session, databaseLogin);
 
     DataStoreImpl dataStore = getDataStore(entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager);
 
     TestEntity entity = dataStore.find(TestEntity.class, 99);
 
-    Assert.assertEquals(entityClassCapture.getValue(), typeCapture2.getValue().getJavaClass());
-    Assert.assertEquals(99, entity.getId());
+    Assert.assertEquals(entityClassCapture.getValue(), typeCapture.getValue().getJavaClass());
+    Assert.assertEquals(99, (int) entity.getId());
     Assert.assertEquals("foo", entity.getName());
 
     // verify mocks
-    verify(entityManagerFactory, entityManager, jpaDynamicHelper, dynamicEntity, schemaManager);
+    verify(entityManagerFactory, entityManager, jpaDynamicHelper, dynamicEntity, schemaManager, jpaEntityManager, session, databaseLogin);
   }
 
   @Test
@@ -421,6 +536,9 @@ public class DataStoreImplTest {
     DynamicClassLoader classLoader = new DynamicClassLoader(DataStoreImplTest.class.getClassLoader());
 
     // create mocks
+    JpaEntityManager jpaEntityManager = createMock(JpaEntityManager.class);
+    ServerSession session = createMock(ServerSession.class);
+    DatabaseLogin databaseLogin = createMock(DatabaseLogin.class);
     EntityManagerFactory entityManagerFactory = createMock(EntityManagerFactory.class);
     EntityManager entityManager = createMock(EntityManager.class);
     JPADynamicHelper jpaDynamicHelper = createNiceMock(JPADynamicHelper.class);
@@ -429,13 +547,21 @@ public class DataStoreImplTest {
     Query query = createMock(Query.class);
 
     // set expectations
+    PowerMock.mockStatic(JpaHelper.class);
+    expect(JpaHelper.getEntityManager(entityManager)).andReturn(jpaEntityManager).anyTimes();
+    PowerMock.replay(JpaHelper.class);
+    expect(jpaEntityManager.getServerSession()).andReturn(session).anyTimes();
+    expect(session.getLogin()).andReturn(databaseLogin).anyTimes();
+    Capture<Sequence> sequenceCapture = new Capture<Sequence>();
+    databaseLogin.addSequence(capture(sequenceCapture));
+    EasyMock.expectLastCall().anyTimes();
     Capture<DynamicType> typeCapture = new Capture<DynamicType>();
     Capture<DynamicType> typeCapture2 = new Capture<DynamicType>();
     jpaDynamicHelper.addTypes(eq(true), eq(true), capture(typeCapture), capture(typeCapture2));
 
-    expect(entityManagerFactory.createEntityManager()).andReturn(entityManager);
+    expect(entityManagerFactory.createEntityManager()).andReturn(entityManager).anyTimes();
     expect(entityManager.createQuery(
-        "SELECT e FROM DS_DataStoreImplTest$TestEntity_1 e WHERE e.DS_id=99")).andReturn(query);
+      "SELECT e FROM DS_DataStoreImplTest$TestEntity_1 e WHERE e.DS_id=99")).andReturn(query);
     entityManager.close();
 
     expect(query.getResultList()).andReturn(Collections.singletonList(dynamicEntity));
@@ -446,7 +572,7 @@ public class DataStoreImplTest {
     expect(dynamicEntity.get("DS_subEntity")).andReturn(subEntity);
 
     // replay mocks
-    replay(entityManagerFactory, entityManager, jpaDynamicHelper, dynamicEntity, query, schemaManager);
+    replay(entityManagerFactory, entityManager, jpaDynamicHelper, dynamicEntity, query, schemaManager, jpaEntityManager, session, databaseLogin);
 
     DataStoreImpl dataStore = getDataStore(entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager);
 
@@ -456,11 +582,11 @@ public class DataStoreImplTest {
 
     TestEntity entity = entities.iterator().next();
 
-    Assert.assertEquals(99, entity.getId());
+    Assert.assertEquals(99, (int) entity.getId());
     Assert.assertEquals("foo", entity.getName());
 
     // verify mocks
-    verify(entityManagerFactory, entityManager, jpaDynamicHelper, dynamicEntity, query, schemaManager);
+    verify(entityManagerFactory, entityManager, jpaDynamicHelper, dynamicEntity, query, schemaManager, jpaEntityManager, session, databaseLogin);
   }
 
   @Test
@@ -468,6 +594,9 @@ public class DataStoreImplTest {
     DynamicClassLoader classLoader = new DynamicClassLoader(DataStoreImplTest.class.getClassLoader());
 
     // create mocks
+    JpaEntityManager jpaEntityManager = createMock(JpaEntityManager.class);
+    ServerSession session = createMock(ServerSession.class);
+    DatabaseLogin databaseLogin = createMock(DatabaseLogin.class);
     EntityManagerFactory entityManagerFactory = createMock(EntityManagerFactory.class);
     EntityManager entityManager = createMock(EntityManager.class);
     JPADynamicHelper jpaDynamicHelper = createNiceMock(JPADynamicHelper.class);
@@ -478,13 +607,21 @@ public class DataStoreImplTest {
     Query query = createMock(Query.class);
 
     // set expectations
+    PowerMock.mockStatic(JpaHelper.class);
+    expect(JpaHelper.getEntityManager(entityManager)).andReturn(jpaEntityManager).anyTimes();
+    PowerMock.replay(JpaHelper.class);
+    expect(jpaEntityManager.getServerSession()).andReturn(session).anyTimes();
+    expect(session.getLogin()).andReturn(databaseLogin).anyTimes();
+    Capture<Sequence> sequenceCapture = new Capture<Sequence>();
+    databaseLogin.addSequence(capture(sequenceCapture));
+    EasyMock.expectLastCall().anyTimes();
     Capture<DynamicType> typeCapture = new Capture<DynamicType>();
     Capture<DynamicType> typeCapture2 = new Capture<DynamicType>();
     jpaDynamicHelper.addTypes(eq(true), eq(true), capture(typeCapture), capture(typeCapture2));
 
-    expect(entityManagerFactory.createEntityManager()).andReturn(entityManager);
+    expect(entityManagerFactory.createEntityManager()).andReturn(entityManager).anyTimes();
     expect(entityManager.createQuery(
-        "SELECT e FROM DS_DataStoreImplTest$TestEntity_1 e WHERE e.DS_name='foo'")).andReturn(query);
+      "SELECT e FROM DS_DataStoreImplTest$TestEntity_1 e WHERE e.DS_name='foo'")).andReturn(query);
     entityManager.close();
 
     List<DynamicEntity> entityList = new LinkedList<DynamicEntity>();
@@ -511,7 +648,7 @@ public class DataStoreImplTest {
 
     // replay mocks
     replay(entityManagerFactory, entityManager, jpaDynamicHelper,
-        dynamicEntity1, dynamicEntity2, dynamicEntity3, query, schemaManager);
+      dynamicEntity1, dynamicEntity2, dynamicEntity3, query, schemaManager, jpaEntityManager, session, databaseLogin);
 
     DataStoreImpl dataStore = getDataStore(entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager);
 
@@ -525,14 +662,14 @@ public class DataStoreImplTest {
 
     // verify mocks
     verify(entityManagerFactory, entityManager, jpaDynamicHelper,
-        dynamicEntity1, dynamicEntity2, dynamicEntity3, query, schemaManager);
+      dynamicEntity1, dynamicEntity2, dynamicEntity3, query, schemaManager, jpaEntityManager, session, databaseLogin);
   }
 
   private DataStoreImpl getDataStore(EntityManagerFactory entityManagerFactory,
                                      JPADynamicHelper jpaDynamicHelper,
                                      DynamicClassLoader classLoader,
                                      SchemaManager schemaManager)
-      throws Exception {
+    throws Exception {
     ViewConfig viewConfig = ViewConfigTest.getConfig(xml);
     ViewEntity viewDefinition = ViewEntityTest.getViewEntity(viewConfig);
 
@@ -542,7 +679,7 @@ public class DataStoreImplTest {
     setPersistenceEntities(viewInstanceEntity);
 
     Injector injector = Guice.createInjector(
-        new TestModule(viewInstanceEntity, entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager));
+      new TestModule(viewInstanceEntity, entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager));
     return injector.getInstance(DataStoreImpl.class);
   }
 
@@ -580,15 +717,20 @@ public class DataStoreImplTest {
       this.subEntity = subEntity;
     }
 
-    int id;
+    public TestEntity(String name, TestSubEntity subEntity) {
+      this.name = name;
+      this.subEntity = subEntity;
+    }
+
+    Integer id = null;
     String name;
     TestSubEntity subEntity;
 
-    public int getId() {
+    public Integer getId() {
       return id;
     }
 
-    public void setId(int id) {
+    public void setId(Integer id) {
       this.id = id;
     }
 
@@ -611,6 +753,8 @@ public class DataStoreImplTest {
 
   public static class TestSubEntity {
 
+    private Integer id = null;
+
     public TestSubEntity() {
     }
 
@@ -618,8 +762,21 @@ public class DataStoreImplTest {
       this.name = name;
     }
 
+    public TestSubEntity(Integer id, String name) {
+      this.id = id;
+      this.name = name;
+    }
+
     String name;
 
+    public Integer getId() {
+      return id;
+    }
+
+    public void setId(Integer id) {
+      this.id = id;
+    }
+
     public String getName() {
       return name;
     }
@@ -638,7 +795,7 @@ public class DataStoreImplTest {
       this.id = id;
     }
 
-    int id;
+    Integer id = null;
     String f1;
     String f2;
     String f3;
@@ -661,11 +818,11 @@ public class DataStoreImplTest {
     String f20;
     String f21;
 
-    public int getId() {
+    public Integer getId() {
       return id;
     }
 
-    public void setId(int id) {
+    public void setId(Integer id) {
       this.id = id;
     }
   }