下面是我的实体类:Company.java, Machine.java
Company.java
package com.shinco.domain;import java.util.HashSet;
import java.util.Set;/**
 * 公司类
 * 
 * @author <a href="[email protected]">yakoo5</a>
 */
public class Company implements java.io.Serializable {
private static final long serialVersionUID = 7614370043539561049L;

// 属性

private Integer id;  // 唯一标示号
private String companyName;  // 公司名称
private Company father; // 所属公司
private Set<Company> children = new HashSet<Company>(0);  // 下属公司
private Set<Machine> machines = new HashSet<Machine>(0);  // 该公司的服务机器 // 默认构造函数
public Company() {
} /**
 * 构造函数
 * 
 * @param companyName 公司名称
 * @param father 上级公司名称
 */
public Company(String companyName, Company father) {
this.companyName = companyName;
this.setFather(father);
}

/**
 * 添加机器
 * 
 * @param m 机器
 */
public void addMachine(Machine m) {
m.setCompany(this);
this.machines.add(m);
}

/**
 * 添加下属公司
 * 
 * @param child 下属公司
 */
public void addChild(Company child){
child.setFather(this);
this.children.add(child);
} // 属性访问器

public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} public String getCompanyName() {
return this.companyName;
} public void setCompanyName(String companyName) {
this.companyName = companyName;
} public Company getFather() {
return father;
}

public void setFather(Company father) {
this.father = father;
} public void setChildren(Set<Company> children) {
this.children = children;
} public Set<Company> getChildren() {
return children;
} public Set<Machine> getMachines() {
return this.machines;
} public void setMachines(Set<Machine> machines) {
this.machines = machines;
}

// 重写toString()方法
@Override
public String toString(){
String sp = ",";
return this.getClass().getName() + "={" + id + sp + companyName + sp + father.getId() + "}";
}}
Machine.java
package com.shinco.domain;/**
 * 机器类
 * 
 * @author <a href="[email protected]">yakoo5</a>
 */
@SuppressWarnings("serial")
public class Machine implements java.io.Serializable { // 属性

private Integer id; // 机器唯一标示号
private Company company; // 该机器所属的公司
private String serviceNumber; // 服务编号
private String serviceName; // 服务名称
private String username; // 用户名 //默认 构造函数
public Machine() {
} /**
 * 构造函数
 * 
 * @param id 机器号
 * @param company 该机器所属公司
 */
public Machine(Integer id, Company company) {
this.id = id;
this.company = company;
} // 属性访问器

public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} public Company getCompany() {
return this.company;
} public void setCompany(Company company) {
this.company = company;
} public String getServiceName() {
return this.serviceName;
} public void setServiceName(String serviceName) {
this.serviceName = serviceName;
} public String getUsername() {
return this.username;
} public void setUsername(String username) {
this.username = username;
} public void setServiceNumber(String serviceNumber) {
this.serviceNumber = serviceNumber;
} public String getServiceNumber() {
return serviceNumber;
}

// 重写toString()方法
@Override
public String toString(){
String sp = ",";
return this.getClass().getName() + "={" + id + sp + serviceNumber + sp +serviceName + sp + username + sp + company.getId() + "}";
}}类似如下显示结果:可参考如下链接中显示的代码:javascript实现可以自由拖动的树形列表
类似Windows资源管理器的Tree菜单
其实拖拽也是公司要去的一部分,这部分可暂不实现。要求要递归显示Company节点内容,终端节点是Machine对象。小弟Javascript,Ajax初学,有些代码还看不懂,请各位高手指点?不胜感激!

