Java Code Examples for org.hibernate.Session#contains()

The following examples show how to use org.hibernate.Session#contains() . 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: HibernatePeriodStore.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Period reloadPeriod( Period period )
{
    Session session = sessionFactory.getCurrentSession();

    if ( session.contains( period ) )
    {
        return period; // Already in session, no reload needed
    }

    Long id = PERIOD_ID_CACHE.get( period.getCacheKey(), key -> getPeriodId( period.getStartDate(), period.getEndDate(), period.getPeriodType() ) )
        .orElse( null );

    Period storedPeriod = id != null ? getSession().get( Period.class, id ) : null;

    return storedPeriod != null ? storedPeriod.copyTransientProperties( period ) : null;
}
 
Example 2
Source File: HibernatePeriodStore.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public PeriodType reloadPeriodType( PeriodType periodType )
{
    Session session = sessionFactory.getCurrentSession();

    if ( periodType == null || session.contains( periodType ) )
    {
        return periodType;
    }

    PeriodType reloadedPeriodType = getPeriodType( periodType.getClass() );

    if ( reloadedPeriodType == null )
    {
        throw new InvalidIdentifierReferenceException( "The PeriodType referenced by the Period is not in database: "
            + periodType.getName() );
    }

    return reloadedPeriodType;
}
 
Example 3
Source File: RemovedEntityTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testRemoveThenContains() {
	Session s = openSession();
	s.beginTransaction();
	Item item = new Item();
	item.setName( "dummy" );
	s.persist( item );
	s.getTransaction().commit();
	s.close();

	s = openSession();
	s.beginTransaction();
	s.delete( item );
	boolean contains = s.contains( item );
	s.getTransaction().commit();
	s.close();

	assertFalse( "expecting removed entity to not be contained", contains );
}
 
Example 4
Source File: ForeignGenerator.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @see org.hibernate.id.IdentifierGenerator#generate(org.hibernate.engine.SessionImplementor, java.lang.Object)
 */
public Serializable generate(SessionImplementor sessionImplementor, Object object)
throws HibernateException {
	
	Session session = (Session) sessionImplementor;

	Object associatedObject = sessionImplementor.getFactory()
	        .getClassMetadata( entityName )
	        .getPropertyValue( object, propertyName, session.getEntityMode() );
	
	if ( associatedObject == null ) {
		throw new IdentifierGenerationException(
				"attempted to assign id from null one-to-one property: " + 
				propertyName
			);
	}
	
	EntityType type = (EntityType) sessionImplementor.getFactory()
       	.getClassMetadata( entityName )
       	.getPropertyType( propertyName );

	Serializable id;
	try {
		id = ForeignKeys.getEntityIdentifierIfNotUnsaved(
				type.getAssociatedEntityName(), 
				associatedObject, 
				sessionImplementor
			); 
	}
	catch (TransientObjectException toe) {
		id = session.save( type.getAssociatedEntityName(), associatedObject );
	}

	if ( session.contains(object) ) {
		//abort the save (the object is already saved by a circular cascade)
		return IdentifierGeneratorFactory.SHORT_CIRCUIT_INDICATOR;
		//throw new IdentifierGenerationException("save associated object first, or disable cascade for inverse association");
	}
	return id;
}
 
Example 5
Source File: CargoRepositoryTest.java    From dddsample-core with MIT License 5 votes vote down vote up
private Long getLongId(Object o) {
    final Session session = sessionFactory.getCurrentSession();
    if (session.contains(o)) {
        return (Long) session.getIdentifier(o);
    } else {
        try {
            Field id = o.getClass().getDeclaredField("id");
            id.setAccessible(true);
            return (Long) id.get(o);
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
}
 
Example 6
Source File: HandlingEventRepositoryTest.java    From dddsample-core with MIT License 5 votes vote down vote up
private Long getLongId(Object o) {
    final Session session = sessionFactory.getCurrentSession();
    if (session.contains(o)) {
        return (Long) session.getIdentifier(o);
    } else {
        try {
            Field id = o.getClass().getDeclaredField("id");
            id.setAccessible(true);
            return (Long) id.get(o);
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
}
 
Example 7
Source File: MCRHIBFileMetadataStore.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void storeNode(MCRFilesystemNode node) throws MCRPersistenceException {

    String id = node.getID();
    String pid = node.getParentID();
    String owner = node.getOwnerID();
    String name = node.getName();
    String label = node.getLabel();
    long size = node.getSize();

    GregorianCalendar date = node.getLastModified();

    String type = null;
    String storeid = null;
    String storageid = null;
    String fctid = null;
    String md5 = null;

    int numchdd = 0;
    int numchdf = 0;
    int numchtd = 0;
    int numchtf = 0;

    if (node instanceof MCRFile) {
        MCRFile file = (MCRFile) node;

        type = "F";
        storeid = file.getStoreID();
        storageid = file.getStorageID();
        fctid = file.getContentTypeID();
        md5 = file.getMD5();
    } else if (node instanceof MCRDirectory) {
        MCRDirectory dir = (MCRDirectory) node;

        type = "D";
        numchdd = dir.getNumChildren(MCRDirectory.DIRECTORIES, MCRDirectory.HERE);
        numchdf = dir.getNumChildren(MCRDirectory.FILES, MCRDirectory.HERE);
        numchtd = dir.getNumChildren(MCRDirectory.DIRECTORIES, MCRDirectory.TOTAL);
        numchtf = dir.getNumChildren(MCRDirectory.FILES, MCRDirectory.TOTAL);
    } else {
        throw new MCRPersistenceException("MCRFilesystemNode must be either MCRFile or MCRDirectory");
    }

    Session session = getSession();
    MCRFSNODES fs = session.get(MCRFSNODES.class, id);
    if (fs == null) {
        fs = new MCRFSNODES();
        fs.setId(id);
    }
    fs.setPid(pid);
    fs.setType(type);
    fs.setOwner(owner);
    fs.setName(name);
    fs.setLabel(label);
    fs.setSize(size);
    fs.setDate(new Timestamp(date.getTime().getTime()));
    fs.setStoreid(storeid);
    fs.setStorageid(storageid);
    fs.setFctid(fctid);
    fs.setMd5(md5);
    fs.setNumchdd(numchdd);
    fs.setNumchdf(numchdf);
    fs.setNumchtd(numchtd);
    fs.setNumchtf(numchtf);
    if (!session.contains(fs)) {
        session.saveOrUpdate(fs);
    }
}