再次出现ID传递不了的问题

时间:2008-05-10 10:56:04   来源:论坛整理  作者:  编辑:chinaitzhe
是Hibernate Struts的架构.
一个删除公告的功能就是实现不了,每次都是出现id to load is required for loading错误.
主体类Topic如下.

public class Topic implements java.io.Serializable {
private static final long serialVersionUID = 1L;
// Fields

private Integer id;

private String name;

private String content;

private String pubdate;

// Constructors

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

/** full constructor */
public Topic(String name, String content, String pubdate) {
this.name = name;
this.content = content;
this.pubdate = pubdate;
}

// Property accessors

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

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

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public String getContent() {
return this.content;
}

public void setContent(String content) {
this.content = content;
}

public String getPubdate() {
return this.pubdate;
}

public void setPubdate(String pubdate) {
this.pubdate = pubdate;
}

}


DAO类如下.

package com.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.criterion.Example;


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

// property constants
public static final String NAME = "name";

public static final String CONTENT = "content";

public static final String PUBDATE = "pubdate";

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

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

public Topic findById(java.lang.Integer id) {
log.debug("getting Topic instance with id: " id);
try {
Topic instance = (Topic) getSession().get("com.dao.Topic", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}

public List findByExample(Topic instance) {
log.debug("finding Topic instance by example");
try {
List results = getSession().createCriteria("com.dao.Topic").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;
}
}

public List findByProperty(String propertyName, Object value) {
log.debug("finding Topic instance with property: " propertyName
", value: " value);
try {
String queryString = "from Topic 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;
}
}

public List findByName(Object name) {
return findByProperty(NAME, name);
}

public List findByContent(Object content) {
return findByProperty(CONTENT, content);
}

public List findByPubdate(Object pubdate) {
return findByProperty(PUBDATE, pubdate);
}

public List findAll() {
log.debug("finding all Topic instances");
try {
String queryString = "from Topic";
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}

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

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

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

删除的代码如下.
package com.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.dao.Topic;
import com.dao.TopicDAO;

public class DeleteTopicAction extends Action{


public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
String grade=(String)session.getAttribute("grade");
if(grade=="治理员"){
Integer id=Integer.valueOf(request.getParameter("id"));
TopicDAO topDao = new TopicDAO();
Topic top = new Topic();
top = topDao.findById(id);
topDao.delete(top);
topDao.getSession().beginTransaction().commit();
return (mapping.findForward("deletesucess"));
}
ActionMessages errors=new ActionMessages();
errors.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("delete.error"));
saveErrors(request,errors);
return (mapping.findForward("deletefailed"));
}

}

servlet中打印的删除代码如下
out.print(" <td> <a href=deletetea.do?id=" top.getId() " > <font size='1'>删除(慎用) </font> </a> </td> </tr>");


起初没进行String跟Integer转换.现在做了转换,还是找不到..不知道是为什么了现在.
进请指教
网友回复:
Java code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



topDao.getSession().beginTransaction();


填上试试
网友回复:TopicDAO topDao = new TopicDAO();
topDao.getSession().beginTransaction(); // 如楼上所说,加上看看
Topic top = new Topic();
top = topDao.findById(id);
topDao.delete(top);
topDao.getSession().getTransaction().commit(); // 这里也要改一下
return (mapping.findForward("deletesucess"));
}
网友回复:一样的,还是出现那个错误...
不知道是什么原因...
TopicForm
package com.struts.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class TopicForm extends ActionForm{

private static final long serialVersionUID = 1L;
String name;
String content;
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)
{
return null;
}

public void reset(ActionMapping mapping,HttpServletRequest request)
{

}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


}


我像对人员的治理功能都是可以用的.就是这个治理公告的不能用..
我在想是不是跟公告表的主键有关,人员表的主键是工号,Varchar的,公告表的主键是int,自增,然后在POJO,DAO里头主键是Interger类的,然后现在我就是不知道这样不同类型改怎么解决,
或者是Form里头没有加主键的问题?应该不是,,我添加公告都是可以的.
网友回复:public void delete(Integer id) {
log.debug("deleting Topic instance");
try {
Topic instance = (Topic) getSession().get(Topic.class, id);
getSession().delete(instance );
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
 
这样试试呀


网友回复:delete的方法应该没有问题....
跟我另外个功能唯一的区别就是表的内容不同.
网友回复:你确定已经从数据查出了信息?
Topic top = new Topic();
top = topDao.findById(id);
你先测试下看有没有信息: System.out.println("~~~~~~~~~~"top.getId());

Topic top = topDao.findById(id); 你把上两句合并了.

网友回复:- -打印不出来,米反应.
网友回复:我想问一下 你的id 在数据库设置为主键 了吗?
网友回复:一切都好...哈...错误所在是out.print(" <td> <a href=deletetea.do?id=" top.getId() " > <font size='1'>删除(慎用) </font> </a> </td> </tr>");
应该是deletetop.do的..又是小错误...哎.
关键字:再次出现,ID,传递,不了,问题,

相关文章

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面