论坛首页 Java版 Hibernate

4层继承,导致严重性能问题

浏览 1959 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2005-05-24
[code:1] /** * 所有的 po 对象都需要从这个类派生. * @author liting * */ public abstract class BaseEntity extends BaseObject{ //对象主键id private String oid; //对象更新时间 private Timestamp updateTime; public Timestamp getUpdateTime() { return updateTime; } public void setUpdateTime(Timestamp updateTime) { this.updateTime = updateTime; } public String getOid() { return oid; } public void setOid(String oid) { this.oid = oid; } public boolean equals(Object o) { if ( o == this ) return true; if ( o == null || oid == null) return false; if ( o instanceof String == false ){ return false; } BaseEntity obj = (BaseEntity)o; return oid.equals(obj.oid); } public int hashCode() { if ( oid == null ) return super.hashCode(); else return oid.hashCode(); } } [/code:1] [code:1] public class Node extends BaseEntity { //节点名称,构造树时,通常会用来作为节点标题. protected String nodeName; //是否是虚拟节点 protected boolean virtual = false; //节点id protected String oid; //实体对象 protected Object nodeObject; //节点编码 protected Object nodeCode; //节点类别 protected String nodeType; //实体id protected String entityId; //儿子节点items protected List children = new ArrayList(); //父亲item protected Node parent; //构造块 { setOid( KeyGenerator.newGUID() ); } public Node(){ } /** * 因为实体节点是由hibernate负责构造的(用默认构造函数),而只有虚拟节点才需要手动构造 * 所以,这个构造函数只会被虚拟节点使用,因此 直接将 virtual设置为true; * * @param pNodeName * @param pNodeType */ public Node(String pNodeName, String pNodeType){ this.virtual = true; this.nodeName = pNodeName; this.nodeType = pNodeType; } public void setParent(Node pParent) { if (this.parent != null) {//更换父亲节点. this.parent.detachNode(this);//从以前父亲节点中detach掉自己. } this.parent = pParent; pParent.addChild(this); //如果是虚拟节点,则进行特殊处理 if ( this.virtual ){ this.entityId = pParent.getEntityId(); this.nodeCode= pParent.getNodeCode(); /** * TODO: 文字信息应该从资源文件中获取 */ this.nodeObject = "虚拟节点"; } } public Node getParent() { return this.parent; } public void addChild(Node pChild) { children.add(pChild); } //从儿子节点中 detach 一个节点 public void detachNode(Node pNode) { this.children.remove(pNode); } public void addChildren(List pChildren) { children.addAll(pChildren); } public void setChildren(List pChildren) { this.children = pChildren; } public void setNodeObject(Object nodeObject) { this.nodeObject = nodeObject; } public void setOid(String oid) { this.oid = oid; } public List getChildren() { return children; } public Object getNodeObject() { return nodeObject; } public String getOid() { return oid; } public void setNodeCode(String nodeCode) { this.nodeCode = nodeCode; } public Object getNodeCode() { return nodeCode; } public void setNodeCode(Object nodeCode) { this.nodeCode = nodeCode; } public String getNodeType() { return nodeType; } public void setNodeType(String nodeType) { this.nodeType = nodeType; } public String toString(){ if ( nodeName == null ){ return "default title"; }else{ return nodeName; } } public String getEntityId() { return entityId; } public void setEntityId(String entityId) { this.entityId = entityId; } public String getNodeName() { return nodeName; } public void setNodeName(String nodeName) { this.nodeName = nodeName; } public boolean isVirtual() { return virtual; } } [/code:1] [code:1] public abstract class LoadXtreeNode extends Node { protected boolean expand; protected String text; protected String action; protected String icon; protected String XMLEncoding; protected String XMLVersion; { action = ""; icon = ""; expand = true; XMLEncoding = "utf-8"; XMLVersion = "1.0"; } public LoadXtreeNode(String pNodeName, String pNodeType){ super(pNodeName, pNodeType); } public LoadXtreeNode(){ super(); } public void setAction(String action) { this.action = action; } public void setIcon(String icon) { this.icon = icon; } public void setText(String text) { this.text = text; } public void setXMLEncoding(String encoding) { XMLEncoding = encoding; } public void setXMLVersion(String version) { XMLVersion = version; } public abstract boolean isExpand(); public abstract String getSrc() ; public String getText() { if (text == null ) return this.toString(); if ( text.trim().length() == 0 ) return this.toString(); return text; } public String getAction() { return action; } public String getIcon() { return icon; } public String getXMLEncoding(){ return XMLEncoding; } public String getXMLVersion(){ return XMLVersion; } protected String getXMLHeader(){ return "<?xml version=\"" + this.getXMLVersion() + "\" encoding=\""+ this.getXMLEncoding()+"\" ?>\r\n" + "<tree>\r\n"; } protected String getXMLFooter(){ return "</tree>\r\n"; } public String getXMLs(){ StringBuffer sb = new StringBuffer(200); sb.append(getXMLHeader()); for(int i=0; i<this.children.size(); i++){ Object obj = this.children.get(i); if ( obj instanceof LoadXtreeNode == false){ continue; } LoadXtreeNode node = (LoadXtreeNode)obj; sb.append(node.getXML()); } sb.append(getXMLFooter()); return sb.toString(); } public String getXML(){ String result = "<tree text=\"${text}\" action=\"${action}\" icon=\"${icon}\" " ; if ( this.isExpand() ){ result = result + " src=\"${src}\"" ; } result = result + "/>\r\n"; Template strTemplate = new Template(); strTemplate.putTemplateVar("text", this.getText()); strTemplate.putTemplateVar("action", this.getAction()); strTemplate.putTemplateVar("icon", this.getIcon()); strTemplate.putTemplateVar("src", this.getSrc()); return strTemplate.merge(result); } } [/code:1] [code:1] /** * 用来保存机构树上所有节点,包含虚拟节点. * @author liting * */ public class OrgNode extends LoadXtreeNode { private static final String ORG = "/ebase/samples/common/images/org.gif"; private static final String PERSON = "/ebase/samples/common/images/person.gif"; public OrgNode(String pNodeName, String pNodeType){ super(pNodeName, pNodeType); } public OrgNode(){ super(); } public boolean isExpand(){ if ("org".equals(nodeType)) return true; else return false; } public String getIcon(){ if ("org".equals(nodeType)){ return ORG; }else if("person".equals(nodeType)){ return PERSON; }else{ return ORG; } } public String getSrc(){ String src = "/ebase/samples/treeAction.do?actionType=loadNode&nodeCode=${nodeCode}&nodeType=${nodeType}"; Template strTemplate = new Template(); strTemplate.putTemplateVar("nodeCode", nodeCode); strTemplate.putTemplateVar("nodeType", nodeType); return strTemplate.merge(src); } public static void main(String[] args){ OrgNode node = new OrgNode(); Org aOrg = new Org(); aOrg.setOrgName("dd"); node.setNodeObject(aOrg); node.setNodeCode("1.2"); node.setNodeType("org"); node.addChild(node); System.out.println(node.getXMLs()); } } [/code:1] [/code]
   
时间:2005-05-24
只有OrgNode 有对应的数据表,配置文件
[code:1]
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="net.jcreate.samples.tree.domain.OrgNode" table="ebase_etree_orgNode" >
<id name = "oid"
column="oid"
unsaved-value = "null">
<generator class="uuid.hex"/>
</id>

<property name="nodeCode" type="string"/>
<property name="nodeName"/>
<property name="nodeType" insert="false" update="false"/>
<property name="entityId" insert="false" update="false"/>
<any name="nodeObject" meta-type="string" id-type="string" cascade="none" >
<meta-value value="org" class="net.jcreate.samples.tree.domain.Org" />
<meta-value value="person" class="net.jcreate.samples.tree.domain.Person" />
<column name="nodeType" />
<column name="entityId"/>
</any>

</class>
</hibernate-mapping>

[/code:1]
   
0 请登录后投票
时间:2005-05-24
表结构
[code:1]
create table ebase_etree_orgNode (
oid TId not null,
nodeCode TInfo100 null,
nodeType TType null,
entityId TId null,
nodeName TName null,
constraint PK_EBASE_ETREE_ORGNODE primary key (oid)
)
go
[/code:1]
   
0 请登录后投票
时间:2005-05-24
如果OrgNode 不派生任何类的话,速度很正常,获取220个节点,不到200ms,但是,如果派生LoadXtreeNode ,速度慢的吓人,3750ms。这是为什么?有人碰到这种问题吗?谢谢!
   
0 请登录后投票
时间:2005-05-24
不好意思,不关hibernate的事情,是我在Node类的构造块加个生成主键的东西:)
{
setOid( KeyGenerator.newGUID() );
}
   
0 请登录后投票
论坛首页 Java版 Hibernate

跳转论坛:
JavaEye推荐