📄 entitybinder.java
字号:
//$Id: EntityBinder.java,v 1.5 2005/02/19 19:07:49 epbernard Exp $package org.hibernate.cfg.annotations;import org.hibernate.util.StringHelper;import org.hibernate.util.ReflectHelper;import org.hibernate.AssertionFailure;import org.hibernate.MappingException;import org.hibernate.AnnotationException;import org.hibernate.engine.Versioning;import org.hibernate.annotations.OptimisticLockType;import org.hibernate.annotations.PolymorphismType;import org.hibernate.annotations.BatchSize;import org.hibernate.annotations.Proxy;import org.hibernate.annotations.Where;import org.hibernate.mapping.PersistentClass;import org.hibernate.mapping.RootClass;import org.hibernate.cfg.AnnotationBinder;import org.hibernate.cfg.ExtendedMappings;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import javax.ejb.Entity;import javax.ejb.AccessType;import java.lang.reflect.Modifier;/** * Stateful holder and processor for binding Entity information * * @author Emmanuel Bernard */public class EntityBinder { private String name; private Class annotatedClass; private PersistentClass persistentClass; private ExtendedMappings mappings; private static Log log = LogFactory.getLog(EntityBinder.class); private String discriminatorValue = ""; private boolean propertyAccess = false; private boolean dynamicInsert; private boolean dynamicUpdate; private boolean mutable; private OptimisticLockType optimisticLockType; private String persister; private PolymorphismType polymorphismType; private boolean selectBeforeUpdate; private int batchSize; private boolean lazy; private String proxyClassName; private String where; public boolean isPropertyAccess() { return propertyAccess; } public EntityBinder(Entity ejb3Ann, org.hibernate.annotations.Entity hibAnn, Class annotatedClass, PersistentClass persistentClass, ExtendedMappings mappings) { this.mappings = mappings; this.persistentClass = persistentClass; this.annotatedClass = annotatedClass; bindEjb3Annotation(ejb3Ann); bindHibernateAnnotation(hibAnn); } private void bindHibernateAnnotation(org.hibernate.annotations.Entity hibAnn) { if (hibAnn != null) { dynamicInsert = hibAnn.dynamicInsert(); dynamicUpdate = hibAnn.dynamicUpdate(); mutable = hibAnn.mutable(); optimisticLockType = hibAnn.optimisticLock(); persister = hibAnn.persister(); selectBeforeUpdate = hibAnn.selectBeforeUpdate(); polymorphismType = hibAnn.polymorphism(); } else { //default values when the annotation is not there dynamicInsert = false; dynamicUpdate = false; mutable = true; optimisticLockType = OptimisticLockType.VERSION; persister = ""; polymorphismType = PolymorphismType.IMPLICIT; selectBeforeUpdate = false; } } private void bindEjb3Annotation(Entity ejb3Ann) { if (ejb3Ann == null) throw new AssertionFailure("@Entity should always be not null"); if ( AnnotationBinder.isDefault( ejb3Ann.name() ) ) { name = StringHelper.unqualify( annotatedClass.getName() ); } else { name = ejb3Ann.name(); } propertyAccess = ejb3Ann.access() == AccessType.PROPERTY; } public void setDiscriminatorValue(String discriminatorValue) { this.discriminatorValue = discriminatorValue; } public void bindEntity() { persistentClass.setAbstract( Modifier.isAbstract( annotatedClass.getModifiers() ) ); persistentClass.setClassName( annotatedClass.getName() ); //persistentClass.setDynamic(false); //no longer needed with the Entity name refactoring? persistentClass.setEntityName( annotatedClass.getName() ); if ( StringHelper.isEmpty( discriminatorValue ) ) { persistentClass.setDiscriminatorValue( persistentClass.getEntityName() ); } else { persistentClass.setDiscriminatorValue(discriminatorValue); } /* * Gavin asked me to make all class as lazy since it is * allowed by the EJB3 spec */ persistentClass.setLazy(lazy); if (! StringHelper.isEmpty(proxyClassName) ) { persistentClass.setProxyInterfaceName(proxyClassName); } persistentClass.setDynamicInsert(dynamicInsert); persistentClass.setDynamicUpdate(dynamicUpdate); if (persistentClass instanceof RootClass) { RootClass rootClass = (RootClass) persistentClass; rootClass.setMutable(mutable); rootClass.setExplicitPolymorphism( isExplicitPolymorphism(polymorphismType) ); if ( StringHelper.isNotEmpty(where) ) rootClass.setWhere(where); } persistentClass.setOptimisticLockMode( getVersioning(optimisticLockType) ); persistentClass.setSelectBeforeUpdate(selectBeforeUpdate); if (! AnnotationBinder.isDefault(persister) ) { try { persistentClass.setEntityPersisterClass( ReflectHelper.classForName(persister) ); } catch ( ClassNotFoundException cnfe ) { throw new AnnotationException("Could not find persister class: " + persister); } } persistentClass.setBatchSize(batchSize); log.debug("Import with entity name=" + name); try { mappings.addImport(persistentClass.getEntityName(), name); } catch (MappingException me) { throw new AnnotationException("Use of the same entity name twice: " + name); } } int getVersioning(OptimisticLockType type) { switch(type) { case VERSION: return Versioning.OPTIMISTIC_LOCK_VERSION; case NONE: return Versioning.OPTIMISTIC_LOCK_NONE; case DIRTY: return Versioning.OPTIMISTIC_LOCK_DIRTY; case ALL: return Versioning.OPTIMISTIC_LOCK_ALL; default: throw new AssertionFailure("optimistic locking not supported: " + type); } } private boolean isExplicitPolymorphism(PolymorphismType type) { switch(type) { case IMPLICIT: return false; case EXPLICIT: return true; default: throw new AssertionFailure("Unknown polymorphism type: " + type); } } public void setBatchSize(BatchSize sizeAnn) { if (sizeAnn != null) { batchSize = sizeAnn.size(); } else { batchSize = -1; } } public void setProxy(Proxy proxy) { if (proxy != null) { lazy = proxy.lazy(); if (!lazy) { proxyClassName = null; } else { if ( AnnotationBinder.isDefault( proxy.proxyClassName() ) ) { proxyClassName = annotatedClass.getName(); } else { proxyClassName = proxy.proxyClassName(); } } } else { lazy = true; proxyClassName = annotatedClass.getName(); } } public void setWhere(Where whereAnn) { if (whereAnn != null) { where = whereAnn.clause(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -