hibernate无法删除,更新,插入表中

时间:2015-11-08 09:06:37

标签: hibernate servlet-filters

我正在使用jsp,servlet,hibernate和mysql开发一个Web应用程序。一切都工作正常,直到两天前,我得到一个奇怪的缺陷,我无法删除或更新我的数据,当我插入我的数据时,它没有进入表格。但我可以在我的网站上看到它。喜欢这张图片:

as you can see I can view inserted data in my website but the table in phpmyadmin is empty!

我读过如果hibernate不能插入,删除或更新它会抛出一个HibernateException但在我的情况下一切正常。

我不知道您需要哪些关于我的项目的信息,我会将我的hibernate配置和Dao代码放在这里,如果您需要更多信息,请告诉我......

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Data;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.HibernateException;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.Session;

import Domain.News;
import org.hibernate.Hibernate;

/**
 *
 * @author Rezaa
 */

public class NewsDaoImp implements NewsDao {

public NewsDaoImp() {
    HibernateUtil.beginTransaction();
}

@Override
public int insert(News news) {
    Session session = HibernateUtil.getSession();

    try {
        session.save(news);
    } catch (HibernateException e) {
        e.printStackTrace();
    }

    return news.getId();
}

@Override
public void delete(int id) {
    Session session = HibernateUtil.getSession();
    try {
        News news = (News) session.get(News.class, id);
        session.delete(news);
    } catch (HibernateException e) {
        e.printStackTrace();
    }

}

@Override
public News getById(int id) {
    Session session = HibernateUtil.getSession();
    News news = null;
    try {
        news = (News) session.get(News.class, id);
        Hibernate.initialize(news.getItem().getImages());
    } catch (HibernateException e) {

    }

    return news;
}

@Override
public List<News> getAll() {
    Session session = HibernateUtil.getSession();
    List<News> news = null;
    try {
        Query q = session.createQuery("from News");
        news = q.list();
        for (News n : news) {
            Hibernate.initialize(n.getImages());
        }
    } catch (HibernateException e) {

    }

    return news;
}

@Override
public void update(News news) {
    Session session = HibernateUtil.getSession();
    try {
        session.update(news);
    } catch (HibernateException e) {
   System.out.println("exeption in news update");
    }
}

}

和我的hibernate配置:

 <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"classpath://org/hibernate/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="hibernate.connection.driver_class">
            com.mysql.jdbc.Driver
        </property>

        <!-- Assume test is the database name -->
        <property name="hibernate.connection.url">
            jdbc:mysql://localhost/ofogh?characterEncoding=UTF-8
        </property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
                <property name="hibernate.current_session_context_class">thread</property>
        <!-- List of XML mapping files -->

        <mapping class="Domain.Item"/> 
        <mapping class="Domain.Image"/> 
        <mapping class="Domain.News"/>
        <mapping class="Domain.Person"/>
        <mapping class="Domain.ContactInfo"/>
        <mapping class="Domain.Specialist"/> 
        <mapping class="Domain.Teacher"/>
        <mapping class="Domain.SubClinic"/>
        <mapping class="Domain.Workshop"/>
        <mapping class="Domain.Tool"/>
        <mapping class="Domain.Video"/>
        <mapping class="Domain.Tag"/>
    </session-factory>
</hibernate-configuration>

HibernateUtil:

    package Data;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
private static final SessionFactory sessionFactory;

private static final ThreadLocal threadSession
        = new ThreadLocal();

private static final ThreadLocal threadTransaction
        = new ThreadLocal();

static {
    try {
        Configuration configuration = new Configuration().configure();
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
                applySettings(configuration.getProperties());
        sessionFactory = configuration.buildSessionFactory(builder.build());
    } catch (Throwable ex) {
        ex.printStackTrace(System.out);
        throw new ExceptionInInitializerError(ex);
    }
}

public static Session getSession() {

    Session s = (Session) threadSession.get();

//打开一个新的Session,如果这个帖子还没有         试试{

        if (s == null) {

            s = sessionFactory.openSession();

            threadSession.set(s);

        }

    } catch (HibernateException ex) {

        //throw new InfrastructureException(ex);
    }

    return s;

}

public static void closeSession() {

    try {

        Session s = (Session) threadSession.get();

        threadSession.set(null);

        if (s != null && s.isOpen()) {
            s.close();
        }

    } catch (HibernateException ex) {

        //throw new InfrastructureException(ex);
    }

}

public static void beginTransaction() {

    Transaction tx = (Transaction) threadTransaction.get();

    try {

        if (tx == null) {

            tx = getSession().beginTransaction();

            threadTransaction.set(tx);

        }

    } catch (HibernateException ex) {

        //throw new InfrastructureException(ex);
    }

}

public static void commitTransaction() {

    Transaction tx = (Transaction) threadTransaction.get();

    try {

        if (tx != null && !tx.wasCommitted()
                && !tx.wasRolledBack()) {
            tx.commit();
        }

        threadTransaction.set(null);

    } catch (HibernateException ex) {

        rollbackTransaction();

        //throw new InfrastructureException(ex);
    }

}

public static void rollbackTransaction() {

    Transaction tx = (Transaction) threadTransaction.get();

    try {

        threadTransaction.set(null);

        if (tx != null && !tx.wasCommitted()
                && !tx.wasRolledBack()) {

            tx.rollback();

        }

    } catch (HibernateException ex) {

        //throw new InfrastructureException(ex);
    } finally {

        closeSession();

    }

}

}

过滤:

 package Servlets;

import Data.HibernateUtil;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

@WebFilter("/*")
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
    //NOP
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    try {
        HibernateUtil.commitTransaction();
        chain.doFilter(request, response);
    } finally {
        HibernateUtil.closeSession();
    }
}

@Override
public void destroy() {
    //NOP
}

}

0 个答案:

没有答案