shaersa 2008-06-24 11:39
浏览 308
已采纳

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]
问题已经解决了,谢谢大家了 分给谁呢?
就给第一个帮我的吧 谢谢大家帮忙了

  • 写回答

13条回答 默认 最新

  • yupengVSyanjie 2008-06-25 10:01
    关注

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

    1. 可能是你在设置外键的时候出问题。
    2. 还有就是你要把前一次生成的东西全部清掉,如hibernate.cfg.xml文件里面也有,
    3. 建议,一般自动生成的适当的修改一下,不要完全依赖工具。
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(12条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