У меня возникают проблемы при попытке внедрить EntityManager из источника данных Tomcat, вот мои файлы:
persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <!-- unidade de persistencia com o nome financas --> <persistence-unit name="cartorioDocket"> <!-- Implementação do JPA, no nosso caso Hibernate --> <!-- <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> --> <!-- Aqui são listadas todas as entidades --> <class>cursos.alura.jpa.jpaComJava.model.Cartorio</class> <properties> <!-- Propriedades JDBC --> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/cartorio" /> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="PASSWORD" /> <!-- Configurações específicas do Hibernate --> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> </properties> </persistence-unit>
spring-context.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="br.com.caelum.contas" /> <mvc:annotation-driven /> <!-- Create default configuration for Hibernate --> <jee:jndi-lookup id="dbDataSource" jndi-name="java:comp/env/jdbc/cartorio" /> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitName" value="cartorioDocket" /> <property name="dataSource" ref="dbDataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> </property> </bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean>
Я пытался добавить это @PersistenceContext(unitName = "cartorioDocket", type = PersistenceContextType.EXTENDED) private EntityManager em; в мой класс DAO, но выбрасывается NullPointer.когда я использую стандартную реализацию JPA, используя Persistence.createEntityManagerFactory("cartorioDocket"), она работает нормально, но с инъекцией нет.
@PersistenceContext(unitName = "cartorioDocket", type = PersistenceContextType.EXTENDED) private EntityManager em;
Persistence.createEntityManagerFactory("cartorioDocket")