2015-12-30 2 views
0
<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" 
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> 
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <class>com.test.dao.CustomerDaoImpl</class> 
    <class>com.test.data.Customer</class> 
    <class>com.test.dto.CustomerDto</class> 
    <properties> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> 
     <property name="hibernate.show_sql" value="false"/> 
     <property name="hibernate.hbm2ddl.auto" value="update"/> 
     <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultNamingStrategy"/> 
     <property name="hibernate.connection.charSet" value="UTF-8"/> 
    </properties> 
</persistence-unit> 

EntityManager всегда пустой и не вводили правильно

класса, где я использую его:

public class CustomerDaoImpl implements CustomerDao { 

@PersistenceContext(unitName = "persistenceUnit") 
private EntityManager entityManager; 

@Transactional 
public List<CustomerDto> getCustomers() { 
    List<CustomerDto> customers = null; 
    List<Customer> cust = new ArrayList<Customer>(); 
    Query q = entityManager.createQuery(
      "SELECT c" 
      + " FROM Customer c "); 

EDIT ДОБАВЛЕНИЕ SPRING-servlet.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> 

    <context:spring-configured /> 
    <context:component-scan base-package="com.test"/> 


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
     destroy-method="close"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost:3306/testdb" /> 
     <property name="username" value="root" /> 
     <property name="password" value="password" /> 
     <property name="testOnBorrow" value="true" /> 
     <property name="testOnReturn" value="true" /> 
     <property name="testWhileIdle" value="true" /> 
     <property name="timeBetweenEvictionRunsMillis" value="1800000" /> 
     <property name="numTestsPerEvictionRun" value="3" /> 
     <property name="minEvictableIdleTimeMillis" value="1800000" /> 
    </bean> 
<bean class="org.springframework.orm.jpa.JpaTransactionManager" 
    id="transactionManager"> 
    <property name="entityManagerFactory" ref="entityManagerFactory" /> 
</bean> 

<tx:annotation-driven mode="aspectj" 
    transaction-manager="transactionManager" /> 

<bean 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" 
    id="entityManagerFactory"> 
    <property name="persistenceUnitName" value="persistenceUnit" /> 
    <property name="dataSource" ref="dataSource" /> 
</bean> 

<bean 
    class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> 
<bean 
    class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> 

<bean 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/js/" /> 
    <property name="suffix" value=".js" /> 
</bean> 

После того, как я протестировал это, я узнал, что Netbeans дает мне ошибку в классе CustomerDaoImpl, и он говорит, что: «Класс указан в файле persistence.xml, но он не аннотирован». Что такое правильная аннотация в этом случае?

Я могу видеть из журнала, что правильно EntityManager для пу: Закрытие JPA EntityManagerFactory для единицы сохранения «persistenceUnit» и я создаю дао так:

@Autowired 
CustomerDao customerDao; 

При использовании @autowired аннотации, CustomerDAO является null, поэтому я попытался создать dao с

customerDao = new CustomerDaoImpl(); 

, а затем entitymanager NULL.

+0

Как вы проверить это? Как вы получаете экземпляр 'CustomerDaoImpl'? –

+0

Во-первых, вы должны аннотировать свой CustomerDaoImpl с помощью @Singleton. – Riadh

+1

@Riadh Поскольку вопрос помечен как 'spring',' CustomerDaoImpl' должен быть фактически аннотирован с '@ Repository'. – Naros

ответ

1

В persistence.xml вам не нужно

<class>com.test.dao.CustomerDaoImpl</class> 
<class>com.test.dto.CustomerDto</class> 

использовать только

<class>com.test.data.Customer</class>