解决方案 »

  1.   

    下面贴出DAO代码:
    CompanyDAO.java
    package com.shinco.dao;import java.util.List;import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.hibernate.LockMode;
    import org.hibernate.Query;
    import org.hibernate.Transaction;
    import org.hibernate.criterion.Example;import com.shinco.domain.Company;
    import com.shinco.util.HibernateSessionFactory;/**
     * CompanyDAO
     * 
     * @author <a href="[email protected]">yakoo5</a>
     */
    public class CompanyDAO extends BaseHibernateDAO {
    private static final Log log = LogFactory.getLog(CompanyDAO.class);
    // 属性常量
    public static final String COMPANY_NAME = "companyName"; /**
     * 保存
     * 
     * @param company
     */
    public void save(Company company) {
    log.debug("saving Company instance"); try {
    Transaction tx = null;
    tx = HibernateSessionFactory.beginTransaction();
    getSession().save(company);
    tx.commit();
    log.debug("save successful");
    } catch (RuntimeException re) {
    log.error("delete failed", re);
    throw re;
    } finally {
    HibernateSessionFactory.closeSession();
    }
    } /**
     * 删除
     * 
     * @param company
     */
    public void delete(Company company) {
    log.debug("deleting Company instance");
    try {
    Transaction tx = null;
    tx = HibernateSessionFactory.beginTransaction();
    getSession().delete(company);
    tx.commit();
    log.debug("delete successful");
    } catch (RuntimeException re) {
    log.error("delete failed", re);
    throw re;
    } finally {
    HibernateSessionFactory.closeSession();
    }
    }

    /**
     * 更新
     * 
     * @param company Company脱管对象
     * @return Company 返回更新后的Company对象
     */
    public Company merge(Company company) {
    log.debug("merging Company instance");
    try {
    Transaction tx = null;
    tx = HibernateSessionFactory.beginTransaction();
    Company result = (Company) getSession().merge(company);
    tx.commit();
    log.debug("merge successful");
    return result;
    } catch (RuntimeException re) {
    log.error("merge failed", re);
    throw re;
    } finally {
    HibernateSessionFactory.closeSession();
    }
    } /**
     * 根据ID查找
     * 
     * @param id Company ID
     * @return Company
     */
    public Company findById(Integer id) {
    log.debug("getting Company instance with id: " + id);
    try {
    Company instance = (Company) getSession().get(
    "com.shinco.domain.Company", id);
    return instance;
    } catch (RuntimeException re) {
    log.error("get failed", re);
    throw re;
    }
    } /**
     * 根据Company实例对象查找
     * 
     * @param instance Company实例
     * @return List<Company>
     */
    @SuppressWarnings("unchecked")
    public List<Company> findByExample(Company instance) {
    log.debug("finding Company instance by example");
    try {
    List results = getSession().createCriteria(
    "com.shinco.domain.Company").add(Example.create(instance))
    .list();
    log.debug("find by example successful, result size: "
    + results.size());
    return results;
    } catch (RuntimeException re) {
    log.error("find by example failed", re);
    throw re;
    }
    } /**
     * 根据Company的某一属性查找
     * 
     * @param propertyName 属性名称
     * @param value 属性值
     * @return List<Company>
     */
    @SuppressWarnings("unchecked")
    public List<Company> findByProperty(String propertyName, Object value) {
    log.debug("finding Company instance with property: " + propertyName
    + ", value: " + value);
    try {
    String queryString = "from Company as model where model."
    + propertyName + "= ?";
    Query queryObject = getSession().createQuery(queryString);
    queryObject.setParameter(0, value);
    return queryObject.list();
    } catch (RuntimeException re) {
    log.error("find by property name failed", re);
    throw re;
    }
    } /**
     * 根据公司名称查找
     * 
     * @param companyName 公司名称
     * @return List
     */
    public List<Company> findByCompanyName(Object companyName) {
    return findByProperty(COMPANY_NAME, companyName);
    } /**
     * 查询所有
     * 
     * @return List<Company>
     */
    @SuppressWarnings("unchecked")
    public List<Company> findAll() {
    log.debug("finding all Company instances");
    try {
    String queryString = "from Company";
    Query queryObject = getSession().createQuery(queryString);
    return queryObject.list();
    } catch (RuntimeException re) {
    log.error("find all failed", re);
    throw re;
    }
    }

    /**
     * 获取最顶层的公司对象
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<Company> findTopLevelCompany(){
    log.debug("finding top lever companys");
    try {
    String queryString = "from Company cp where cp.id = cp.father.id";
    Query query =HibernateSessionFactory.getSession().createQuery(queryString);
    List<Company> results = query.list();
    log.debug("find top lever companys successful, result size: "
    + results.size());
    return results;
    } catch (RuntimeException re) {
    log.error("find top lever companys failed", re);
    throw re;
    }
    } public void attachDirty(Company instance) {
    log.debug("attaching dirty Company instance");
    try {
    Transaction tx = null;
    tx = HibernateSessionFactory.beginTransaction();
    getSession().saveOrUpdate(instance);
    tx.commit();
    log.debug("attach successful");
    } catch (RuntimeException re) {
    log.error("attach failed", re);
    throw re;
    } finally {
    HibernateSessionFactory.closeSession();
    }
    } public void attachClean(Company instance) {
    log.debug("attaching clean Company instance");
    try {
    Transaction tx = null;
    tx = HibernateSessionFactory.beginTransaction();
    getSession().lock(instance, LockMode.NONE);
    log.debug("attach successful");
    tx.commit();
    } catch (RuntimeException re) {
    log.error("attach failed", re);
    throw re;
    } finally {
    HibernateSessionFactory.closeSession();
    }
    }
    }
      

  2.   

    MachineDAO.java
    package com.shinco.dao;import java.util.List;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.hibernate.LockMode;
    import org.hibernate.Query;
    import org.hibernate.Transaction;
    import org.hibernate.criterion.Example;import com.shinco.domain.Machine;
    import com.shinco.util.HibernateSessionFactory;/**
     * MachineDAO
     * 
     * @author <a href="[email protected]">yakoo5</a>
     */
    public class MachineDAO extends BaseHibernateDAO {
    private static final Log log = LogFactory.getLog(MachineDAO.class);
    // 属性常量
    public static final String SERVICE_NUMBER = "serviceNumber";// 服务编号
    public static final String SERVICE_NAME = "serviceName"; // 服务名称
    public static final String USERNAME = "username"; // 用户名 /**
     * 保存
     * @param machine
     */
    public void save(Machine machine) {
    log.debug("saving Machine instance");
    try {
    Transaction tx = null;
    tx = HibernateSessionFactory.beginTransaction();
    getSession().save(machine);
    tx.commit();
    log.debug("save successful");
    } catch (RuntimeException re) {
    log.error("save failed", re);
    throw re;
    } finally {
    HibernateSessionFactory.closeSession();
    }
    } /**
     * 删除
     * 
     * @param machine
     */
    public void delete(Machine machine) {
    log.debug("deleting Machine instance");
    try {
    Transaction tx = null;
    tx = HibernateSessionFactory.beginTransaction();
    getSession().delete(machine);
    tx.commit();
    log.debug("delete successful");
    } catch (RuntimeException re) {
    log.error("delete failed", re);
    throw re;
    } finally {
    HibernateSessionFactory.closeSession();
    }
    }

    /**
     * 更新
     * 
     * @param detachedInstance 脱管Machine对象
     * @return Machine
     */
    public Machine merge(Machine detachedInstance) {
    log.debug("merging Machine instance");
    try {
    Transaction tx = null;
    tx = HibernateSessionFactory.beginTransaction();
    Machine result = (Machine) getSession().merge(detachedInstance);
    tx.commit();
    log.debug("merge successful");
    return result;
    } catch (RuntimeException re) {
    log.error("merge failed", re);
    throw re;
    } finally {
    HibernateSessionFactory.closeSession();
    }
    } /**
     * 根据ID查找
     * 
     * @param id 机器ID
     * @return Machine
     */
    public Machine findById(Integer id) {
    log.debug("getting Machine instance with id: " + id);
    try {
    Machine instance = (Machine) getSession().get(
    "com.shinco.domain.Machine", id);
    return instance;
    } catch (RuntimeException re) {
    log.error("get failed", re);
    throw re;
    }
    } /**
     * 根据Machine实例对象查找
     * 
     * @param instance 实例对象
     * @return List<Machine>
     */
    @SuppressWarnings("unchecked")
    public List<Machine> findByExample(Machine instance) {
    log.debug("finding Machine instance by example");
    try {
    List results = getSession().createCriteria(
    "com.shinco.domain.Machine").add(Example.create(instance))
    .list();
    log.debug("find by example successful, result size: "
    + results.size());
    return results;
    } catch (RuntimeException re) {
    log.error("find by example failed", re);
    throw re;
    }
    } /**
     * 根据Machine的属性查找
     * 
     * @param propertyName 属性名称
     * @param value 属性值
     * @return List<Machine>
     */
    @SuppressWarnings("unchecked")
    public List<Machine> findByProperty(String propertyName, Object value) {
    log.debug("finding Machine instance with property: " + propertyName
    + ", value: " + value);
    try {
    String queryString = "from Machine as model where model."
    + propertyName + "= ?";
    Query queryObject = getSession().createQuery(queryString);
    queryObject.setParameter(0, value);
    return queryObject.list();
    } catch (RuntimeException re) {
    log.error("find by property name failed", re);
    throw re;
    }
    } /**
     * 根据服务名称查找
     * 
     * @param serviceName 服务名称
     * @return List
     */
    public List<Machine> findByServiceName(Object serviceName) {
    return findByProperty(SERVICE_NAME, serviceName);
    } /**
     * 根据用户名查找
     * 
     * @param username 用户名
     * @return List<Machine>
     */
    public List<Machine> findByUsername(Object username) {
    return findByProperty(USERNAME, username);
    } /**
     * 查找所有
     * 
     * @return List<Machine>
     */
    @SuppressWarnings("unchecked")
    public List<Machine> findAll() {
    log.debug("finding all Machine instances");
    try {
    String queryString = "from Machine";
    Query queryObject = getSession().createQuery(queryString);
    return queryObject.list();
    } catch (RuntimeException re) {
    log.error("find all failed", re);
    throw re;
    }
    } public void attachDirty(Machine instance) {
    log.debug("attaching dirty Machine instance");
    try {
    Transaction tx = null;
    tx = HibernateSessionFactory.beginTransaction();
    getSession().saveOrUpdate(instance);
    tx.commit();
    log.debug("attach successful");
    } catch (RuntimeException re) {
    log.error("attach failed", re);
    throw re;
    } finally {
    HibernateSessionFactory.closeSession();
    }
    } public void attachClean(Machine instance) {
    log.debug("attaching clean Machine instance");
    try {
    Transaction tx = null;
    tx = HibernateSessionFactory.beginTransaction();
    getSession().lock(instance, LockMode.NONE);
    tx.commit();
    log.debug("attach successful");
    } catch (RuntimeException re) {
    log.error("attach failed", re);
    throw re;
    } finally {
    HibernateSessionFactory.closeSession();
    }
    }
    }希望高手多多指点,最好能贴出代码,急用!多谢了!
      

  3.   

    我的资源里有个 AJAX 加载树形菜单源码。楼主可以去看看。