我正在尝试使用hibernate和sessions/sessionfactory向数据库添加一行。但是,当我执行以下代码时,它只向数据库添加了一次值为“1,null”的代码。我认为问题出在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");
}
}
以下是my product.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;
}
}
我还有冬眠课:
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。
1条答案
按热度按时间zzzyeukh1#
您实际上没有使用请求参数,因为您的产品构造函数如下所示:
所以这句话并没有达到你的预期:
这些也不是:
或者这些:
(顺便问一下,我不知道这有什么用
p
如果你还用product
设置查询参数)。因此,最终产品名为null,id为1,因为
GenerationType.AUTO
,否则也会是空的,我想。您需要更改构造函数以使用以下参数:
这样,您以后就可以向数据库查询发送一些内容。