0
votes

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.

1
do you mean you wan to scan packages containing Entities? LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); factoryBean.setPackagesToScan("com.abc.data"); ? - user3487063
But I am using LocalEntityManagerFactoryBean. And that object doesn't have it method. Should I change configuration to that, which you described? - kamilguti
yes, if you want to use the scanning. Read more in LocalContainerEntityManagerFactoryBean documentation. - user3487063
Ok. So I jar Spring Boot in that case is not any option to avoid class in persistence.xml? - kamilguti
You can try this : <property name="hibernate.archive.autodetection" value="class"/> putting in your persistence.xml, see stackoverflow.com/questions/1780341/… - user3487063

1 Answers

0
votes

Adding my comments as an answer, let me know if that helps:

From LocalContainerEntityManagerFactoryBean doc:

As with LocalEntityManagerFactoryBean, configuration settings are usually read in from a META-INF/persistence.xml config file, residing in the class path, according to the general JPA configuration contract. However, this FactoryBean is more flexible in that you can override the location of the persistence.xml file, specify the JDBC DataSources to link to, etc.

Without using LocalEntityManagerFactoryBean , you can do:

LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setPackagesToScan("pkg.containing.entities");

If you want to stay with persistence.xml and LocalEntityManagerFactoryBean ,you can try Hibernate auto detect feature by adding following to your persistence.xml file

<property name="hibernate.archive.autodetection" value="class"/>

See this for more on how to setup your persistence.xml for this case.