当前位置: 首页>>代码示例>>Java>>正文


Java NonUniqueObjectException类代码示例

本文整理汇总了Java中org.hibernate.NonUniqueObjectException的典型用法代码示例。如果您正苦于以下问题:Java NonUniqueObjectException类的具体用法?Java NonUniqueObjectException怎么用?Java NonUniqueObjectException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


NonUniqueObjectException类属于org.hibernate包,在下文中一共展示了NonUniqueObjectException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: save

import org.hibernate.NonUniqueObjectException; //导入依赖的package包/类
public static int save(List<TimedValue> timedValues){
	return HibernateUtil.withSession((session) -> {
		int saved = 0;
		session.beginTransaction();
		for (TimedValue timedValue : timedValues){
			try{
				session.saveOrUpdate(timedValue);
				saved++;
			}catch(NonUniqueObjectException e){
				// This is happening because the TFL stations contain a duplicate ID
				log.warn("Could not save timed value for subject {}, attribute {}, time {}: {}",
						timedValue.getId().getSubject().getLabel(),
						timedValue.getId().getAttribute().getDescription(),
						timedValue.getId().getTimestamp().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME),
						e.getMessage());
			}
			if ( saved % 20 == 0 ) { //20, same as the JDBC batch size
				//flush a batch of inserts and release memory:
				session.flush();
				session.clear();
			}
		}
		session.getTransaction().commit();
		return saved;
	});
}
 
开发者ID:FutureCitiesCatapult,项目名称:TomboloDigitalConnector,代码行数:27,代码来源:TimedValueUtils.java

示例2: save

import org.hibernate.NonUniqueObjectException; //导入依赖的package包/类
public static int save(List<FixedValue> fixedValues){
    return HibernateUtil.withSession((session) -> {
        int saved = 0;
        session.beginTransaction();
        for (FixedValue fixedValue : fixedValues){
            try{
                session.saveOrUpdate(fixedValue);
                saved++;
            }catch(NonUniqueObjectException e){
                // This is happening because the TFL stations contain a duplicate ID
                log.warn("Could not save fixed value for subject {}, attribute {}: {}",
                        fixedValue.getId().getSubject().getLabel(),
                        fixedValue.getId().getAttribute().getLabel(),
                        e.getMessage());
            }
            if ( saved % 20 == 0 ) { //20, same as the JDBC batch size
                //flush a batch of inserts and release memory:
                session.flush();
                session.clear();
            }
        }
        session.getTransaction().commit();
        return saved;
    });
}
 
开发者ID:FutureCitiesCatapult,项目名称:TomboloDigitalConnector,代码行数:26,代码来源:FixedValueUtils.java

示例3: testPersistThenMergeInSameTxnWithVersion

import org.hibernate.NonUniqueObjectException; //导入依赖的package包/类
public void testPersistThenMergeInSameTxnWithVersion() {
	Session s = openSession();
	Transaction tx = s.beginTransaction();
	VersionedEntity entity = new VersionedEntity( "test", "test" );
	s.persist( entity );
	s.merge( new VersionedEntity( "test", "test-2" ) );

	try {
		// control operation...
		s.saveOrUpdate( new VersionedEntity( "test", "test-3" ) );
		fail( "saveOrUpdate() should fail here" );
	}
	catch( NonUniqueObjectException expected ) {
		// expected behavior
	}

	tx.commit();
	s.close();

	cleanup();
}
 
开发者ID:cacheonix,项目名称:cacheonix-core,代码行数:22,代码来源:MergeTest.java

示例4: testPersistThenMergeInSameTxnWithTimestamp

import org.hibernate.NonUniqueObjectException; //导入依赖的package包/类
public void testPersistThenMergeInSameTxnWithTimestamp() {
	Session s = openSession();
	Transaction tx = s.beginTransaction();
	TimestampedEntity entity = new TimestampedEntity( "test", "test" );
	s.persist( entity );
	s.merge( new TimestampedEntity( "test", "test-2" ) );

	try {
		// control operation...
		s.saveOrUpdate( new TimestampedEntity( "test", "test-3" ) );
		fail( "saveOrUpdate() should fail here" );
	}
	catch( NonUniqueObjectException expected ) {
		// expected behavior
	}

	tx.commit();
	s.close();

	cleanup();
}
 
开发者ID:cacheonix,项目名称:cacheonix-core,代码行数:22,代码来源:MergeTest.java

示例5: lookup

import org.hibernate.NonUniqueObjectException; //导入依赖的package包/类
@SuppressWarnings("unchecked") 
// ^^^ unavoidable, due to lack of genericity in Hibernate lib
public <T> T lookup( Class<T> c, int object_id ) 
{
    if ( log.isDebugEnabled() )
        log.debug( "attempting to lookup object of " 
                 + c.getName() 
                 + " with id=" 
                 + object_id );
    
    try
    {
        Session session = getHibernateSession();
        T entity = (T) session.get( c, object_id );
        return entity;
    }
    catch ( NonUniqueObjectException e )
    {
        __log_exception( e, c );
        throw e;
    }
}
 
