struts+hibernate中数据库修改操作的问题 transientInstance是什么?对象吗?的确是HQL错误 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 transientInstance是从form表单传过来的,hql哪的语法错误啊,应该怎么写啊,我看书上是这么写啊 是HQL语句,Card是hibernate从数据库映射出来的 hql 写错了。应该是:from Card card where card.id='"+transientInstance.getId()+"' 已经改过了,还是不好用啊UpdateAction.java public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { CardForm cardForm = (CardForm) form;// TODO Auto-generated method stub CardDAO dao = new CardDAO(); Card card = new Card(); card.setId(cardForm.getId()); card.setName(cardForm.getName()); card.setCompany(cardForm.getCompany()); card.setAddress(cardForm.getAddress()); card.setTelephone(cardForm.getTelephone()); card.setMobilephone(cardForm.getMobilephone()); card.setHttp(cardForm.getHttp()); card.setEmail(cardForm.getEmail()); dao.update(card); return mapping.findForward("success"); }Dao.java public void update(Card transientInstance) { log.debug("updating Card instance"); try { Session session = HibernateUtil.getSessionFactory() .getCurrentSession(); session.beginTransaction(); Query query = session.createQuery("from Card card where card.id='"+transientInstance.getId()+"'"); Card card = (Card)query.list().get(0); card.setName(transientInstance.getName()); card.setCompany(transientInstance.getCompany()); card.setAddress(transientInstance.getAddress()); card.setTelephone(transientInstance.getTelephone()); card.setMobilephone(transientInstance.getMobilephone()); card.setHttp(transientInstance.getHttp()); card.setEmail(transientInstance.getEmail()); session.update(card); session.getTransaction().commit(); log.debug("update successful"); } catch (RuntimeException re) { log.error("update failed", re); throw re; } }tomcat报错:org.hibernate.exception.SQLGrammarException: could not execute query org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67) org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) org.hibernate.loader.Loader.doList(Loader.java:2160) org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2041) org.hibernate.loader.Loader.list(Loader.java:2036) org.hibernate.hql.classic.QueryTranslatorImpl.list(QueryTranslatorImpl.java:888) org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172) org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121) org.hibernate.impl.QueryImpl.list(QueryImpl.java:79) com.dao.CardDAO.update(CardDAO.java:55) com.action.UpdateAction.execute(UpdateAction.java:59) org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484) org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274) org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482) org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525) javax.servlet.http.HttpServlet.service(HttpServlet.java:710) javax.servlet.http.HttpServlet.service(HttpServlet.java:803)怎么回事啊???? Query query = session.createQuery("from Card as card where card.id=" + transientInstance.getId()); 改了还是一样的报错啊,还有哪不对吗?我应该在UpdateAction.java 里先new一个对象,把表单的数据存一下,然后在传到dao里update,按照表单的id把数据库对应的查询出来,再进行替换处理,思路应该没错吧,不知具体的操作还有哪错误啊 把你的hibernate的数据库映射文件贴出来看看。 package com.vo;/** * CardId generated by MyEclipse - Hibernate Tools */public class Card implements java.io.Serializable{ // Fields private Long id; private String name; private String company; private String address; private Long telephone; private Long mobilephone; private String http; private String email; // Constructors /** default constructor */ public Card() { } /** full constructor */ public Card(Long id, String name, String company, String address, Long telephone, Long mobilephone, String http, String email) { this.id = id; this.name = name; this.company = company; this.address = address; this.telephone = telephone; this.mobilephone = mobilephone; this.http = http; this.email = email; } // Property accessors public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getCompany() { return this.company; } public void setCompany(String company) { this.company = company; } public String getAddress() { return this.address; } public void setAddress(String address) { this.address = address; } public Long getTelephone() { return this.telephone; } public void setTelephone(Long telephone) { this.telephone = telephone; } public Long getMobilephone() { return this.mobilephone; } public void setMobilephone(Long mobilephone) { this.mobilephone = mobilephone; } public String getHttp() { return this.http; } public void setHttp(String http) { this.http = http; } public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } public int hashCode() { int result = 17; result = 37 * result + (getId() == null ? 0 : this.getId().hashCode()); result = 37 * result + (getName() == null ? 0 : this.getName().hashCode()); result = 37 * result + (getCompany() == null ? 0 : this.getCompany().hashCode()); result = 37 * result + (getAddress() == null ? 0 : this.getAddress().hashCode()); result = 37 * result + (getTelephone() == null ? 0 : this.getTelephone().hashCode()); result = 37 * result + (getMobilephone() == null ? 0 : this.getMobilephone() .hashCode()); result = 37 * result + (getHttp() == null ? 0 : this.getHttp().hashCode()); result = 37 * result + (getEmail() == null ? 0 : this.getEmail().hashCode()); return result; }} <?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 - Hibernate Tools--><hibernate-mapping> <class name="com.vo.Card" table="CARD"> <id name="id" column="ID" type="java.lang.Long"> <generator class="sequence"> <param name="sequence">SEQ_CARD</param> </generator> </id> <property name="name" type="java.lang.String"> <column name="NAME" length="20" /> </property> <property name="company" type="java.lang.String"> <column name="COMPANY" length="100" /> </property> <property name="address" type="java.lang.String"> <column name="ADDRESS" length="100" /> </property> <property name="telephone" type="java.lang.Long"> <column name="TELEPHONE" precision="22" scale="0" /> </property> <property name="mobilephone" type="java.lang.Long"> <column name="MOBILEPHONE" precision="22" scale="0" /> </property> <property name="http" type="java.lang.String"> <column name="HTTP" length="50" /> </property> <property name="email" type="java.lang.String"> <column name="EMAIL" length="50" /> </property> </class></hibernate-mapping> 根据你的错误情况,肯定在:Query query = session.createQuery("from Card where transientInstance.id='"+transientInstance.getId()+"'"); 这句就出错了,建议你断点调试跟踪下,可能是:Card transientInstance这个传过来的是空值吧... System.out.println( transientInstance.getId());//确认不是nullQuery query = session.createQuery("from Card card where card.id=:id").setLong("id", transientInstance.getId()); 发现传过来的值是null,我页面是通过<input type="text" property="name" value="<bean:write name="card" property="name"/>">传值的,是bean:write不能把值放在value里吗,还是根本就取不到啊 问题解决了,把property换成name就好用了 cookie的用法:测试用户流量,出错看不懂。 struts2的疑问?? hashCode 是什么东西有什么用 数据库数据显示是正常的,但是加了 菜鸟问题。 jb8中写了个绘图的applet(很多参数),jb中正常运行,离开JB运行不了,马上给分!! 赐教………… jive 中Thread是什么意思啊,到底怎样翻译啊,JiveThread表又是拿来干什么的呢, 如何取得将来的日期? 初学, Hibernate, 报错,找不到域namingStrategy 求教,急,请好心人帮忙! 关于新闻列表更新的问题
UpdateAction.java
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
CardForm cardForm = (CardForm) form;// TODO Auto-generated method stub
CardDAO dao = new CardDAO();
Card card = new Card();
card.setId(cardForm.getId());
card.setName(cardForm.getName());
card.setCompany(cardForm.getCompany());
card.setAddress(cardForm.getAddress());
card.setTelephone(cardForm.getTelephone());
card.setMobilephone(cardForm.getMobilephone());
card.setHttp(cardForm.getHttp());
card.setEmail(cardForm.getEmail());
dao.update(card);
return mapping.findForward("success");
}
Dao.java
public void update(Card transientInstance)
{
log.debug("updating Card instance");
try
{
Session session = HibernateUtil.getSessionFactory()
.getCurrentSession();
session.beginTransaction();
Query query = session.createQuery("from Card card where card.id='"+transientInstance.getId()+"'");
Card card = (Card)query.list().get(0);
card.setName(transientInstance.getName());
card.setCompany(transientInstance.getCompany());
card.setAddress(transientInstance.getAddress());
card.setTelephone(transientInstance.getTelephone());
card.setMobilephone(transientInstance.getMobilephone());
card.setHttp(transientInstance.getHttp());
card.setEmail(transientInstance.getEmail());
session.update(card);
session.getTransaction().commit(); log.debug("update successful");
} catch (RuntimeException re)
{
log.error("update failed", re);
throw re;
}
}
tomcat报错:
org.hibernate.exception.SQLGrammarException: could not execute query
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
org.hibernate.loader.Loader.doList(Loader.java:2160)
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2041)
org.hibernate.loader.Loader.list(Loader.java:2036)
org.hibernate.hql.classic.QueryTranslatorImpl.list(QueryTranslatorImpl.java:888)
org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
com.dao.CardDAO.update(CardDAO.java:55)
com.action.UpdateAction.execute(UpdateAction.java:59)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
怎么回事啊????
我应该在UpdateAction.java 里先new一个对象,把表单的数据存一下,然后在传到dao里update,
按照表单的id把数据库对应的查询出来,再进行替换处理,思路应该没错吧,不知具体的操作还有哪错误啊
* CardId generated by MyEclipse - Hibernate Tools
*/public class Card implements java.io.Serializable
{ // Fields private Long id; private String name; private String company; private String address; private Long telephone; private Long mobilephone; private String http; private String email; // Constructors /** default constructor */
public Card()
{
} /** full constructor */
public Card(Long id, String name, String company, String address,
Long telephone, Long mobilephone, String http, String email)
{
this.id = id;
this.name = name;
this.company = company;
this.address = address;
this.telephone = telephone;
this.mobilephone = mobilephone;
this.http = http;
this.email = email;
} // Property accessors public Long getId()
{
return this.id;
} public void setId(Long id)
{
this.id = id;
} public String getName()
{
return this.name;
} public void setName(String name)
{
this.name = name;
} public String getCompany()
{
return this.company;
} public void setCompany(String company)
{
this.company = company;
} public String getAddress()
{
return this.address;
} public void setAddress(String address)
{
this.address = address;
} public Long getTelephone()
{
return this.telephone;
} public void setTelephone(Long telephone)
{
this.telephone = telephone;
} public Long getMobilephone()
{
return this.mobilephone;
} public void setMobilephone(Long mobilephone)
{
this.mobilephone = mobilephone;
} public String getHttp()
{
return this.http;
} public void setHttp(String http)
{
this.http = http;
} public String getEmail()
{
return this.email;
} public void setEmail(String email)
{
this.email = email;
} public int hashCode()
{
int result = 17; result = 37 * result + (getId() == null ? 0 : this.getId().hashCode());
result = 37 * result
+ (getName() == null ? 0 : this.getName().hashCode());
result = 37 * result
+ (getCompany() == null ? 0 : this.getCompany().hashCode());
result = 37 * result
+ (getAddress() == null ? 0 : this.getAddress().hashCode());
result = 37 * result
+ (getTelephone() == null ? 0 : this.getTelephone().hashCode());
result = 37
* result
+ (getMobilephone() == null ? 0 : this.getMobilephone()
.hashCode());
result = 37 * result
+ (getHttp() == null ? 0 : this.getHttp().hashCode());
result = 37 * result
+ (getEmail() == null ? 0 : this.getEmail().hashCode());
return result;
}}
<!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 - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.vo.Card" table="CARD">
<id name="id" column="ID" type="java.lang.Long">
<generator class="sequence">
<param name="sequence">SEQ_CARD</param>
</generator>
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="20" />
</property>
<property name="company" type="java.lang.String">
<column name="COMPANY" length="100" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" length="100" />
</property>
<property name="telephone" type="java.lang.Long">
<column name="TELEPHONE" precision="22" scale="0" />
</property>
<property name="mobilephone" type="java.lang.Long">
<column name="MOBILEPHONE" precision="22" scale="0" />
</property>
<property name="http" type="java.lang.String">
<column name="HTTP" length="50" />
</property>
<property name="email" type="java.lang.String">
<column name="EMAIL" length="50" />
</property>
</class>
</hibernate-mapping>
Query query = session.createQuery("from Card where transientInstance.id='"+transientInstance.getId()+"'");
这句就出错了,建议你断点调试跟踪下,可能是:Card transientInstance
这个传过来的是空值吧...
Query query = session.createQuery("from Card card where card.id=:id").setLong("id", transientInstance.getId());
bean:write不能把值放在value里吗,还是根本就取不到啊