下面列出了org.hibernate.Session#contains ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
@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;
}
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 );
}
/**
* @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;
}
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();
}
}
}
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();
}
}
}
@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);
}
}