Hibernate null pointer(session) when having a lot of mapping classes

时间:2016-04-25 09:23:30

标签: hibernate nullpointerexception

When I'm inserting an object with a generic DAO class im getting null pointer in session, that happen only when I have a lot of mapping classes (I have 110 tables in db), if I do the same test with exactly same code but only with 10 mapping classes in the package mappings/hibernate.cfg the program works correctly, it looks like DAO tries to go faster than hibernate session loading? (It's perfonmance problem?) Thanks in advance I'm wasting so much time to solve this.

This is my hibernate session:

public class HibernateSession {
    private static final SessionFactory sessionFactory= buildSessionFactory();
    private static Session session;

    private static SessionFactory buildSessionFactory() {
        Configuration configuration = new Configuration();
        configuration.configure();
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        return sessionFactory;
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static Session getSession() {
        if (null == session || !session.isOpen()) {
            session = sessionFactory.openSession();

        }
        return session;
    }

}

insert method:

public void insert(T entity) throws HibernateException {
    try {
        startTransaction();
        session.persist(entity);
        session.flush();
    } catch (HibernateException e) {
        handleException(e);
    } finally {
        endTransaction();
    }
}

protected void startTransaction() {

    session = HibernateSession.getSession();
    session.beginTransaction();
}

    protected void endTransaction() {
    session.getTransaction().commit();
    session.close();
}

Console:

    abr 25, 2016 10:52:59 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
abr 25, 2016 10:53:00 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
Exception in thread "main" java.lang.NullPointerException
    at tws.hibernate.dao.GenericDAO.endTransaction(GenericDAO.java:58)
    at tws.hibernate.dao.GenericDAO.insert(GenericDAO.java:87)
    at TwsTestRunner.main(TwsTestRunner.java:26)

1 个答案:

答案 0 :(得分:0)

好吧,我解决了它,这是一个不同的异常,但是没有在控制台中显示,我可以看到在程序开始时执行HibernateSession实例的错误,错误是:

Caused by: org.hibernate.AnnotationException: Mixing nullable and non nullable columns in a property is not allowed: 

这是数据库的一个问题,有些FK标记为不可为空,因此hibernate将它们映射错误。,当我有几张桌子时程序正常工作,因为它们是正确的。