| Method from org.hibernate.ejb.AbstractEntityManagerImpl Detail: |
public void clear() {
//adjustFlushMode();
try {
getSession().clear();
}
catch (HibernateException he) {
throwPersistenceException( he );
}
}
|
public boolean contains(Object entity) {
try {
if ( entity != null
&& ! ( entity instanceof HibernateProxy )
&& getSession().getSessionFactory().getClassMetadata( entity.getClass() ) == null ) {
throw new IllegalArgumentException( "Not an entity:" + entity.getClass() );
}
return getSession().contains( entity );
}
catch (MappingException e) {
throw new IllegalArgumentException( e.getMessage(), e );
}
catch (HibernateException he) {
throwPersistenceException( he );
return false;
}
}
|
public Query createNamedQuery(String name) {
//adjustFlushMode();
try {
return new QueryImpl( getSession().getNamedQuery( name ), this );
}
catch (HibernateException he) {
throwPersistenceException( he );
return null;
}
}
|
public Query createNativeQuery(String sqlString) {
//adjustFlushMode();
try {
SQLQuery q = getSession().createSQLQuery( sqlString );
return new QueryImpl( q, this );
}
catch (HibernateException he) {
throwPersistenceException( he );
return null;
}
}
|
public Query createNativeQuery(String sqlString,
Class resultClass) {
//adjustFlushMode();
try {
SQLQuery q = getSession().createSQLQuery( sqlString );
q.addEntity( "alias1", resultClass.getName(), LockMode.READ );
return new QueryImpl( q, this );
}
catch (HibernateException he) {
throwPersistenceException( he );
return null;
}
}
|
public Query createNativeQuery(String sqlString,
String resultSetMapping) {
//adjustFlushMode();
try {
SQLQuery q = getSession().createSQLQuery( sqlString );
q.setResultSetMapping( resultSetMapping );
return new QueryImpl( q, this );
}
catch (HibernateException he) {
throwPersistenceException( he );
return null;
}
}
|
public Query createQuery(String ejbqlString) {
//adjustFlushMode();
try {
return new QueryImpl( getSession().createQuery( ejbqlString ), this );
}
catch (HibernateException he) {
throwPersistenceException( he );
return null;
}
}
|
public A find(Class entityClass,
Object primaryKey) {
//adjustFlushMode();
try {
return (A) getSession().get( entityClass, (Serializable) primaryKey );
}
catch (ObjectDeletedException e) {
//the spec is silent about people doing remove() find() on the same PC
return null;
}
catch (ObjectNotFoundException e) {
//should not happen on the entity itself with get
throw new IllegalArgumentException( e.getMessage(), e );
}
catch (MappingException e) {
throw new IllegalArgumentException( e.getMessage(), e );
}
catch (TypeMismatchException e ) {
throw new IllegalArgumentException( e.getMessage(), e );
}
catch (ClassCastException e) {
throw new IllegalArgumentException( e.getMessage(), e );
}
catch (HibernateException he) {
throwPersistenceException( he );
return null;
}
}
|
public void flush() {
try {
if ( ! isTransactionInProgress() ) {
throw new TransactionRequiredException( "no transaction is in progress" );
}
//adjustFlushMode();
getSession().flush();
}
catch (HibernateException he) {
throwPersistenceException( he );
}
}
|
public Object getDelegate() {
return getSession();
}
returns the underlying session |
public FlushModeType getFlushMode() {
FlushMode mode = getSession().getFlushMode();
if ( mode == FlushMode.AUTO ) {
this.flushModeType = FlushModeType.AUTO;
}
else if ( mode == FlushMode.COMMIT ) {
this.flushModeType = FlushModeType.COMMIT;
}
// else if ( mode == FlushMode.NEVER ) {
// if ( PersistenceContextType.EXTENDED == persistenceContextType && !isTransactionInProgress() ) {
// //we are in flushMode none for EXTENDED
// return flushMode;
// }
// else {
// return null; //TODO exception?
// }
// }
else {
return null; //TODO exception?
}
//otherwise this is an unknown mode for EJB3
return flushModeType;
}
|
abstract protected Session getRawSession()
Return a Session (even if the entity manager is closed |
public T getReference(Class entityClass,
Object primaryKey) {
//adjustFlushMode();
try {
return (T) getSession().load( entityClass, (Serializable) primaryKey );
}
catch (MappingException e) {
throw new IllegalArgumentException( e.getMessage(), e );
}
catch (TypeMismatchException e ) {
throw new IllegalArgumentException( e.getMessage(), e );
}
catch (ClassCastException e) {
throw new IllegalArgumentException( e.getMessage(), e );
}
catch (HibernateException he) {
throwPersistenceException( he );
return null;
}
}
|
abstract public Session getSession()
|
public EntityTransaction getTransaction() {
if ( transactionType == PersistenceUnitTransactionType.JTA ) {
throw new IllegalStateException( "A JTA EntityManager cannot use getTransaction()" );
}
return tx;
}
|
public boolean isTransactionInProgress() {
return ( (SessionImplementor) getRawSession() ).isTransactionInProgress();
}
|
public void joinTransaction() {
joinTransaction( false );
}
|
public void lock(Object entity,
LockModeType lockMode) {
try {
if ( ! isTransactionInProgress() ) {
throw new TransactionRequiredException( "no transaction is in progress" );
}
//adjustFlushMode();
if ( !contains( entity ) ) throw new IllegalArgumentException( "entity not in the persistence context" );
getSession().lock( entity, getLockMode( lockMode ) );
}
catch (HibernateException he) {
throwPersistenceException( he );
}
}
|
protected void markAsRollback() {
log.debug( "mark transaction for rollback");
if ( tx.isActive() ) {
tx.setRollbackOnly();
}
else {
//no explicit use of the tx. boudaries methods
if ( PersistenceUnitTransactionType.JTA == transactionType ) {
TransactionManager transactionManager =
( (SessionFactoryImplementor) getRawSession().getSessionFactory() ).getTransactionManager();
if ( transactionManager == null ) {
throw new PersistenceException(
"Using a JTA persistence context wo setting hibernate.transaction.manager_lookup_class"
);
}
try {
transactionManager.setRollbackOnly();
}
catch (SystemException e) {
throw new PersistenceException( "Unable to set the JTA transaction as RollbackOnly", e );
}
}
}
}
|
public A merge(A entity) {
checkTransactionNeeded();
//adjustFlushMode();
try {
return (A) getSession().merge( entity );
}
catch (ObjectDeletedException sse) {
throw new IllegalArgumentException( sse );
}
catch (MappingException e) {
throw new IllegalArgumentException( e.getMessage(), e );
}
catch (HibernateException he) {
throwPersistenceException( he );
return null;
}
}
|
public void persist(Object entity) {
checkTransactionNeeded();
//adjustFlushMode();
try {
getSession().persist( entity );
}
catch (MappingException e) {
throw new IllegalArgumentException( e.getMessage() );
}
catch (HibernateException he) {
throwPersistenceException( he );
}
}
|
protected void postInit() {
//register in Sync if needed
if ( PersistenceUnitTransactionType.JTA.equals( transactionType ) ) joinTransaction( true );
Object flushMode = properties.get( "org.hibernate.flushMode" );
if (flushMode != null) {
getSession().setFlushMode( ConfigurationHelper.getFlushMode( flushMode ) );
}
this.properties = null;
}
|
public void refresh(Object entity) {
checkTransactionNeeded();
//adjustFlushMode();
try {
if ( ! getSession().contains( entity ) ) {
throw new IllegalArgumentException( "Entity not managed" );
}
getSession().refresh( entity );
}
catch (MappingException e) {
throw new IllegalArgumentException( e.getMessage(), e );
}
catch (HibernateException he) {
throwPersistenceException( he );
}
}
|
public void remove(Object entity) {
checkTransactionNeeded();
//adjustFlushMode();
try {
getSession().delete( entity );
}
catch (MappingException e) {
throw new IllegalArgumentException( e.getMessage(), e );
}
catch (HibernateException he) {
throwPersistenceException( he );
}
}
|
public void setFlushMode(FlushModeType flushModeType) {
this.flushModeType = flushModeType;
if ( flushModeType == FlushModeType.AUTO ) {
getSession().setFlushMode( FlushMode.AUTO );
}
else if ( flushModeType == FlushModeType.COMMIT ) {
getSession().setFlushMode( FlushMode.COMMIT );
}
else {
throw new AssertionFailure( "Unknown FlushModeType: " + flushModeType );
}
}
|
public void throwPersistenceException(PersistenceException e) {
if ( ! ( e instanceof NoResultException || e instanceof NonUniqueResultException ) ) {
try {
markAsRollback();
}
catch (Exception ne) {
//we do not want the subsequent exception to swallow the original one
log.error( "Unable to mark for rollback on PersistenceException: ", ne);
}
}
throw e;
}
|
public void throwPersistenceException(HibernateException e) {
if ( e instanceof StaleStateException ) {
PersistenceException pe = wrapStaleStateException( (StaleStateException) e );
throwPersistenceException( pe );
}
else if ( e instanceof ConstraintViolationException ) {
//FIXME this is bad cause ConstraintViolationException happens in other circumstances
throwPersistenceException( new EntityExistsException( e ) );
}
else if ( e instanceof ObjectNotFoundException ) {
throwPersistenceException( new EntityNotFoundException( e.getMessage() ) );
}
else if ( e instanceof org.hibernate.NonUniqueResultException ) {
throwPersistenceException( new NonUniqueResultException( e.getMessage() ) );
}
else if ( e instanceof UnresolvableObjectException ) {
throwPersistenceException( new EntityNotFoundException( e.getMessage() ) );
}
else if ( e instanceof QueryException ) {
throw new IllegalArgumentException( e );
}
else if ( e instanceof TransientObjectException ) {
try {
markAsRollback();
}
catch (Exception ne) {
//we do not want the subsequent exception to swallow the original one
log.error( "Unable to mark for rollback on TransientObjectException: ", ne);
}
throw new IllegalStateException( e ); //Spec 3.2.3 Synchronization rules
}
else {
throwPersistenceException( new PersistenceException( e ) );
}
}
|
public PersistenceException wrapStaleStateException(StaleStateException e) {
PersistenceException pe;
if ( e instanceof StaleObjectStateException ) {
StaleObjectStateException sose = (StaleObjectStateException) e;
Serializable identifier = sose.getIdentifier();
if (identifier != null) {
Object entity = getRawSession().load( sose.getEntityName(), identifier );
if ( entity instanceof Serializable ) {
//avoid some user errors regarding boundary crossing
pe = new OptimisticLockException( null, e, entity );
}
else {
pe = new OptimisticLockException( e );
}
}
else {
pe = new OptimisticLockException( e );
}
}
else {
pe = new OptimisticLockException( e );
}
return pe;
}
|