Firstly I would like to write : I know that configuring Hibernate JPA for Spring Boot is not so good idea and how to say "it is stupid", because Spring boot give you autoconfiguration etc. Ok, I know but I am learning by self Spring and Hibernate and I want to configure Hibernate step by step and Inject to Spring Context. So, my problem is, that I configure Hibernate and wrote the class to configuration, which use the "persitence.xml". Configuration for the @PersistenceContext is ok and EntityManager is working properly. But @Entity is not added to Hibernate context if is not include in "persistence.xml" file.
build.gradle is for jar file and that is the problem?
plugins {
id 'org.springframework.boot' version '2.1.9.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group = 'com.kamil'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
compile("org.springframework.boot:spring-boot-starter-web")
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.2.0.RELEASE'
/* compile group: 'org.hibernate', name: 'hibernate-core', version: '5.4.6.Final'
compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.4.8.Final'*/
compile("com.h2database:h2")
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.18'
}
The configuration class for JPA:
@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
public class HibernateConfiguration {
@Bean(name = "entityManagerFactory")
public LocalEntityManagerFactoryBean entityManagerFactoryBean(){
LocalEntityManagerFactoryBean factoryBean = new LocalEntityManagerFactoryBean();
factoryBean.setPersistenceUnitName("MySQL");
return factoryBean;
}
@Bean(name = "transactionManager")
public JpaTransactionManager JpaTransactionManagerBean(){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());
System.out.println("EMF "+entityManagerFactoryBean().getObject().toString());
return transactionManager;
}
}
When I have in "persistence.xml" file the Entity is ok. But if no the "Unknown Entity" is throwing. Is it possible to avoid in "persistence.xml" and only use the annotation for Entity in that configuration of application? That means Spring Boot jar + Hibernate JPA ?
I know that is Spring Data JPA in Spring Boot, but I will see more details for JPA if I configure in that form. I use SpringBoot, Spring Framework and Hibernate docs.
Thanks for reply. If I understood something wrong, please correct me.