Java Code Examples for org.hibernate.persister.entity.EntityPersister#getIdentifierGenerator()
The following examples show how to use
org.hibernate.persister.entity.EntityPersister#getIdentifierGenerator() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PooledSequenceTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
public void testNormalBoundary() { EntityPersister persister = sfi().getEntityPersister( Entity.class.getName() ); assertClassAssignability( SequenceStyleGenerator.class, persister.getIdentifierGenerator().getClass() ); SequenceStyleGenerator generator = ( SequenceStyleGenerator ) persister.getIdentifierGenerator(); assertClassAssignability( OptimizerFactory.PooledOptimizer.class, generator.getOptimizer().getClass() ); OptimizerFactory.PooledOptimizer optimizer = ( OptimizerFactory.PooledOptimizer ) generator.getOptimizer(); int increment = optimizer.getIncrementSize(); Entity[] entities = new Entity[ increment + 1 ]; Session s = openSession(); s.beginTransaction(); for ( int i = 0; i < increment; i++ ) { entities[i] = new Entity( "" + ( i + 1 ) ); s.save( entities[i] ); assertEquals( 2, generator.getDatabaseStructure().getTimesAccessed() ); // initialization calls seq twice assertEquals( increment + 1, optimizer.getLastSourceValue() ); // initialization calls seq twice assertEquals( i + 1, optimizer.getLastValue() ); assertEquals( increment + 1, optimizer.getLastSourceValue() ); } // now force a "clock over" entities[ increment ] = new Entity( "" + increment ); s.save( entities[ increment ] ); assertEquals( 3, generator.getDatabaseStructure().getTimesAccessed() ); // initialization (2) + clock over assertEquals( ( increment * 2 ) + 1, optimizer.getLastSourceValue() ); // initialization (2) + clock over assertEquals( increment + 1, optimizer.getLastValue() ); s.getTransaction().commit(); s.beginTransaction(); for ( int i = 0; i < entities.length; i++ ) { assertEquals( i + 1, entities[i].getId().intValue() ); s.delete( entities[i] ); } s.getTransaction().commit(); s.close(); }
Example 2
Source File: BasicSequenceTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
public void testNormalBoundary() { EntityPersister persister = sfi().getEntityPersister( Entity.class.getName() ); assertClassAssignability( SequenceStyleGenerator.class, persister.getIdentifierGenerator().getClass() ); SequenceStyleGenerator generator = ( SequenceStyleGenerator ) persister.getIdentifierGenerator(); int count = 5; Entity[] entities = new Entity[count]; Session s = openSession(); s.beginTransaction(); for ( int i = 0; i < count; i++ ) { entities[i] = new Entity( "" + ( i + 1 ) ); s.save( entities[i] ); long expectedId = i + 1; assertEquals( expectedId, entities[i].getId().longValue() ); assertEquals( expectedId, generator.getDatabaseStructure().getTimesAccessed() ); assertEquals( expectedId, generator.getOptimizer().getLastSourceValue() ); } s.getTransaction().commit(); s.beginTransaction(); for ( int i = 0; i < count; i++ ) { assertEquals( i + 1, entities[i].getId().intValue() ); s.delete( entities[i] ); } s.getTransaction().commit(); s.close(); }
Example 3
Source File: HiLoSequenceTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
public void testNormalBoundary() { EntityPersister persister = sfi().getEntityPersister( Entity.class.getName() ); assertClassAssignability( SequenceStyleGenerator.class, persister.getIdentifierGenerator().getClass() ); SequenceStyleGenerator generator = ( SequenceStyleGenerator ) persister.getIdentifierGenerator(); assertClassAssignability( OptimizerFactory.HiLoOptimizer.class, generator.getOptimizer().getClass() ); OptimizerFactory.HiLoOptimizer optimizer = ( OptimizerFactory.HiLoOptimizer ) generator.getOptimizer(); int increment = optimizer.getIncrementSize(); Entity[] entities = new Entity[ increment + 1 ]; Session s = openSession(); s.beginTransaction(); for ( int i = 0; i < increment; i++ ) { entities[i] = new Entity( "" + ( i + 1 ) ); s.save( entities[i] ); assertEquals( 1, generator.getDatabaseStructure().getTimesAccessed() ); // initialization assertEquals( 1, optimizer.getLastSourceValue() ); // initialization assertEquals( i + 1, optimizer.getLastValue() ); assertEquals( increment + 1, optimizer.getHiValue() ); } // now force a "clock over" entities[ increment ] = new Entity( "" + increment ); s.save( entities[ increment ] ); assertEquals( 2, generator.getDatabaseStructure().getTimesAccessed() ); // initialization assertEquals( 2, optimizer.getLastSourceValue() ); // initialization assertEquals( increment + 1, optimizer.getLastValue() ); assertEquals( ( increment * 2 ) + 1, optimizer.getHiValue() ); s.getTransaction().commit(); s.beginTransaction(); for ( int i = 0; i < entities.length; i++ ) { assertEquals( i + 1, entities[i].getId().intValue() ); s.delete( entities[i] ); } s.getTransaction().commit(); s.close(); }
Example 4
Source File: BasicForcedTableSequenceTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
public void testNormalBoundary() { EntityPersister persister = sfi().getEntityPersister( Entity.class.getName() ); assertClassAssignability( SequenceStyleGenerator.class, persister.getIdentifierGenerator().getClass() ); SequenceStyleGenerator generator = ( SequenceStyleGenerator ) persister.getIdentifierGenerator(); int count = 5; Entity[] entities = new Entity[count]; Session s = openSession(); s.beginTransaction(); for ( int i = 0; i < count; i++ ) { entities[i] = new Entity( "" + ( i + 1 ) ); s.save( entities[i] ); long expectedId = i + 1; assertEquals( expectedId, entities[i].getId().longValue() ); assertEquals( expectedId, generator.getDatabaseStructure().getTimesAccessed() ); assertEquals( expectedId, generator.getOptimizer().getLastSourceValue() ); } s.getTransaction().commit(); s.beginTransaction(); for ( int i = 0; i < count; i++ ) { assertEquals( i + 1, entities[i].getId().intValue() ); s.delete( entities[i] ); } s.getTransaction().commit(); s.close(); }
Example 5
Source File: BasicTableTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
public void testNormalBoundary() { EntityPersister persister = sfi().getEntityPersister( Entity.class.getName() ); assertClassAssignability( TableGenerator.class, persister.getIdentifierGenerator().getClass() ); TableGenerator generator = ( TableGenerator ) persister.getIdentifierGenerator(); int count = 5; Entity[] entities = new Entity[count]; Session s = openSession(); s.beginTransaction(); for ( int i = 0; i < count; i++ ) { entities[i] = new Entity( "" + ( i + 1 ) ); s.save( entities[i] ); long expectedId = i + 1; assertEquals( expectedId, entities[i].getId().longValue() ); assertEquals( expectedId, generator.getTableAccessCount() ); assertEquals( expectedId, generator.getOptimizer().getLastSourceValue() ); } s.getTransaction().commit(); s.beginTransaction(); for ( int i = 0; i < count; i++ ) { assertEquals( i + 1, entities[i].getId().intValue() ); s.delete( entities[i] ); } s.getTransaction().commit(); s.close(); }
Example 6
Source File: PooledTableTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
public void testNormalBoundary() { EntityPersister persister = sfi().getEntityPersister( Entity.class.getName() ); assertClassAssignability( TableGenerator.class, persister.getIdentifierGenerator().getClass() ); TableGenerator generator = ( TableGenerator ) persister.getIdentifierGenerator(); assertClassAssignability( OptimizerFactory.PooledOptimizer.class, generator.getOptimizer().getClass() ); OptimizerFactory.PooledOptimizer optimizer = ( OptimizerFactory.PooledOptimizer ) generator.getOptimizer(); int increment = optimizer.getIncrementSize(); Entity[] entities = new Entity[ increment + 1 ]; Session s = openSession(); s.beginTransaction(); for ( int i = 0; i < increment; i++ ) { entities[i] = new Entity( "" + ( i + 1 ) ); s.save( entities[i] ); assertEquals( 2, generator.getTableAccessCount() ); // initialization calls seq twice assertEquals( increment + 1, optimizer.getLastSourceValue() ); // initialization calls seq twice assertEquals( i + 1, optimizer.getLastValue() ); assertEquals( increment + 1, optimizer.getLastSourceValue() ); } // now force a "clock over" entities[ increment ] = new Entity( "" + increment ); s.save( entities[ increment ] ); assertEquals( 3, generator.getTableAccessCount() ); // initialization (2) + clock over assertEquals( ( increment * 2 ) + 1, optimizer.getLastSourceValue() ); // initialization (2) + clock over assertEquals( increment + 1, optimizer.getLastValue() ); s.getTransaction().commit(); s.beginTransaction(); for ( int i = 0; i < entities.length; i++ ) { assertEquals( i + 1, entities[i].getId().intValue() ); s.delete( entities[i] ); } s.getTransaction().commit(); s.close(); }
Example 7
Source File: HiLoTableTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
public void testNormalBoundary() { EntityPersister persister = sfi().getEntityPersister( Entity.class.getName() ); assertClassAssignability( TableGenerator.class, persister.getIdentifierGenerator().getClass() ); TableGenerator generator = ( TableGenerator ) persister.getIdentifierGenerator(); assertClassAssignability( OptimizerFactory.HiLoOptimizer.class, generator.getOptimizer().getClass() ); OptimizerFactory.HiLoOptimizer optimizer = ( OptimizerFactory.HiLoOptimizer ) generator.getOptimizer(); int increment = optimizer.getIncrementSize(); Entity[] entities = new Entity[ increment + 1 ]; Session s = openSession(); s.beginTransaction(); for ( int i = 0; i < increment; i++ ) { entities[i] = new Entity( "" + ( i + 1 ) ); s.save( entities[i] ); assertEquals( 1, generator.getTableAccessCount() ); // initialization assertEquals( 1, optimizer.getLastSourceValue() ); // initialization assertEquals( i + 1, optimizer.getLastValue() ); assertEquals( increment + 1, optimizer.getHiValue() ); } // now force a "clock over" entities[ increment ] = new Entity( "" + increment ); s.save( entities[ increment ] ); assertEquals( 2, generator.getTableAccessCount() ); // initialization assertEquals( 2, optimizer.getLastSourceValue() ); // initialization assertEquals( increment + 1, optimizer.getLastValue() ); assertEquals( ( increment * 2 ) + 1, optimizer.getHiValue() ); s.getTransaction().commit(); s.beginTransaction(); for ( int i = 0; i < entities.length; i++ ) { assertEquals( i + 1, entities[i].getId().intValue() ); s.delete( entities[i] ); } s.getTransaction().commit(); s.close(); }
Example 8
Source File: DefaultReactivePersistEventListener.java From hibernate-reactive with GNU Lesser General Public License v2.1 | 4 votes |
/** * Handle the given create event. * * @param event The create event to be handled. */ public CompletionStage<Void> reactiveOnPersist(PersistEvent event, IdentitySet createCache) throws HibernateException { final SessionImplementor source = event.getSession(); final Object object = event.getObject(); final Object entity; if ( object instanceof HibernateProxy ) { LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer(); if ( li.isUninitialized() ) { if ( li.getSession() == source ) { return CompletionStages.nullFuture(); //NOTE EARLY EXIT! } else { return CompletionStages.failedFuture( new PersistentObjectException( "uninitialized proxy passed to persist()" ) ); } } entity = li.getImplementation(); } else { entity = object; } final String entityName; if ( event.getEntityName() != null ) { entityName = event.getEntityName(); } else { entityName = source.bestGuessEntityName( entity ); event.setEntityName( entityName ); } final EntityEntry entityEntry = source.getPersistenceContextInternal().getEntry( entity ); EntityState entityState = EntityState.getEntityState( entity, entityName, entityEntry, source, true ); if ( entityState == EntityState.DETACHED ) { // JPA 2, in its version of a "foreign generated", allows the id attribute value // to be manually set by the user, even though this manual value is irrelevant. // The issue is that this causes problems with the Hibernate unsaved-value strategy // which comes into play here in determining detached/transient state. // // Detect if we have this situation and if so null out the id value and calculate the // entity state again. // NOTE: entityEntry must be null to get here, so we cannot use any of its values EntityPersister persister = source.getFactory().getMetamodel().entityPersister( entityName ); if (persister.getIdentifierGenerator() instanceof ForeignGenerator) { if ( LOG.isDebugEnabled() && persister.getIdentifier( entity, source ) != null ) { LOG.debug( "Resetting entity id attribute to null for foreign generator" ); } persister.setIdentifier( entity, null, source ); entityState = EntityState.getEntityState( entity, entityName, entityEntry, source, true ); } } switch ( entityState ) { case DETACHED: { return CompletionStages.failedFuture( new PersistentObjectException( "detached entity passed to persist: " + EventUtil.getLoggableName( event.getEntityName(), entity ) ) ); } case PERSISTENT: { return entityIsPersistent( event, createCache ); } case TRANSIENT: { return entityIsTransient( event, createCache ); } case DELETED: { entityEntry.setStatus( Status.MANAGED ); entityEntry.setDeletedState( null ); event.getSession().getActionQueue().unScheduleDeletion( entityEntry, event.getObject() ); return entityIsDeleted( event, createCache ); } default: { return CompletionStages.failedFuture( new ObjectDeletedException( "deleted entity passed to persist", null, EventUtil.getLoggableName( event.getEntityName(), entity ) ) ); } } }
Example 9
Source File: AbstractReactiveSaveEventListener.java From hibernate-reactive with GNU Lesser General Public License v2.1 | 4 votes |
private static CompletionStage<?> generateId(Object entity, EventSource source, EntityPersister persister) { IdentifierGenerator generator = persister.getIdentifierGenerator(); return generator instanceof ReactiveIdentifierGenerator ? ( (ReactiveIdentifierGenerator<?>) generator ).generate( (ReactiveSession) source, entity ) : CompletionStages.completedFuture( generator.generate( source.getSession(), entity ) ); }
Example 10
Source File: BulkManipulationTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
public void testInsertWithGeneratedId() { // Make sure the env supports bulk inserts with generated ids... EntityPersister persister = sfi().getEntityPersister( PettingZoo.class.getName() ); IdentifierGenerator generator = persister.getIdentifierGenerator(); if ( !HqlSqlWalker.supportsIdGenWithBulkInsertion( generator ) ) { return; } // create a Zoo Zoo zoo = new Zoo(); zoo.setName( "zoo" ); Session s = openSession(); Transaction t = s.beginTransaction(); s.save( zoo ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); int count = s.createQuery( "insert into PettingZoo (name) select name from Zoo" ).executeUpdate(); t.commit(); s.close(); assertEquals( "unexpected insertion count", 1, count ); s = openSession(); t = s.beginTransaction(); PettingZoo pz = ( PettingZoo ) s.createQuery( "from PettingZoo" ).uniqueResult(); t.commit(); s.close(); assertEquals( zoo.getName(), pz.getName() ); assertTrue( zoo.getId() != pz.getId() ); s = openSession(); t = s.beginTransaction(); s.createQuery( "delete Zoo" ).executeUpdate(); t.commit(); s.close(); }
Example 11
Source File: BulkManipulationTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
public void testInsertWithGeneratedVersionAndId() { // Make sure the env supports bulk inserts with generated ids... EntityPersister persister = sfi().getEntityPersister( IntegerVersioned.class.getName() ); IdentifierGenerator generator = persister.getIdentifierGenerator(); if ( !HqlSqlWalker.supportsIdGenWithBulkInsertion( generator ) ) { return; } Session s = openSession(); Transaction t = s.beginTransaction(); IntegerVersioned entity = new IntegerVersioned( "int-vers" ); s.save( entity ); s.createQuery( "select id, name, version from IntegerVersioned" ).list(); t.commit(); s.close(); Long initialId = entity.getId(); int initialVersion = entity.getVersion(); s = openSession(); t = s.beginTransaction(); int count = s.createQuery( "insert into IntegerVersioned ( name ) select name from IntegerVersioned" ).executeUpdate(); t.commit(); s.close(); assertEquals( "unexpected insertion count", 1, count ); s = openSession(); t = s.beginTransaction(); IntegerVersioned created = ( IntegerVersioned ) s.createQuery( "from IntegerVersioned where id <> :initialId" ) .setLong( "initialId", initialId.longValue() ) .uniqueResult(); t.commit(); s.close(); assertEquals( "version was not seeded", initialVersion, created.getVersion() ); s = openSession(); t = s.beginTransaction(); s.createQuery( "delete IntegerVersioned" ).executeUpdate(); t.commit(); s.close(); }
Example 12
Source File: BulkManipulationTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
public void testInsertWithGeneratedTimestampVersion() { // Make sure the env supports bulk inserts with generated ids... EntityPersister persister = sfi().getEntityPersister( TimestampVersioned.class.getName() ); IdentifierGenerator generator = persister.getIdentifierGenerator(); if ( !HqlSqlWalker.supportsIdGenWithBulkInsertion( generator ) ) { return; } Session s = openSession(); Transaction t = s.beginTransaction(); TimestampVersioned entity = new TimestampVersioned( "int-vers" ); s.save( entity ); s.createQuery( "select id, name, version from TimestampVersioned" ).list(); t.commit(); s.close(); Long initialId = entity.getId(); //Date initialVersion = entity.getVersion(); s = openSession(); t = s.beginTransaction(); int count = s.createQuery( "insert into TimestampVersioned ( name ) select name from TimestampVersioned" ).executeUpdate(); t.commit(); s.close(); assertEquals( "unexpected insertion count", 1, count ); s = openSession(); t = s.beginTransaction(); TimestampVersioned created = ( TimestampVersioned ) s.createQuery( "from TimestampVersioned where id <> :initialId" ) .setLong( "initialId", initialId.longValue() ) .uniqueResult(); t.commit(); s.close(); assertNotNull( created.getVersion() ); //assertEquals( "version was not seeded", initialVersion, created.getVersion() ); s = openSession(); t = s.beginTransaction(); s.createQuery( "delete TimestampVersioned" ).executeUpdate(); t.commit(); s.close(); }