nodefactory.java
来自「Hibernate开发及整合应用大全 蔡雪焘编著 本书用典型的示例剖析Hiber」· Java 代码 · 共 164 行
JAVA
164 行
/* * Created on 03-08-2003 * * To change the template for this generated file go to * Window - Preferences - Java - Code Generation - Code and Comments */package net.sf.hibernate.console.node;import java.util.ArrayList;import java.util.Map;import java.util.Set;import javax.swing.Icon;import javax.swing.ImageIcon;import net.sf.hibernate.console.Start;import net.sf.hibernate.HibernateException;import net.sf.hibernate.Session;import net.sf.hibernate.SessionFactory;import net.sf.hibernate.metadata.ClassMetadata;import net.sf.hibernate.metadata.CollectionMetadata;import net.sf.hibernate.proxy.HibernateProxyHelper;import net.sf.hibernate.type.EntityType;import net.sf.hibernate.type.PersistentCollectionType;import net.sf.hibernate.type.Type;/** * @author MAX */public class NodeFactory { SessionFactory sf; private Map classMetaData; private ArrayList classes; private Map collectionMetaData; /** * @param sf2 */ public NodeFactory(SessionFactory sf2) { try { setSessionFactory(sf2); } catch(HibernateException he) { throw new RuntimeException("nodeFactory()", he); } } void setSessionFactory(SessionFactory sf) throws HibernateException { classMetaData = sf.getAllClassMetadata(); collectionMetaData = sf.getAllCollectionMetadata(); Set keyset = classMetaData.keySet(); classes = new ArrayList(); classes.addAll(keyset); } public BaseNode createRootNode() { return new RootNode(this, classes); } public ClassNode createClassNode(BaseNode node, Class clazz) { return new ClassNode(this, node, clazz.getName(), getMetaData(clazz),null,false); } public ClassMetadata getMetaData(Class clazz) { Object o = classMetaData.get(clazz); return (ClassMetadata) o; } /** * @param string */ public CollectionMetadata getCollectionMetaData(String role) { return (CollectionMetadata) collectionMetaData.get(role); } public BaseNode createPropertyNode(BaseNode parent, int idx, ClassMetadata metadata) { return createPropertyNode(parent, idx, metadata, null,false); } public BaseNode createPropertyNode(BaseNode node, int i, ClassMetadata md, Object baseObject, boolean objectGraph) { return new PropertyNode(this, node,i,md,baseObject,objectGraph); } /** * @param node * @param md * @return */ public Object createIdentifierNode(BaseNode parent, ClassMetadata md) { return new IdentifierNode(this, parent, md); } public BaseNode createNode(BaseNode parent, final Class clazz) { ClassMetadata metadata = getMetaData(clazz); if(metadata!=null) { return createClassNode(parent, clazz); } return new BaseNode(this, parent) { public String getHQL() { return null; } public String getName() { return "Unknown -> " + clazz; } protected void checkChildren() { // TODO Auto-generated method stub } }; } public PersistentCollectionNode createPersistentCollectionNode(ClassNode node, String name, ClassMetadata md, PersistentCollectionType type, Object baseObject, boolean objectGraph) { return new PersistentCollectionNode(this, node, name, type, md, getCollectionMetaData(type.getRole()), baseObject, objectGraph); } public Icon getIconForType(Type type) { Icon result = unkownProperty; if(type.isEntityType()) { EntityType et = (EntityType) type; if(!et.isOneToOne()) { result = many2oneIcon; } else { result = one2oneIcon; } } else if (type.isObjectType()) { result = anyIcon; } else if (type.isComponentType()) { result = componentIcon; } else if (type.isPersistentCollectionType()) { PersistentCollectionType pct = (PersistentCollectionType)type; result = one2manyIcon; //could also be values/collecionts? } else { result = propertyIcon; } return result; } static final Icon mappedClassIcon = new ImageIcon(Start.class.getResource("images/mapped_class.gif")); static final Icon unmappedClassIcon = new ImageIcon(Start.class.getResource("images/unmapped_class.gif")); static final Icon propertyIcon = new ImageIcon(Start.class.getResource("images/property.gif")); static final Icon one2manyIcon = new ImageIcon(Start.class.getResource("images/1tonproperty.gif")); static final Icon many2oneIcon = new ImageIcon(Start.class.getResource("images/mto1property.gif")); static final Icon one2oneIcon = new ImageIcon(Start.class.getResource("images/1to1property.gif")); static final Icon anyIcon = new ImageIcon(Start.class.getResource("images/anyproperty.gif")); static final Icon componentIcon = new ImageIcon(Start.class.getResource("images/componentproperty.gif")); static final Icon idpropertyIcon = new ImageIcon(Start.class.getResource("images/idproperty.gif")); static final Icon rootIcon = new ImageIcon(Start.class.getResource("images/types.gif")); static final Icon unkownProperty = new ImageIcon(Start.class.getResource("images/unknowntypeproperty.gif")); public BaseNode createObjectNode(Session session, Object o) { ClassMetadata md = getMetaData(HibernateProxyHelper.getClass(o)); return new ClassNode(this,null,md.getMappedClass().getName(),md,o,true); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?