简体   繁体   中英

LocalContainerEntityManagerFactoryBean cannot be converted to EntityManagerFactory

I don't understand, what is happening at all!

Suddenly, simple things stopped to work!

Now ehen I am trying to define something like here http://docs.spring.io/spring-data/jpa/docs/1.10.6.RELEASE/reference/html/#jpa.java-config

I get the following compile error

incompatible types: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean cannot be converted to javax.persistence.EntityManagerFactory

And this is correct, since LocalContainerEntityManagerFactoryBean does not implement EntityManagerFactory , which is required by JpaTransactionManager#setEntityManagerFactory() method.

How it cn be, that official example contains absolutely incorrect snippet? How it can be, that this snippet was compiling for me for some time?

What I broke?

UPDATE

I was able to rewrite

txManager.setEntityManagerFactory(entityManagerFactory());

to

txManager.setEntityManagerFactory(entityManagerFactory().getObject());

So it is an error in the doc?

Yes, the documentation is wrong

Use this code to inject entity manager factory into txManager

@Bean @Autowired public PlatformTransactionManager transactionManager(EntityManagerFactory factory) {
    JpaTransactionManager txManager = new JpaTransactionManager();
    txManager.setEntityManagerFactory(factory);
    return txManager;
}
txManager.setEntityManagerFactory(entityManagerFactory());

is not suppose to work you either have to do this

txManager.setEntityManagerFactory(entityManagerFactory().getObject());

or

txManager.setEntityManagerFactory(entityManagerFactory().getNativeEntityManagerFactory()

so honestly I don't know why it previously worked.

I guess it is a mistake since I tried that way as described in the official doc and It didn't work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM