D
D
Dima Yakovlev2020-09-07 11:04:58
Java
Dima Yakovlev, 2020-09-07 11:04:58

Spring + Hibernate java 1.8 on windows 10 in intellij idea have conflicts?

I'm writing here as I couldn't find a solution to the problem myself. There is a project written in java 1.8. The customer has no desire to rewrite for the new version. Wanted to deploy it on my work machine with Windows 10. Installed the required version of java, apache tomcat 8.5, mysql 5.7, intellij is the latest version.
I must say right away - the same project works flawlessly with the same set under Centos on VM and on ubuntu 20.04 on the second partition of the disk.

Here is the error on the user account login method:

Mistake
javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'merge' call org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:282) com.sun.proxy.$Proxy243.merge(Unknown Source) com.projectName.dao.GenericDaoImpl.update(GenericDaoImpl.java:59) com.projectName.mvc.service.impl.UserServiceImpl.updateUnsuccessfullAttempt(UserServiceImpl.java:380) com.projectName.security.security.auth.ajax.AjaxAuthenticationProvider.authenticate(AjaxAuthenticationProvider.java:58) org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:174) org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:199) org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$AuthenticationManagerDelegator.authenticate(WebSecurityConfigurerAdapter.java:469) com.projectName.security.security.auth.ajax.AjaxLoginProcessingFilter.attemptAuthentication(AjaxLoginProcessingFilter.java:61) org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:121) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:66) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214) org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177) org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)


Code and other files.


app.properties

database.driverClassName=com.mysql.jdbc.Driver
database.url=jdbc:mysql://127.0.0.1:3306/nt
database.username=root
database.password=qwerty

#spring database configuration
spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1

#Hibernate related properties
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=true

HibernateConfiguration.java
package com.projectName.mvc.configuration;

import java.util.Properties;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.projectName.dao" })
@PropertySource(value = { "classpath:application.properties" })
@PropertySource(value = "file:${CONF_DIR}/application-override.properties", ignoreResourceNotFound = true)
public class HibernateConfiguration {

  @Autowired
  private Environment environment;

  @Bean
  public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource());
    sessionFactory.setPackagesToScan(new String[] { "com.projectName.entity" });
    sessionFactory.setHibernateProperties(hibernateProperties());
    return sessionFactory;
  }

  @Bean
  public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(environment.getRequiredProperty("database.driverClassName"));
    dataSource.setUrl(environment.getRequiredProperty("database.url"));
    dataSource.setUsername(environment.getRequiredProperty("database.username"));
    dataSource.setPassword(environment.getRequiredProperty("database.password"));
    return dataSource;
  }

  private Properties hibernateProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
    properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
    properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));

    properties.put("spring.datasource.testOnBorrow",
        environment.getRequiredProperty("spring.datasource.testOnBorrow"));
    properties.put("spring.datasource.validationQuery",
        environment.getRequiredProperty("spring.datasource.validationQuery"));
    return properties;
  }

  @Bean
  @Autowired
  public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
    final JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(emf);
    return transactionManager;
  }
}

GenericDaoImpl.java
package com.projectName.dao;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.StoredProcedureQuery;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.transaction.Transactional;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.query.internal.NativeQueryImpl;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.projectName.entity.AbstractEntity;

@Repository("genericDao")
@Transactional
public class GenericDaoImpl implements GenericDao {

  
  @Autowired
  protected SessionFactory sessionFactory;
  
  
  
  @PersistenceContext
  private EntityManager entityManager;
  
  public EntityManager getEntityManager() {
    return entityManager;
  }

  public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
  }

  @Override
  public <E extends AbstractEntity> E create(E model) {
    entityManager.persist(model);
    return model;
  }

  @Override
  public <E extends AbstractEntity> E update(E model) {
    model = entityManager.merge(model);
    entityManager.flush();
    return model;
  }

  @Override
  public <E extends AbstractEntity> void delete(E model) {
     entityManager.remove(model);

  }

  @Override
  public <E extends AbstractEntity> E getById(Class<E> modelClass, Serializable id) {
      return entityManager.find(modelClass, id);
  }

  @Override
  public <E extends AbstractEntity> void deleteById(Class<E> modelClass, Serializable id) {
    final E entity = getById(modelClass,id);
    entityManager.remove(entity);

  }

  @Override
  public <E extends AbstractEntity> List<E> getAll(Class<E> model) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<E> criteriaQuery = criteriaBuilder.createQuery(model);
    criteriaQuery.from(model);
    return entityManager.createQuery(criteriaQuery).getResultList();
  }

}




Also asked a question on stackoverflow .

There is a suspicion that in Windows there is some kind of conflict with libs. I hope to help in solving this problem. While working from ubuntu, but I would like to understand the problem on Windows =)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question