本文整理汇总了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;
});
}
示例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;
});
}
示例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();
}
示例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();
}
示例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;
}
}
示例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;
}
示例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());
}
示例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;
}
}
}
}
示例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() );
}
}
示例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;
}
示例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() );
}
}
示例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;
}
示例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" );
}
}
示例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
);
}
示例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;
}
}