开发者ID:glycoinfo,项目名称:eurocarbdb,代码行数:23,代码来源:HibernateEntityManager.java

示例6: runCommand

import org.hibernate.NonUniqueObjectException; //导入依赖的package包/类
@Override
public Command runCommand() throws Exception {
	isUpdate = (object.getPid() != null);
	T aux = completeObject(object);
	
	try{
		object = (aux != null)?aux:object;
		dao.persist(object);
	}catch(NonUniqueObjectException nuoe){
		if(autoMerge){
			dao.merge(object);
		}else
			throw nuoe;
	}
	
	return this;
}
 
开发者ID:malaguna,项目名称:cmdit,代码行数:18,代码来源:SaveAbstractObjCmd.java

示例7: updateChangingAnInstanceCreatedByHandUsingARealIdShouldFail

import org.hibernate.NonUniqueObjectException; //导入依赖的package包/类
@Test(expected=NonUniqueObjectException.class)
public void updateChangingAnInstanceCreatedByHandUsingARealIdShouldFail() {
	CategoryRepository dao = this.newDao();
	Category c = this.newCategory("c1", false);
	this.insert(c, dao);
	this.commit();
	
	int id = c.getId();
	
	Category c2 = new Category();
	c2.setId(id);
	c2.setName("c2");
	c2.setModerated(true);
	c2.setDisplayOrder(2);

	this.update(c2, dao);

	Category loaded = dao.get(id);
	Assert.assertEquals("c2", loaded.getName());
	Assert.assertEquals(true, loaded.isModerated());
	Assert.assertEquals(2, loaded.getDisplayOrder());
}
 
开发者ID:eclipse123,项目名称:JForum,代码行数:23,代码来源:CategoryDAOTestCase.java

示例8: lockInHibernate

import org.hibernate.NonUniqueObjectException; //导入依赖的package包/类
/**
 * Locks an entity (LockMode.NONE) in current hibernate session.
 *
 * @param entity
 *     the entity to lock.
 * @param hibernateSession
 *     the hibernate session.
 */
private void lockInHibernate(IEntity entity, Session hibernateSession) {
  if (!hibernateSession.contains(entity)) {
    // Do not use get before trying to lock.
    // Get performs a DB query.
    try {
      hibernateSession.buildLockRequest(LockOptions.NONE).lock(entity);
    } catch (NonUniqueObjectException ex) {
      if (hibernateSession == noTxSession) {
        hibernateSession.clear();
        hibernateSession.buildLockRequest(LockOptions.NONE).lock(entity);
      } else {
        throw ex;
      }
    }
  }
}
 
开发者ID:jspresso,项目名称:jspresso-ce,代码行数:25,代码来源:HibernateBackendController.java

示例9: checkUniqueness

