问题描述
我hibernate已经生成了DAO 后续又加给数据库加入新表再次要生成DAO及POJO需要注意什么????我自己可能生成出了问题 所以程序报错了java.lang.ClassCastException: com.po.Users这是类型转换异常我的全部财产~ 谢谢大家帮帮忙了问题补充:我用的是SSH框架。 就是Hibernate反向生成DAO这个肯定是我第二次生成DAO时错误了 可能是操作失误 还是第2次生成DAO要注意什么请大家指点还有1楼的地址无效了~问题补充:userspackage com.po;/** * Users generated by MyEclipse Persistence Tools */public class Users implements java.io.Serializable {// Fieldsprivate UsersId id;// Constructors/** default constructor */public Users() {}/** full constructor */public Users(UsersId id) {this.id = id;}// Property accessorspublic UsersId getId() {return this.id;}public void setId(UsersId id) {this.id = id;}}UsersDAOpackage 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 constantsprotected 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");}}UsersIdpackage com.po;/** * UsersId generated by MyEclipse Persistence Tools */public class UsersId implements java.io.Serializable {// Fieldsprivate 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 accessorspublic 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"><!-- Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping> <class name="com.po.Users" table="users" schema="dbo" catalog="usernews"> <composite-id name="id" class="com.po.UsersId"> <key-property name="username" type="java.lang.String"> <column name="username" length="20" /> </key-property> <key-property name="userpass" type="java.lang.String"> <column name="userpass" length="20" /> </key-property> </composite-id> </class></hibernate-mapping>都是反向生成的怎么可能错!~问题补充:问题已经解决了,谢谢大家了 分给谁呢?就给第一个帮我的吧 谢谢大家帮忙了
解决方案
我也碰过一些这方面的错误 1. 可能是你在设置外键的时候出问题。 2. 还有就是你要把前一次生成的东西全部清掉,如hibernate.cfg.xml文件里面也有, 3. 建议,一般自动生成的适当的修改一下,不要完全依赖工具。
解决方案二:
能不能把所有关于你的关联文件及类文件共享啊?你的这部分代码我觉得不能说明问题的所在
解决方案三:
你这个问题是一个常见的问题,你可以有几种解决方案:第一:就是把这个类文件和xml相对的文件全部删除后在重新生成。第二:就是在原有的类中添加你又新添的字段的属性,和方法,在xml对应的文件中修改一个就OK,第二种方法可能要求的东西比较多,如:你应该熟悉hibernate的这种机制就好做了。第一种方法要求的不太多,直接重新做一次就OK。
解决方案四:
用myeclipse反向工程就可以了
解决方案五:
用的MyEclipse在新表反向工程生成。当将工程移到别处时,将catalog名字改,
解决方案六:
看了一下lz的代码,Dao 没有实现一个接口,SSH 中 Spring 整合 Hibernate 需要实现一个接口吧...看看 夏昕的 <<Spring开发指南>> 中 Hiberante in Spring
解决方案七:
如果是用myeclisp工具再次自动生成要注意要把相关联的表要一起选择,否则两表之间的关系就不能自动生成,只能自动生成单表相关代码最好再次生成之前删除干净 DAO PO和相关配置文件
解决方案八:
建议手动更改,把源码贴出来,大家也好帮你找到原因
解决方案九:
把以前的删除干净 dao po 还有那个配置文件都删掉
解决方案十:
1、hibernate生成工具在生成过程中是可以有选择进行表的代码生成的,如果确定改变不大,可以不选择已生成过的表;2、生成过程中如果有对已有对象的少量变更,可以手动修改;3、原表对新表的关链关系可以手动增加。
解决方案十一:
你是用Myeclipse的生成工具吗?
解决方案十二:
对新表进行反向工程就可以了
解决方案十三:
生成DAO?怎么生成的?建议你试用我的DAO框架。http://jasongreen.iteye.com/blog/203478你给的信息太少了,没法帮你分析阿