我正在尝试使用hibernate和sessions/sessionfactory向数据库添加一行。然而,当我执行以下代码时,它只向数据库中添加了一次值为"0"的值;1,空";。我认为问题出在ProductAdd.java(下面(中,但我不确定。
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import com.product.Product;
import com.utility.HibernateUtility;
@WebServlet("/ProductAdd")
public class ProductAdd extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("inside servlet");
Product product= new Product(request.getParameter("prodId"),request.getParameter("prodName"));
try {
Product p = new Product();
p.setName(product.getName());
p.setId(product.getName());
Session session = HibernateUtility.getSessionFactory().openSession();
org.hibernate.Transaction transaction = null;
try {
transaction = session.beginTransaction();
String sql = "INSERT INTO Product VALUES (:idVal, :nameVal)";
Query query = session.createSQLQuery(sql);
query.setParameter("idVal", product.getId());
query.setParameter("nameVal", product.getName());
query.executeUpdate();
session.getTransaction().commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
System.out.println(("Product is added."));
} catch (Exception e) {
// TODO: handle exception
}
HttpSession session= request.getSession();
session.setAttribute("sesname", request.getParameter("prodId"));
response.sendRedirect("addsuccess.jsp");
}
}
如果需要的话,这是我的产品.java:
package com.product;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Product implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String id;
private String name;
public Product(String string, String string2) {
super();
// TODO Auto-generated constructor stub
}
public Product() {
// TODO Auto-generated constructor stub
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我还有我的Hibernate实用程序类:
package com.utility;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtility {
private static final SessionFactory sessionFactory = createSessionFactory();
public static SessionFactory createSessionFactory() {
try {
return new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Exception ex) {
System.err.println("SessionFactory creation failed");
throw new ExceptionInInitializerError(ex);
}
}
/**
* @return the sessionfactory
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
连同hibernate.cfg.xml.
您实际上并没有使用请求参数,因为您的产品构造函数看起来像这样:
public Product(String string, String string2) {
super();
// TODO Auto-generated constructor stub
}
所以这条线并没有达到你的预期:
Product product= new Product(request.getParameter("prodId"),request.getParameter("prodName"));
这些都不起作用:
Product p = new Product();
p.setName(product.getName());
p.setId(product.getName());
或者这些:
query.setParameter("idVal", product.getId());
query.setParameter("nameVal", product.getName());
(顺便说一句,如果您仍然使用product
来设置查询参数,则不确定p
的用途(。
因此,由于GenerationType.AUTO
,产品名称为null,id为1,否则我认为也将为null。
您需要更改构造函数以使用以下参数:
public Product(String string, String string2) {
this.id = string;
this.name = string2;
}
这样,您以后就可以向数据库查询发送一些内容。