⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classmapping.java

📁 Hibernate深入浅出
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
//$Id: ClassMapping.java,v 1.18.2.6 2004/07/22 21:26:28 maxcsaucdk Exp $package net.sf.hibernate.tool.hbm2java;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.SortedMap;import java.util.SortedSet;import java.util.TreeMap;import java.util.TreeSet;import net.sf.hibernate.CompositeUserType;import net.sf.hibernate.UserType;import net.sf.hibernate.type.PrimitiveType;import net.sf.hibernate.type.Type;import net.sf.hibernate.type.TypeFactory;import net.sf.hibernate.util.ReflectHelper;import net.sf.hibernate.util.StringHelper;import org.apache.commons.collections.MultiMap;import org.apache.commons.lang.StringUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.jdom.Attribute;import org.jdom.Element;public class ClassMapping extends MappingElement {       static private Log log = LogFactory.getLog(ClassMapping.class);          private ClassName name = null;   private ClassName generatedName = null;   private String superClass = null;   private ClassMapping superClassMapping = null;   private String proxyClass = null;   private List fields = new ArrayList();   private TreeSet imports = new TreeSet();   private List subclasses = new ArrayList();   private final Map components = new HashMap();   private boolean mustImplementEquals = false;      private boolean shouldBeAbstract = false;            public ClassMapping(String classPackage, MappingElement parentElement, ClassName superClass, ClassMapping superClassMapping, Element classElement, MultiMap inheritedMeta) {         this(classPackage, parentElement, superClass, classElement, inheritedMeta);               this.superClassMapping = superClassMapping;                  if(this.superClassMapping!=null) {             List l = this.superClassMapping.getAllFieldsForFullConstructor();             for (Iterator iter = l.iterator(); iter.hasNext();) {                FieldProperty element = (FieldProperty) iter.next();                ClassName ct = element.getClassType();                if(ct!=null) { // add imports for superclasses possible fields.                 addImport(ct);                 } else {                 addImport(element.getFullyQualifiedTypeName());                }            }         }    }      public ClassMapping(String classPackage, MappingElement parentElement, ClassName superClass, Element classElement, MultiMap inheritedMeta)  {      super(classElement, parentElement);      initWith(classPackage, superClass, classElement, false, inheritedMeta);   }      public ClassMapping(String classPackage, Element classElement, MappingElement parentElement, MultiMap inheritedMeta) {       super(classElement, parentElement);      initWith(classPackage, null, classElement, false, inheritedMeta);   }      public ClassMapping(String classPackage, Element classElement, MappingElement parentElement, boolean component, MultiMap inheritedMeta)  {       super(classElement, parentElement);      initWith(classPackage, null, classElement, component, inheritedMeta);   }    protected void initWith(String classPackage, ClassName mySuperClass, Element classElement, boolean component, MultiMap inheritedMeta) {        	String fullyQualifiedName = classElement.getAttributeValue(component?"class":"name");        	if(fullyQualifiedName.indexOf('.')<0 && classPackage!=null && classPackage.trim().length()>0) {    		fullyQualifiedName = classPackage + "." + fullyQualifiedName;    	}    	log.debug("Processing mapping for class: " + fullyQualifiedName);        	setMetaAttribs(MetaAttributeHelper.loadAndMergeMetaMap(classElement, inheritedMeta));        	//    class & package names    	name = new ClassName(fullyQualifiedName);    	    	if(getMeta("generated-class")!=null) {    		generatedName = new ClassName(getMetaAsString("generated-class").trim());    		shouldBeAbstract = true;    		log.warn("Generating " + generatedName + " instead of " + name);       	} else {    		generatedName = name;    	}    	    	if(mySuperClass!=null) {    		this.superClass = mySuperClass.getName();    		addImport(mySuperClass); // can only be done AFTER this class gets its own name.    	}    	            // get the properties defined for this class      List propertyList = new ArrayList();      propertyList.addAll( classElement.getChildren("property") );      propertyList.addAll( classElement.getChildren("version") );      propertyList.addAll( classElement.getChildren("timestamp") );      propertyList.addAll( classElement.getChildren("key-property") );      propertyList.addAll( classElement.getChildren("any"));      propertyList.addAll( classElement.getChildren("parent"));          // get all many-to-one associations defined for the class      List manyToOneList = new ArrayList();      manyToOneList.addAll( classElement.getChildren("many-to-one") );      manyToOneList.addAll( classElement.getChildren("key-many-to-one") );          Attribute att = classElement.getAttribute("proxy");      if (att!=null) proxyClass = att.getValue();          Element id = classElement.getChild("id");          if (id != null) {      	 propertyList.add(0, id);      	// implementEquals();      }          // composite id      Element cmpid = classElement.getChild("composite-id");      if (cmpid != null) {      	          String cmpname = cmpid.getAttributeValue("name");         String cmpclass = cmpid.getAttributeValue("class");         if ( cmpname==null || cmpname.equals(StringHelper.EMPTY_STRING) ) {            //Embedded composite id            //implementEquals();            propertyList.addAll(0, cmpid.getChildren("key-property") );            manyToOneList.addAll(0, cmpid.getChildren("key-many-to-one") );         }          else {         	implementEquals();         	//Composite id class            ClassMapping mapping = new ClassMapping(classPackage, cmpid, this, true, getMetaAttribs());            MultiMap metaForCompositeid = MetaAttributeHelper.loadAndMergeMetaMap(cmpid, getMetaAttribs());            mapping.implementEquals();            ClassName classType = new ClassName(cmpclass);            // add an import and field for this property            addImport(classType);            FieldProperty cmpidfield =  new FieldProperty(cmpid, this, cmpname, classType, false, true, false, metaForCompositeid);            addFieldProperty(cmpidfield);            components.put( mapping.getFullyQualifiedName(), mapping);         }      }          // checked after the default sets of implement equals.      if(getMetaAsBool("implement-equals")) {      	implementEquals();      } else if(getMeta("implement-equals")!=null && !getMetaAsBool("implement-equals")){      	implementEquals(false);      }            // derive the class imports and fields from the properties      for (Iterator properties = propertyList.iterator(); properties.hasNext();) {         Element propertyElement = (Element) properties.next();                  MultiMap metaForProperty = MetaAttributeHelper.loadAndMergeMetaMap(propertyElement, getMetaAttribs());         String propertyName = propertyElement.getAttributeValue("name");         if ( propertyName == null || propertyName.trim().equals(StringHelper.EMPTY_STRING) ) {            continue; //since an id doesn't necessarily need a name         }                  // ensure that the type is specified         String type = propertyElement.getAttributeValue("type");         if (type == null && cmpid != null)  { // for composite-keys           type = propertyElement.getAttributeValue("class");         }         if("timestamp".equals(propertyElement.getName())){             type = "java.util.Date";         }                  if("any".equals(propertyElement.getName())) {             type = "java.lang.Object";         }                  if("parent".equals(propertyElement.getName())) {         	type = ((ClassMapping)this.getParentElement()).getFullyQualifiedName();         }                  if ( type == null || type.trim().equals(StringHelper.EMPTY_STRING) ) {            log.warn("property \"" + propertyName + "\" in class " + getName() + " is missing a type attribute");            continue;         }                           // handle in a different way id and properties...         // ids may be generated and may need to be of object type in order to support         // the unsaved-value "null" value.         // Properties may be nullable (ids may not)         if (propertyElement == id)  {            Element generator = propertyElement.getChild("generator");            String unsavedValue = propertyElement.getAttributeValue("unsaved-value");            boolean mustBeNullable = ( unsavedValue != null && unsavedValue.equals("null") );            boolean generated = !generator.getAttributeValue("class").equals("assigned");            ClassName rtype = getFieldType(type, mustBeNullable, false);            addImport(rtype);            FieldProperty idField = new FieldProperty(propertyElement, this, propertyName, rtype, false, true, generated, metaForProperty);            addFieldProperty(idField);         }          else {            String notnull = propertyElement.getAttributeValue("not-null");            // if not-null property is missing lets see if it has been            // defined at column level            if(notnull == null) {               Element column = propertyElement.getChild("column");               if(column != null)                  notnull = column.getAttributeValue("not-null");            }            boolean nullable = ( notnull == null || notnull.equals("false") );         	boolean key = propertyElement.getName().startsWith("key-"); //a composite id property            ClassName t = getFieldType(type,nullable,false);            addImport(t);            FieldProperty stdField =new FieldProperty(propertyElement, this, propertyName, t, nullable && !key, key, false, metaForProperty);            addFieldProperty(stdField);                     }      }          // one to ones      List onetooneList = classElement.getChildren("one-to-one");      for ( Iterator onetoones = onetooneList.iterator(); onetoones.hasNext(); ) {            Element onetoone = (Element) onetoones.next();    		            MultiMap metaForOneToOne = MetaAttributeHelper.loadAndMergeMetaMap(onetoone,getMetaAttribs());            String propertyName = onetoone.getAttributeValue("name");                        // ensure that the class is specified            String clazz = onetoone.getAttributeValue("class");            if( StringUtils.isEmpty(clazz) ) {                log.warn("one-to-one \"" + name + "\" in class " + getName() + " is missing a class attribute");                continue;            }            ClassName cn = getFieldType(clazz);            addImport(cn);            FieldProperty fm =  new FieldProperty(onetoone, this, propertyName, cn, true, metaForOneToOne);            addFieldProperty(fm);                  }          // many to ones - TODO: consolidate with code above      for ( Iterator manytoOnes = manyToOneList.iterator(); manytoOnes.hasNext(); ) {         Element manyToOne = (Element) manytoOnes.next();                  MultiMap metaForManyToOne = MetaAttributeHelper.loadAndMergeMetaMap(manyToOne,getMetaAttribs());         String propertyName = manyToOne.getAttributeValue("name");                  // ensure that the type is specified         String type = manyToOne.getAttributeValue("class");         if ( StringUtils.isEmpty(type) ) {            log.warn("many-to-one \"" + propertyName + "\" in class " + getName() + " is missing a class attribute");            continue;         }         ClassName classType = new ClassName(type);                  // is it nullable?         String notnull = manyToOne.getAttributeValue("not-null");         boolean nullable = ( notnull == null || notnull.equals("false") );         boolean key = manyToOne.getName().startsWith("key-"); //a composite id property          

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -