0

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.

kamilguti
  • 23
  • 1
  • 6
  • do you mean you wan to scan packages containing Entities? `LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); factoryBean.setPackagesToScan("com.abc.data");` ? – user3487063 Nov 20 '19 at 21:06
  • But I am using LocalEntityManagerFactoryBean. And that object doesn't have it method. Should I change configuration to that, which you described? – kamilguti Nov 20 '19 at 21:08
  • yes, if you want to use the scanning. Read more in [LocalContainerEntityManagerFactoryBean](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.html) documentation. – user3487063 Nov 20 '19 at 21:10
  • Ok. So I jar Spring Boot in that case is not any option to avoid class in persistence.xml? – kamilguti Nov 20 '19 at 21:31
  • You can try this : `` putting in your `persistence.xml`, see https://stackoverflow.com/questions/1780341/do-i-need-class-elements-in-persistence-xml – user3487063 Nov 20 '19 at 21:49

1 Answers1

0

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.

user3487063
  • 3,672
  • 1
  • 17
  • 24
  • Thanks for your answer. Previosu I found on stackoverflow info about: But it does't help with my problem. Still was "unknown Entity". I found one article, (availible on link https://www.boraji.com/spring-4-hibernate-5-jpa-2-integration-example). And in configraution persistence.xml doesn't have "hibernate.archive.autodetection", but only @ComponentScans. I tried this too, but still "unknown entity". Only adding to persistence.xml help with my problem. I am thinking that I have maybe in some place wrong config. – kamilguti Nov 21 '19 at 10:03
  • In LocalContainerEntityManagerFactoryBean always have to put setPackagesToScan() ? Is any option to auto detection Spring + Hibernate JPA @Entity class? In my case in LocalEntityManagerFactoryBean that is not working. Still I have to put manually to persistence.xml ? – kamilguti Nov 22 '19 at 12:13
  • @kamilguti, I'll check this today. Can you post your persistence.xml and dependencies just in case. – user3487063 Nov 22 '19 at 15:11
  • Today I saw your comment, ok I did public repo with the same configuration. Could you check on github? https://github.com/kamilall234/Service – kamilguti Nov 24 '19 at 12:41
  • Did you check maybe? I have still the trouble. My dependencies are in gradle in that simple porject on github. I am still thinking about that auto detection for @Entity annotation. Why EntityManager is not see that Entity if is not added to persistence.xml? EVen if is "hibernate.archive.autodetection" in .xml. I know that xml is not the best solution. I will try today with LocalEntityContainerManagerFactoryBean class for JPA. One more question, If I would like cofigure custome SpringBoot, should I looking for in Spring Boot doc or better in Spring Framework? – kamilguti Nov 26 '19 at 12:51
  • your git project doesn't have any details except the spring-starter – user3487063 Nov 26 '19 at 16:38
  • Ok sory, now is add to branch SQL_Connect on github. Previously I am not add persistence.xml to commit. Sory for my mistake. – kamilguti Nov 27 '19 at 20:45