Answer the question
In order to leave comments, you need to log in
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:
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
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;
}
}
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();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question