我是一个从数据库中插入和删除内容的网络应用程序。 这是我的数据库,名为" shine":
package entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "sdplines", catalog = "shine")
public class SDPLines implements java.io.Serializable {
@Id
@GeneratedValue
private int id;
@Column(name="value")
private String value;
@Column(name="session_id")
private String SessionId;
@ManyToOne
@JoinColumn(name="u_id")
private Utente utente;
public SDPLines() {
}
public SDPLines(String value) {
this.value=value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public Utente getUtente() {
return utente;
}
public void setUtente(Utente utente) {
this.utente = utente;
}
public String getSessionId() {
return SessionId;
}
public void setSessionId(String sessionId) {
SessionId = sessionId;
}
这些是我的实体:
在SDPLines.java中
package entities;
import java.util.Set;
// Generated 17-mar-2016 19.34.01 by Hibernate Tools 3.4.0.CR1
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* Utente generated by hbm2java
*/
@Entity
@Table(name = "utente", catalog = "shine")
public class Utente implements java.io.Serializable {
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
@Column(name = "ID", unique = true, nullable = false)
private long id;
private String username;
private String nome;
private String cognome;
private String password;
@OneToMany(mappedBy="utente")
private Set<SDPLines> sdp;
public Utente() {
}
public Utente(String username) {
this.username = username;
}
public Utente(String username, String nome, String cognome, String password) {
this.username = username;
this.nome = nome;
this.cognome = cognome;
this.password = password;
}
@Column(name = "username", unique = true, nullable = false, length = 10)
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name = "nome", length = 45)
public String getNome() {
return this.nome;
}
public void setNome(String nome) {
this.nome = nome;
}
@Column(name = "cognome", length = 45)
public String getCognome() {
return this.cognome;
}
public void setCognome(String cognome) {
this.cognome = cognome;
}
@Column(name = "password", length = 45)
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@OneToMany(mappedBy="utente")
public Set<SDPLines> getSdp() {
return sdp;
}
public void setSdp(Set<SDPLines> sdp) {
this.sdp = sdp;
}
}
在Utente.java
package model;
import org.hibernate.Query;
import entities.SDPLines;
import entities.Utente;
public class SDPLineModel extends AbstractModel {
public SDPLineModel(){
super (Utente.class);
}
public SDPLines findFingerprint(String fingerprint, Long u_id){
try{
if(!sessionFactory.getCurrentSession().getTransaction().isActive())
sessionFactory.getCurrentSession().getTransaction().begin();
Query q= this.sessionFactory.getCurrentSession().createQuery("select fing from SDPLines fing where fing.value=:valore and fing.utente.id=:id");
q.setString("valore", fingerprint);
q.setLong("id",u_id);
System.out.println("eseguo la query. Valore nel DB: "+fingerprint);
System.out.println("Fingerprint relativa a id"+u_id);
return (SDPLines) q.uniqueResult();
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public SDPLines loginUsername(String username){
try{
if(!sessionFactory.getCurrentSession().getTransaction().isActive())
sessionFactory.getCurrentSession().getTransaction().begin();
Query q= this.sessionFactory.getCurrentSession().createQuery("select ut from Utente ut where ut.username=:username");
q.setString("username", username);
System.out.println("eseguo la query. Valore nel DB: "+username);
return (SDPLines) q.uniqueResult();
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public SDPLines findFingerprintNew(String fingerprint, Long user_id) {
try{
if(!sessionFactory.getCurrentSession().getTransaction().isActive())
sessionFactory.getCurrentSession().getTransaction().begin();
Query q= this.sessionFactory.getCurrentSession().createQuery("select fing from SDPLines fing where fing.value=:valore and fing.utente.id=:id");
q.setString("valore", fingerprint);
q.setLong("id",user_id);
System.out.println("eseguo la query:fingerprint= "+fingerprint);
System.out.println("Fingerprint relativa a id= "+user_id);
return (SDPLines) q.uniqueResult();
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public SDPLines findFingerprintFinal(String fingerprint, String sessionid) {
try{
if(!sessionFactory.getCurrentSession().getTransaction().isActive())
sessionFactory.getCurrentSession().getTransaction().begin();
Query q= this.sessionFactory.getCurrentSession().createQuery("select fing from SDPLines fing where fing.value=:valore and fing.SessionId=:id");
q.setString("valore", fingerprint);
q.setString("id",sessionid);
System.out.println("eseguo la query:fingerprint= "+fingerprint);
System.out.println("Fingerprint relativa a id= "+sessionid);
return (SDPLines) q.uniqueResult();
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public void deleteFingerprint(String fingerprint, String sessionid) {
try{
if(!sessionFactory.getCurrentSession().getTransaction().isActive())
sessionFactory.getCurrentSession().getTransaction().begin();
// Query q= this.sessionFactory.getCurrentSession().createQuery("delete fing from SDPLines fing where fing.value=:valore and fing.SessionId=:id");
Query q= this.sessionFactory.getCurrentSession().createQuery("delete SDPLines where value=:valore and SessionId=:id");
q.setString("valore", fingerprint);
q.setString("id",sessionid);
System.out.println("eseguo la query: DELETE fingerprint= "+fingerprint);
System.out.println("Fingerprint relativa a id= "+sessionid+" DELETED");
}catch(Exception e){
e.printStackTrace();
}
}
这是SDPLineModel.java,它管理实体:
package controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;
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.Session;
import org.hibernate.SessionFactory;
import entities.SDPLines;
import entities.Utente;
import model.HibernateUtil;
import model.SDPLineModel;
import model.UtenteModel;
/**
* Servlet implementation class AccountController
*/
@WebServlet("/AccountController")
public class AccountController extends HttpServlet {
private static final long serialVersionUID = 1L;
private UtenteModel um= new UtenteModel();
public AccountController() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action= request.getParameter("action");
if(action==null){
request.getRequestDispatcher("menu.html").forward(request, response);
}else
{
if(action.equalsIgnoreCase("logout")){
HttpSession session= request.getSession();
session.removeAttribute("username");
response.sendRedirect("AccountController");
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action= request.getParameter("action");
UtenteModel um= new UtenteModel();
SDPLineModel sdpmodel= new SDPLineModel();
if (action.equals("InsertSDPLine")) {
HttpSession session1= request.getSession();
// String username = (String) session1.getAttribute("username");
// String password = (String) session1.getAttribute("password");
// Utente ut= um.loginUsername(username);
Long user_id = (Long) session1.getAttribute("user_id");
// Utente ut = um.login(username, password);
Utente ut = um.loginByID(user_id);
Long u_id = ut.getId();
SessionFactory sf = HibernateUtil.createSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
SDPLines sdp = new SDPLines(request.getParameter("sdpline"));
sdp.setSessionId(request.getParameter("sessionid"));
sdp.setUtente(ut);
session.save(sdp);
session.getTransaction().commit();
session.close();
String message = "INSERTED";
// response.addHeader("FINGERPRINT_VALIDITY", message);
System.out.println("sdpline: "+sdp+"inserita!");
response.getOutputStream().print(message);
response.getOutputStream().flush();
response.getOutputStream().close();
}
if (action.equals("CheckFingerprint")) {
// HttpSession session1= request.getSession();
// String username = (String) request.getParameter("username");
String fingerprint = (String) request.getParameter("fingerprint");
String sessionid = (String) request.getParameter("sessionid");
//String user_id = request.getParameter("user_id");
// HttpSession session1= request.getSession();
// Long user_id = (Long) session1.getAttribute("user_id");
// Utente ut = um.loginByID(user_id);
// Utente ut= um.loginUsername(username);
// Utente ut = um.login(username, password);
// Long u_id = ut.getId();
// SDPLines sdpline = sdpmodel.findFingerprint(fingerprint, user_id);
// SDPLines sdpline = sdpmodel.findFingerprintNew(fingerprint, user_id);
SDPLines sdpline = sdpmodel.findFingerprintFinal(fingerprint, sessionid);
if (sdpline == null) {
String message = "NOT_VALID";
// response.addHeader("FINGERPRINT_VALIDITY", message);
System.out.println("Fingerprint NON trovata! ");
response.getOutputStream().print(message);
response.getOutputStream().flush();
response.getOutputStream().close();
// request.getRequestDispatcher("AfterLogin.jsp").forward(request, response); // va nella pagina welcome
}
else {
String message = "VALID";
System.out.println("Fingerprint trovata! ");
response.getOutputStream().print(message);
response.getOutputStream().flush();
response.getOutputStream().close();
}
}
if (action.equals("DeleteFingerprint")) {
// HttpSession session1= request.getSession();
// String username = (String) request.getParameter("username");
String fingerprint = (String) request.getParameter("sdpline");
String sessionid = (String) request.getParameter("sessionid");
//String user_id = request.getParameter("user_id");
// HttpSession session1= request.getSession();
// Long user_id = (Long) session1.getAttribute("user_id");
// Utente ut = um.loginByID(user_id);
// Utente ut= um.loginUsername(username);
// Utente ut = um.login(username, password);
// Long u_id = ut.getId();
// SDPLines sdpline = sdpmodel.findFingerprint(fingerprint, user_id);
// SDPLines sdpline = sdpmodel.findFingerprintNew(fingerprint, user_id);
SDPLines sdpline = sdpmodel.findFingerprintFinal(fingerprint, sessionid);
if (sdpline == null) {
String message = "FINGEPRINT_NOT_PRESENT!";
// response.addHeader("FINGERPRINT_VALIDITY", message);
System.out.println("DELETE FINGEPRINT: FINGERPRINT NON PRESENTE! ");
response.getOutputStream().print(message);
response.getOutputStream().flush();
response.getOutputStream().close();
// request.getRequestDispatcher("AfterLogin.jsp").forward(request, response); // va nella pagina welcome
}
else {
sdpmodel.deleteFingerprint(fingerprint, sessionid);
String message = "DELETED";
System.out.println("Fingerprint rimossa! ");
response.getOutputStream().print(message);
response.getOutputStream().flush();
response.getOutputStream().close();
}
}
if(action.equals("loginUt")){
Utente ut= um.login(request.getParameter("username"), request.getParameter("password"));
if(ut==null){
request.setAttribute("message","Account Invalido");
request.getRequestDispatcher("loginUtente.jsp").forward(request, response); // rimane nella pagina della form
}else
{
HttpSession session= request.getSession();
session.setAttribute("username",request.getParameter("username"));
session.setAttribute("password",request.getParameter("password"));
session.setAttribute("user_id",ut.getId());
request.setAttribute("username",request.getParameter("username"));
// request.getRequestDispatcher("welcomeUtente.jsp").forward(request, response); // va nella pagina welcome
request.getRequestDispatcher("AfterLogin.jsp").forward(request, response); // va nella pagina welcome
}
}else
{
if(action.equals("registra")){
Utente ut= new Utente();
//prelevo valori dalla form e li scrivo nel DB
ut.setNome(request.getParameter("nome"));
ut.setCognome(request.getParameter("cognome"));
ut.setUsername(request.getParameter("username"));
ut.setPassword(request.getParameter("password"));
String password=request.getParameter("RepeatPassword");
//registrazione nuovo utente
this.um.create(ut);
request.getRequestDispatcher("loginUtente.jsp").forward(request, response);
}
}
}
}
我是一个在插入和删除特定的&#34; sdplines&#34;之间切换的servlet。基于&#34; session_id&#34;的行和&#34;价值&#34; SDPLine值。
我的问题如下:当我想通过浏览器将session_id和值放入我想要存储到我的mysql数据库中的新sdplines行时,这个servlet工作正常。但是,当我想删除一个sdplines行(总是从其session_id和value startinf)servlet时,使用hibernate,不会删除它。我只是简单地替换了#34;选择&#34;去&#34;删除&#34;在我的查询中。
这是servlet:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/shine</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="show_sql">true</property>
<mapping class="entities.SDPLines"/>
<mapping class="entities.Utente"/>
</session-factory>
</hibernate-configuration>
这是hibernate.cfg.xml
SQL> startup mount
SQL> alter session set events '10015 trace name adjust_scn level 1';
SQL> alter database open resetlogs;
答案 0 :(得分:1)
好吧,看来你创建查询后......你似乎没有运行它。
在从方法返回之前添加批量更新执行并记住提交事务并关闭会话(除非您在删除后执行更多事务操作)之后:
Query q= this.sessionFactory.getCurrentSession()
.createQuery("delete SDPLines where value=:valore and SessionId=:id");
q.setString("valore", fingerprint);
q.setString("id",sessionid);
q.executeUpdate();
session.getTransaction().commit();
}catch(Exception e){
session.getTransaction.rollback();
}finally{
session.close();
}