hibernate再次生成问题

[b]我hibernate已经生成了DAO 后续又加给数据库加入新表再次要生成DAO及POJO需要注意什么????[/b]

我自己可能生成出了问题 所以程序报错了
java.lang.ClassCastException: com.po.Users
这是类型转换异常

我的全部财产~ 谢谢大家帮帮忙了
[b]问题补充:[/b]
我用的是SSH框架。 就是Hibernate反向生成DAO

这个肯定是我第二次生成DAO时错误了 可能是操作失误 还是第2次生成DAO要注意什么
请大家指点

还有1楼的地址无效了~
[b]问题补充:[/b]
users

package com.po;

/**

  • Users generated by MyEclipse Persistence Tools */

public class Users implements java.io.Serializable {

// Fields

private UsersId id;

// Constructors

/** default constructor */
public Users() {
}

/** full constructor */
public Users(UsersId id) {
    this.id = id;
}

// Property accessors

public UsersId getId() {
    return this.id;
}

public void setId(UsersId id) {
    this.id = id;
}

}

UsersDAO

package com.po;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**

  • Data access object (DAO) for domain model class Users.
  • @see com.po.Users
  • @author MyEclipse Persistence Tools */

public class UsersDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(UsersDAO.class);

// property constants

protected void initDao() {
    // do nothing
}

public void save(Users transientInstance) {
    log.debug("saving Users instance");
    try {
        getHibernateTemplate().save(transientInstance);
        log.debug("save successful");
    } catch (RuntimeException re) {
        log.error("save failed", re);
        throw re;
    }
}

public void delete(Users persistentInstance) {
    log.debug("deleting Users instance");
    try {
        getHibernateTemplate().delete(persistentInstance);
        log.debug("delete successful");
    } catch (RuntimeException re) {
        log.error("delete failed", re);
        throw re;
    }
}

public Users findById(com.po.UsersId id) {
    log.debug("getting Users instance with id: " + id);
    try {
        Users instance = (Users) getHibernateTemplate().get("com.po.Users",
                id);
        return instance;
    } catch (RuntimeException re) {
        log.error("get failed", re);
        throw re;
    }
}

public List findByExample(Users instance) {
    log.debug("finding Users instance by example");
    try {
        List results = getHibernateTemplate().findByExample(instance);
        log.debug("find by example successful, result size: "
                + results.size());
        return results;
    } catch (RuntimeException re) {
        log.error("find by example failed", re);
        throw re;
    }
}

public List findByProperty(String propertyName, Object value) {
    log.debug("finding Users instance with property: " + propertyName
            + ", value: " + value);
    try {
        String queryString = "from Users as model where model."
                + propertyName + "= ?";
        return getHibernateTemplate().find(queryString, value);
    } catch (RuntimeException re) {
        log.error("find by property name failed", re);
        throw re;
    }
}

public List findAll() {
    log.debug("finding all Users instances");
    try {
        String queryString = "from Users";
        return getHibernateTemplate().find(queryString);
    } catch (RuntimeException re) {
        log.error("find all failed", re);
        throw re;
    }
}

public Users merge(Users detachedInstance) {
    log.debug("merging Users instance");
    try {
        Users result = (Users) getHibernateTemplate().merge(
                detachedInstance);
        log.debug("merge successful");
        return result;
    } catch (RuntimeException re) {
        log.error("merge failed", re);
        throw re;
    }
}

public void attachDirty(Users instance) {
    log.debug("attaching dirty Users instance");
    try {
        getHibernateTemplate().saveOrUpdate(instance);
        log.debug("attach successful");
    } catch (RuntimeException re) {
        log.error("attach failed", re);
        throw re;
    }
}

public void attachClean(Users instance) {
    log.debug("attaching clean Users instance");
    try {
        getHibernateTemplate().lock(instance, LockMode.NONE);
        log.debug("attach successful");
    } catch (RuntimeException re) {
        log.error("attach failed", re);
        throw re;
    }
}

public static UsersDAO getFromApplicationContext(ApplicationContext ctx) {
    return (UsersDAO) ctx.getBean("UsersDAO");
}

}

UsersId

package com.po;

/**

  • UsersId generated by MyEclipse Persistence Tools */

public class UsersId implements java.io.Serializable {

// Fields

private String username;

private String userpass;

// Constructors

/** default constructor */
public UsersId() {
}

/** full constructor */
public UsersId(String username, String userpass) {
    this.username = username;
    this.userpass = userpass;
}

// Property accessors

public String getUsername() {
    return this.username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getUserpass() {
    return this.userpass;
}

public void setUserpass(String userpass) {
    this.userpass = userpass;
}

public boolean equals(Object other) {
    if ((this == other))
        return true;
    if ((other == null))
        return false;
    if (!(other instanceof UsersId))
        return false;
    UsersId castOther = (UsersId) other;

    return ((this.getUsername() == castOther.getUsername()) || (this
            .getUsername() != null
            && castOther.getUsername() != null && this.getUsername()
            .equals(castOther.getUsername())))
            && ((this.getUserpass() == castOther.getUserpass()) || (this
                    .getUserpass() != null
                    && castOther.getUserpass() != null && this
                    .getUserpass().equals(castOther.getUserpass())));
}

public int hashCode() {
    int result = 17;
    result = 37 * result
            + (getUsername() == null ? 0 : this.getUsername().hashCode());
    result = 37 * result
            + (getUserpass() == null ? 0 : this.getUserpass().hashCode());
    return result;
}

}

Users.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">












都是反向生成的怎么可能错!~
[b]问题补充:[/b]
问题已经解决了,谢谢大家了 分给谁呢?
就给第一个帮我的吧 谢谢大家帮忙了

我也碰过一些这方面的错误

  1. 可能是你在设置外键的时候出问题。
  2. 还有就是你要把前一次生成的东西全部清掉,如hibernate.cfg.xml文件里面也有,
  3. 建议,一般自动生成的适当的修改一下,不要完全依赖工具。

生成DAO?怎么生成的?
建议你试用我的DAO框架。
http://jasongreen.iteye.com/blog/203478

你给的信息太少了,没法帮你分析阿

对新表进行反向工程就可以了

你是用Myeclipse的生成工具吗?

1、hibernate生成工具在生成过程中是可以有选择进行表的代码生成的,如果确定改变不大,可以不选择已生成过的表;
2、生成过程中如果有对已有对象的少量变更,可以手动修改;
3、原表对新表的关链关系可以手动增加。

把以前的删除干净 dao po 还有那个配置文件都删掉

建议手动更改,把源码贴出来,大家也好帮你找到原因

如果是用myeclisp工具再次自动生成要注意要把相关联的表要一起选择,否则两表之间的关系就不能自动生成,只能自动生成单表相关代码
最好再次生成之前删除干净 DAO PO和相关配置文件

看了一下lz的代码,Dao 没有实现一个接口,

SSH 中 Spring 整合 Hibernate 需要实现一个接口吧...

看看 夏昕的 <> 中 Hiberante in Spring

用的MyEclipse在新表反向工程生成。当将工程移到别处时,将catalog名字改,

用myeclipse反向工程就可以了

你这个问题是一个常见的问题,你可以有几种解决方案:
第一:就是把这个类文件和xml相对的文件全部删除后在重新生成。
第二:就是在原有的类中添加你又新添的字段的属性,和方法,在xml对应的文件中修改一个就OK,
第二种方法可能要求的东西比较多,如:你应该熟悉hibernate的这种机制就好做了。
第一种方法要求的不太多,直接重新做一次就OK。

能不能把所有关于你的关联文件及类文件共享啊?
你的这部分代码我觉得不能说明问题的所在