主题:[讨论]更新数据时的问题
eclipse,oracle9i数据库开发一个struts+hibernate架构的系统
更新数据库中的记录时,没有在原来的基础上改,反而新加了另一个记录?这是什么问题?
ShowproductForm showproductForm = (ShowproductForm) form;
Product updateproduct=showproductForm.getUpdateproduct();
int pro_id=updateproduct.getId();
SessionFactory sf=new Configuration().configure().buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
String sqlQuery="select u from Product u where u.id="+"'"+pro_id+"'";
Query lQuery=session.createQuery(sqlQuery);
ArrayList userList=(ArrayList)lQuery.list();
Product product=(Product)userList.get(0);
product=updateproduct;
session.save(product);
tx.commit();
session.close();
return mapping.findForward("update_product_suc");
public class ShowproductForm extends ActionForm {
/*
* Generated Methods
*/
private static final long serialVersionUID=1L;
private String hid;
private Product viewproduct;
[color=FF0000]private Product updateproduct=new Product(); //因为new吗?可是没有new,就报错了!
public String getHid()
{
return hid;
}
public void setHid(String hid )
{
this.hid=hid;
}
public Product getViewproduct()
{
return viewproduct;
}
public void setViewproduct(Product viewproduct )
{
this.viewproduct=viewproduct;
}
public Product getUpdateproduct()
{
return updateproduct;
}
public void setUpdateproduct(Product updateproduct )
{
this.updateproduct=updateproduct;
}
private ArrayList userlist;
public ArrayList getUserlist()
{
return userlist;
}
public void setUserlist(ArrayList userlist)
{
this.userlist=userlist;
}
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
private String describe;
/** pro_id property */
private int id;
/** price property */
private String price;
/** life property */
private String life;
/** pro_date property */
private String pro_date;
/** pro_name property */
private String pro_name;
/** quantity property */
private String quantity;
/*
* Generated Methods
*/
private String error;
public String getError(){
return error;
}
public void setErrorMessage(String error){
this.error=error;
}
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}
/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
/**
* Returns the descrip.
* @return String
*/
public String getDescribe() {
return describe;
}
/**
* Set the descrip.
* @param descrip The descrip to set
*/
public void setDescrip(String describe) {
this.describe = describe;
}
/**
* Returns the pro_id.
* @return String
*/
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
/**
* Returns the price.
* @return String
*/
public String getPrice() {
return price;
}
/**
* Set the price.
* @param price The price to set
*/
public void setPrice(String price) {
this.price = price;
}
/**
* Returns the life.
* @return String
*/
public String getLife() {
return life;
}
/**
* Set the life.
* @param life The life to set
*/
public void setLife(String life) {
this.life = life;
}
/**
* Returns the pro_date.
* @return String
*/
public String getPro_date() {
return pro_date;
}
/**
* Set the pro_date.
* @param pro_date The pro_date to set
*/
public void setPro_date(String pro_date) {
this.pro_date = pro_date;
}
/**
* Returns the pro_name.
* @return String
*/
public String getPro_name() {
return pro_name;
}
/**
* Set the pro_name.
* @param pro_name The pro_name to set
*/
public void setPro_name(String pro_name) {
this.pro_name = pro_name;
}
/**
* Returns the quantity.
* @return String
*/
public String getQuantity() {
return quantity;
}
/**
* Set the quantity.
* @param quantity The quantity to set
*/
public void setQuantity(String quantity) {
this.quantity = quantity;
}
}[em10][em10][em10]
更新数据库中的记录时,没有在原来的基础上改,反而新加了另一个记录?这是什么问题?
ShowproductForm showproductForm = (ShowproductForm) form;
Product updateproduct=showproductForm.getUpdateproduct();
int pro_id=updateproduct.getId();
SessionFactory sf=new Configuration().configure().buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
String sqlQuery="select u from Product u where u.id="+"'"+pro_id+"'";
Query lQuery=session.createQuery(sqlQuery);
ArrayList userList=(ArrayList)lQuery.list();
Product product=(Product)userList.get(0);
product=updateproduct;
session.save(product);
tx.commit();
session.close();
return mapping.findForward("update_product_suc");
public class ShowproductForm extends ActionForm {
/*
* Generated Methods
*/
private static final long serialVersionUID=1L;
private String hid;
private Product viewproduct;
[color=FF0000]private Product updateproduct=new Product(); //因为new吗?可是没有new,就报错了!
public String getHid()
{
return hid;
}
public void setHid(String hid )
{
this.hid=hid;
}
public Product getViewproduct()
{
return viewproduct;
}
public void setViewproduct(Product viewproduct )
{
this.viewproduct=viewproduct;
}
public Product getUpdateproduct()
{
return updateproduct;
}
public void setUpdateproduct(Product updateproduct )
{
this.updateproduct=updateproduct;
}
private ArrayList userlist;
public ArrayList getUserlist()
{
return userlist;
}
public void setUserlist(ArrayList userlist)
{
this.userlist=userlist;
}
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
private String describe;
/** pro_id property */
private int id;
/** price property */
private String price;
/** life property */
private String life;
/** pro_date property */
private String pro_date;
/** pro_name property */
private String pro_name;
/** quantity property */
private String quantity;
/*
* Generated Methods
*/
private String error;
public String getError(){
return error;
}
public void setErrorMessage(String error){
this.error=error;
}
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}
/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
/**
* Returns the descrip.
* @return String
*/
public String getDescribe() {
return describe;
}
/**
* Set the descrip.
* @param descrip The descrip to set
*/
public void setDescrip(String describe) {
this.describe = describe;
}
/**
* Returns the pro_id.
* @return String
*/
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
/**
* Returns the price.
* @return String
*/
public String getPrice() {
return price;
}
/**
* Set the price.
* @param price The price to set
*/
public void setPrice(String price) {
this.price = price;
}
/**
* Returns the life.
* @return String
*/
public String getLife() {
return life;
}
/**
* Set the life.
* @param life The life to set
*/
public void setLife(String life) {
this.life = life;
}
/**
* Returns the pro_date.
* @return String
*/
public String getPro_date() {
return pro_date;
}
/**
* Set the pro_date.
* @param pro_date The pro_date to set
*/
public void setPro_date(String pro_date) {
this.pro_date = pro_date;
}
/**
* Returns the pro_name.
* @return String
*/
public String getPro_name() {
return pro_name;
}
/**
* Set the pro_name.
* @param pro_name The pro_name to set
*/
public void setPro_name(String pro_name) {
this.pro_name = pro_name;
}
/**
* Returns the quantity.
* @return String
*/
public String getQuantity() {
return quantity;
}
/**
* Set the quantity.
* @param quantity The quantity to set
*/
public void setQuantity(String quantity) {
this.quantity = quantity;
}
}[em10][em10][em10]