import org.hibernate.NonUniqueObjectException; //导入依赖的package包/类
@Override
public void checkUniqueness(EntityKey key, Object object) throws HibernateException {
	final Object entity = getEntity( key );
	if ( entity == object ) {
		throw new AssertionFailure( "object already associated, but no entry was found" );
	}
	if ( entity != null ) {
		throw new NonUniqueObjectException( key.getIdentifier(), key.getEntityName() );
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:11,代码来源:StatefulPersistenceContext.java

示例10: load

import org.hibernate.NonUniqueObjectException; //导入依赖的package包/类
/**
 * Performs the load of an entity.
 *
 * @param event The initiating load request event
 * @param persister The persister corresponding to the entity to be loaded
 * @param keyToLoad The key of the entity to be loaded
 * @param options The defined load options
 *
 * @return The loaded entity.
 *
 * @throws HibernateException
 */
protected Object load(
		final LoadEvent event,
		final EntityPersister persister,
		final EntityKey keyToLoad,
		final LoadEventListener.LoadType options) {

	if ( event.getInstanceToLoad() != null ) {
		if ( event.getSession().getPersistenceContext().getEntry( event.getInstanceToLoad() ) != null ) {
			throw new PersistentObjectException(
					"attempted to load into an instance that was already associated with the session: " +
							MessageHelper.infoString(
									persister,
									event.getEntityId(),
									event.getSession().getFactory()
							)
			);
		}
		persister.setIdentifier( event.getInstanceToLoad(), event.getEntityId(), event.getSession() );
	}

	Object entity = doLoad( event, persister, keyToLoad, options );

	boolean isOptionalInstance = event.getInstanceToLoad() != null;

	if ( !options.isAllowNulls() || isOptionalInstance ) {
		if ( entity == null ) {
			event.getSession()
					.getFactory()
					.getEntityNotFoundDelegate()
					.handleEntityNotFound( event.getEntityClassName(), event.getEntityId() );
		}
	}

	if ( isOptionalInstance && entity != event.getInstanceToLoad() ) {
		throw new NonUniqueObjectException( event.getEntityId(), event.getEntityClassName() );
	}

	return entity;
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:52,代码来源:DefaultLoadEventListener.java

示例11: checkUniqueness

import org.hibernate.NonUniqueObjectException; //导入依赖的package包/类
/**
 * Attempts to check whether the given key represents an entity already loaded within the
 * current session.
 * @param object The entity reference against which to perform the uniqueness check.
 * @throws HibernateException
 */
public void checkUniqueness(EntityKey key, Object object) throws HibernateException {
	Object entity = getEntity(key);
	if ( entity == object ) {
		throw new AssertionFailure( "object already associated, but no entry was found" );
	}
	if ( entity != null ) {
		throw new NonUniqueObjectException( key.getIdentifier(), key.getEntityName() );
	}
}
 
开发者ID:cacheonix,项目名称:cacheonix-core,代码行数:16,代码来源:StatefulPersistenceContext.java

示例12: load

import org.hibernate.NonUniqueObjectException; //导入依赖的package包/类
/**
 * Perfoms the load of an entity.
 *
 * @return The loaded entity.
 * @throws HibernateException
 */
protected Object load(
	final LoadEvent event, 
	final EntityPersister persister, 
	final EntityKey keyToLoad, 
	final LoadEventListener.LoadType options)
throws HibernateException {

	if ( event.getInstanceToLoad() != null ) {
		if ( event.getSession().getPersistenceContext().getEntry( event.getInstanceToLoad() ) != null ) {
			throw new PersistentObjectException(
					"attempted to load into an instance that was already associated with the session: " +
					MessageHelper.infoString( persister, event.getEntityId(), event.getSession().getFactory() )
				);
		}
		persister.setIdentifier( event.getInstanceToLoad(), event.getEntityId(), event.getSession().getEntityMode() );
	}

	Object entity = doLoad(event, persister, keyToLoad, options);
	
	boolean isOptionalInstance = event.getInstanceToLoad() != null;
	
	if ( !options.isAllowNulls() || isOptionalInstance ) {
		if ( entity == null ) {
			event.getSession().getFactory().getEntityNotFoundDelegate().handleEntityNotFound( event.getEntityClassName(), event.getEntityId() );
		}
	}

	if ( isOptionalInstance && entity != event.getInstanceToLoad() ) {
		throw new NonUniqueObjectException( event.getEntityId(), event.getEntityClassName() );
	}

	return entity;
}
 
开发者ID:cacheonix,项目名称:cacheonix-core,代码行数:40,代码来源:DefaultLoadEventListener.java

示例13: store

import org.hibernate.NonUniqueObjectException; //导入依赖的package包/类
public <T> void store( T entity ) 
{
    if ( log.isDebugEnabled() )
        log.debug( "attempting to store (make persistent) object " 
                 + entity
                 );
        
    Session s = getHibernateSession();
    
    if ( entity instanceof EurocarbObject )
        validate( (EurocarbObject) entity );
    
    try
    {
        s.save( entity );
        //s.saveOrUpdate( entity );
    }
    catch ( NonUniqueObjectException ex )
    {
        log.warn( "passed " 
             + entity.getClass().getSimpleName() 
             + " was originally loaded in a different Session"
             + "; attempting to merge changes..."
             , ex 
         );
        
        s.merge( entity );   
        log.debug( "changes merged ok" );
    }
}
 
开发者ID:glycoinfo,项目名称:eurocarbdb,代码行数:31,代码来源:HibernateEntityManager.java

示例14: __log_exception

import org.hibernate.NonUniqueObjectException; //导入依赖的package包/类
private final void __log_exception( NonUniqueObjectException e, Object entity )
{
    log.warn( "Caught NonUniqueObjectException while working with "
            + entity.getClass().getName()
            + ". This often means that you need to (re-)implement "
            + "the equals(Object) & hashCode() methods in this class "
            + "so that objects in the session can be compared for "
            + "*equality* rather than *identity*.",
            e
            );
}
 
开发者ID:glycoinfo,项目名称:eurocarbdb,代码行数:12,代码来源:HibernateEntityManager.java

示例15: lock

import org.hibernate.NonUniqueObjectException; //导入依赖的package包/类
public static boolean lock(SessionFactory sessionFactory, Object target) {

		Session session = sessionFactory.getCurrentSession();

		try {
			session.buildLockRequest(LockOptions.NONE).lock(target);
			return true;
		} catch (NonUniqueObjectException e) {
			//  different object with the same identifier value was already associated with the session
			return false;
		}
	}
 
开发者ID:curtiszimmerman,项目名称:AlgoTrader,代码行数:13,代码来源:HibernateUtil.java


注:本文中的org.hibernate.NonUniqueObjectException类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。