我是新来的冬眠者。我试图设置一些值与休眠到我的数据库。我想这很容易,但是我无法在互联网上找到答案。 我需要找到列foregin键userId = int idPrPivo并设置字符串prvo,drugo和trece的值到数据库中。 所以有人可以向我解释如何使用hibenrate将值设置到数据库中吗?
这是我的ajax
function sacuvajpiva(){
var user=localStorage.getItem("username");
var prvo=document.getElementById("prvopivoselect");
var prvopivo=prvo.options[prvo.selectedIndex].text;
var drugo=document.getElementById("drugopivoselect");
var drugopivo=drugo.options[drugo.selectedIndex].text;
var trece=document.getElementById("trecepivoselect");
var trecepivo=trece.options[trece.selectedIndex].text;
$.post("sacuvajpivaservlet", {"prvopivo":prvopivo, "drugopivo":drugopivo, "trecepivo":trecepivo, "user":user}, function(data){
});
}
这是我的servlet
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
String prvo=request.getParameter("prvopivo");
String drugo=request.getParameter("drugopivo");
String trece=request.getParameter("trecepivo");
String user=request.getParameter("user");
Configuration myConf = new Configuration();
myConf.configure("hibernate.cfg.xml");
StandardServiceRegistry service = new StandardServiceRegistryBuilder().
applySettings(myConf.getProperties()).build();
SessionFactory myFactory = myConf.buildSessionFactory(service);
Session conn = myFactory.openSession();
Transaction t = conn.beginTransaction();
List <Pivo> prvopivo=conn.createQuery("SELECT p FROM Pivo p WHERE pivIme='"+prvo+"'").list();
int idPrPivo=prvopivo.get(0).getPivId();
t.commit();
conn.close();
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
}