-2

I have library module which I want to use to store Hibernate models. I ask have Spring WAR package which I want to use.

Main Spring WAR:

@Configuration
@EnableTransactionManagement
public class ContextDatasource {

    @Bean
    public LocalSessionFactoryBean sessionFactory() throws NamingException {
        final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { "org.plugin.database.models" });
        sessionFactory.setHibernateProperties(hibernateProperties());

        return sessionFactory;
    }

    @Bean
    public DataSource dataSource() throws NamingException {
        return (DataSource) new JndiTemplate().lookup("java:/global/production_gateway");
    }

    @Bean
    public PlatformTransactionManager hibernateTransactionManager() throws NamingException {
        final HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory().getObject());
        return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    private final Properties hibernateProperties() {
        final Properties hibernateProperties = new Properties();
        return hibernateProperties;
    }
}

Library DAO:

public class BlacklistsDaoHibernate implements BlacklistsDao {

    Session session;

    @Autowired
    SessionFactory sessionFactory;

    public BlacklistsDaoHibernate() {
        session = sessionFactory.getCurrentSession();
    }

    @Override
    public void saveOrUpdate(BlacklistsModel blacklistsModel) throws Exception {
        try {
            session.getTransaction().begin();
            session.saveOrUpdate(blacklistsModel);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
            throw new Exception("Error");
        }
    }

But I get NPE at this line session = sessionFactory.getCurrentSession();

What is the proper way to use sessionFactory into the Library Jar module?

flyingfox
  • 13,414
  • 3
  • 24
  • 39
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

1 Answers1

0

You can use @PostConstruct or constructor injection. Spring instantiates your object, then resolves its @Autowired fields. You can ask that it resolve your object's dependencies prior to instantiating the object via constructor injection.

With constructor injection:

SessionFactory sessionFactory;
Session session;
public BlacklistsDaoHibernate(@Autowired SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
    this.session = sessionFactory.getCurrentSession();
}

With @PostConstruct:

@Autowired
SessionFactory sessionFactory;
Session session;

@PostConstruct
void init() {
    this.session = sessionFactory.getCurrentSession();
}

Edit: If your goal is just to resolve the NullPointerException as mentioned in the comments, use this instead:

@PostConstruct
void init() {
    this.session = sessionFactory.openSession();
}

There's an example of the typical usage of SessionFactory at https://www.java2novice.com/hibernate/session-factory.

Steve Perkins
  • 117
  • 1
  • 7
  • I get null for session. – Peter Penzov Jul 08 '18 at 15:33
  • Are you starting the session on SessionFactory somewhere else? The point of calling `.getCurrentSession()` is so you don't create a bunch of extra sessions throughout your program. Usually this is handled by creating a custom "session factory" that injects `SessionFactory` and when some kind of getSession() function is called, checks to see if a session has already been opened. If not, it begins a new session and returns that. – Steve Perkins Jul 08 '18 at 15:37
  • No, I suppose that my SessionFactory is not correct. Can you check it am I missing something? – Peter Penzov Jul 08 '18 at 17:05
  • @PeterPenzov Take a look at my edit to this answer. Does that help? – Steve Perkins Jul 09 '18 at 11:20
  • I suspect that I'm not properly configuring the application startup. Can you please take a look here? https://stackoverflow.com/questions/51242707/nosuchmethodexception-org-springframework-boot-autoconfigure-http-httpmessageco – Peter Penzov Jul 09 '18 at 11:30
  • Nah. I've already answered your question and you downvoted me in response, so I'm not interested in solving your other problems. – Steve Perkins Jul 11 '18 at 21:00
  • It's not my down vote. – Peter Penzov Jul 11 '18 at 21:10
  • Should I use @Transactional in order to activate @Autowired? – Peter Penzov Jul 11 '18 at 21:11