#java #spring #hibernate #hsqldb #entitymanager
#java #spring #переход в спящий режим #hsqldb #entitymanager
Вопрос:
Я использую entity manager в своем spring 3.2.2
приложении на базовом hsqld:
@Transactional
public class TestDaoImpl implements PersonDao {
/**
* Logger
*/
private static final Logger log = Logger.getLogger(TestDaoImpl.class);
@PersistenceContext
private EntityManager em;
/**
* @param em
*/
public TestDaoImpl() {
super();
}
@Override
public List<Person> getAll() {
log.info("get all DAO");
//TODO fix query!!!
List<Person> resultList = em.createQuery("SELECT * FROM Person;", Person.class).getResultList();
log.info(resultList.toString());
return resultList;
}
}
Мои pom.xml
зависимости:
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-tools</artifactId>
<version>3.2.0.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
Моя проблема в том, что my EntityManager em
равно нулю. Следовательно, диспетчер сущностей не внедряется spring. Вот контекст моего приложения:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<tx:annotation-driven />
<context:annotation-config />
<bean id="mainGUI" class="com.testApp.MainWindow" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:hsql://localhost/testDB" />
<property name="username" value="sa" />
<property name="password" value="" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="jpaData" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
Any recommendations why my application context does not get instantiated? Spring and hibernate load correctly!
UPDATE
I changed my class to:
@Transactional
@Repository
public class TestDaoImpl implements PersonDao {
private static final Logger log = Logger.getLogger(PersonDaoImpl.class);
@PersistenceContext
public EntityManager em;
public TestDaoImpl() {
super();
}
@Override
public List<Person> getAll() {
log.info("get all DAO");
//TODO fix query!!!
List<Person> resultList = em.createQuery("SELECT * FROM Person;", Person.class).getResultList();
log.info(resultList.toString());
return resultList;
}
}
However, I still get the exception java.lang.NullPointerException
at:
em.createQuery("SELECT * FROM Person;", Person.class).getResultList();
The debugger shows that, em
is still null.
Обновить
Мой файл журнала:
0 [main] INFO com.Application.main.startGUI - set look and feel
775 [main] INFO org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@291aff: startup date [Thu Jul 03 10:22:25 CEST 2014]; root of context hierarchy
854 [main] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [applicationContext.xml]
1383 [main] INFO org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'jpaData'
1546 [main] INFO org.hibernate.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
1551 [main] INFO org.hibernate.Version - HHH000412: Hibernate Core {4.2.0.Final}
1553 [main] INFO org.hibernate.cfg.Environment - HHH000206: hibernate.properties not found
1554 [main] INFO org.hibernate.cfg.Environment - HHH000021: Bytecode provider name : javassist
1576 [main] INFO org.hibernate.ejb.Ejb3Configuration - HHH000204: Processing PersistenceUnitInfo [
name: jpaData
...]
1859 [main] INFO org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator - HHH000130: Instantiating explicit connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
2593 [main] INFO org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.HSQLDialect
2624 [main] INFO org.hibernate.engine.transaction.internal.TransactionFactoryInitiator - HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
2630 [main] INFO org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory - HHH000397: Using ASTQueryTranslatorFactory
2971 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000228: Running hbm2ddl schema update
2971 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000102: Fetching database metadata
2975 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000396: Updating schema
3002 [main] INFO org.hibernate.tool.hbm2ddl.TableMetadata - HHH000261: Table found: PUBLIC.PUBLIC.READATA
3002 [main] INFO org.hibernate.tool.hbm2ddl.TableMetadata - HHH000037: Columns: [budgetednetrentalrevenuespernetsalesrevenues, marketrisklevel, installmentsontotalfacilityineurforyear10, totcom_9, totcom_8, totcom_7, customer, city, cashcollateral, leasecontractcurrency, cncy, projnr, maturitydate, usetype, totalnla, userisklevel, actualnetrentalrevenuespernetsalesrevenues, country, gccnr, che, market, totcom_10, percentagerentalincomeforyear8, percentagerentalincomeforyear7, avbinc_10, percentagerentalincomeforyear9, percentagerentalincomeforyear4, percentagerentalincomeforyear3, percentagerentalincomeforyear6, tic_contractcurrency, percentagerentalincomeforyear5, installmentsontotalfacilityincontractcurrencyforyear10, percentagerentalincomeforyear2, percentagerentalincomeforyear1, defaultrisklevel, avbinc_7, avbinc_6, installmentsontotalfacilityincontractcurrencyforyear9, avbinc_9, installmentsontotalfacilityincontractcurrencyforyear8, avbinc_8, installmentsontotalfacilityincontractcurrencyforyear7, avbinc_3, installmentsontotalfacilityincontractcurrencyforyear6, avbinc_2, installmentsontotalfacilityincontractcurrencyforyear5, avbinc_5, avbinc_4, installmentsontotalfacilityincontractcurrencyforyear4, installmentsontotalfacilityincontractcurrencyforyear3, externalcommitment, installmentsontotalfacilityincontractcurrencyforyear2, avbinc_1, installmentsontotalfacilityincontractcurrencyforyear1, id, installmentsontotalfacilityineurforyear8, installmentsontotalfacilityineurforyear7, installmentsontotalfacilityineurforyear9, locqual, repaysou, marketvalue_contractcurrency, installmentsontotalfacilityineurforyear1, installmentsontotalfacilityineurforyear2, refinancingrate, installmentsontotalfacilityineurforyear5, installmentsontotalfacilityineurforyear6, installmentsontotalfacilityineurforyear3, installmentsontotalfacilityineurforyear4, actualnla, externalcommitmentinpecentage, projstat, totcom_1, totcom_2, percentagerentalincomeforyear10, totcom_3, totcom_4, projectstatusrisklevel, totcom_5, totcom_6]
3002 [main] INFO org.hibernate.tool.hbm2ddl.TableMetadata - HHH000108: Foreign keys: []
3002 [main] INFO org.hibernate.tool.hbm2ddl.TableMetadata - HHH000126: Indexes: [sys_idx_sys_pk_10114_10115]
3006 [main] INFO org.hibernate.tool.hbm2ddl.TableMetadata - HHH000261: Table found: PUBLIC.PUBLIC.SCENARIOBASE
3006 [main] INFO org.hibernate.tool.hbm2ddl.TableMetadata - HHH000037: Columns: [id, others, see, year, cee, we]
3006 [main] INFO org.hibernate.tool.hbm2ddl.TableMetadata - HHH000108: Foreign keys: []
3007 [main] INFO org.hibernate.tool.hbm2ddl.TableMetadata - HHH000126: Indexes: [sys_idx_sys_pk_10117_10118]
3007 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000232: Schema update complete
3068 [main] INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7a1576: defining beans [org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,personBaseDaoImpl,mainGUI,dataSource,entityManagerFactory,transactionManager,entityManager,org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
3402 [AWT-EventQueue-0] INFO com.Application.gui.personSelection.MainWindow - make Layout
3402 [AWT-EventQueue-0] INFO com.Application.gui.personSelection.MainWindow - add Menue Bar
3413 [AWT-EventQueue-0] INFO com.Application.gui.personSelection.MainWindow - add Tab Bar
3416 [AWT-EventQueue-0] INFO com.Application.gui.personSelection.MainTabPanel - enterMainTabPanel create Layout method
3420 [AWT-EventQueue-0] INFO com.Application.gui.personSelection.PersonSelectionPanel - enter placeSelectionWithButtons method
3511 [AWT-EventQueue-0] INFO com.Application.gui.accronData.GUITabPanel - enter GUITabPanel create Layout method
3514 [AWT-EventQueue-0] INFO com.Application.service.PersonBaseServiceImpl - reveiving all data in the person base service
3514 [AWT-EventQueue-0] INFO com.Application.dao.PersonBaseDaoImpl - get all DAO
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.Application.dao.PersonBaseDaoImpl.getAll(PersonBaseDaoImpl.java:51)
at com.Application.service.PersonBaseServiceImpl.getAll(PersonBaseServiceImpl.java:45)
at com.Application.gui.accronData.GUIDataPanel.<init>(GUIDataPanel.java:42)
at com.Application.gui.accronData.GUITabPanel.createLayout(GUITabPanel.java:42)
at com.Application.gui.personSelection.MainWindow.createTabBar(MainWindow.java:159)
at com.Application.gui.personSelection.MainWindow.makeLayout(MainWindow.java:182)
at com.Application.gui.personSelection.MainWindow.access$1(MainWindow.java:173)
at com.Application.gui.personSelection.MainWindow$4.run(MainWindow.java:196)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Обновить
Мой PersonBaseServiceImpl
:
public class PersonBaseServiceImpl implements PersonBaseService {
/**
* Logger
*/
private static final Logger log = Logger.getLogger(PersonBaseServiceImpl.class);
/**
* PersonBaseDAO Access
*/
private TestDao testBaseDAO; // TestDao is an interface to my TestDaoImpl class
/**
* Constructor
* @param scenarioBaseDAO
*/
public PersonBaseServiceImpl(){
this.testBaseDAO = new TestDaoImpl();
}
@Override
public List<PersonBase> getAll() {
log.info("reveiving all data in the scenario base service");
return testBaseDAO.getAll();
}
}
Комментарии:
1. Как
TestDaoImpl
используется? Вы внедряете это в тест? Если да, то каким образом?2. @geoand Спасибо за ваш ответ! Я в основном создаю экземпляр на уровне сервиса. Пожалуйста, посмотрите мой файл журнала! Я также отладил свое приложение и обнаружил, что em равно нулю, поэтому запрос, который я хочу создать в моей базе данных, не может быть использован;(. Я действительно ценю ваш ответ!
3. Не могли бы вы, пожалуйста, опубликовать
PersonBaseServiceImpl
?4. @geoand Спасибо за ваш ответ! Пожалуйста, посмотрите мое последнее обновление.
Ответ №1:
Проблема в том, что вы создаете экземпляр PersonBaseDao
самостоятельно (используя new
), и это для того, чтобы позволить Spring управлять его жизненным циклом.
Вам нужно использовать @Autowired
на PersonBaseDao и создать PersonBaseServiceImpl
управляемый компонент Spring, используя аннотацию стереотипа (вероятно @Service
).
То же самое, конечно, относится и к способу использования PersonBaseServiceImpl внутри любого компонента, вызывающего его
Ответ №2:
Вам не хватает @Repository
аннотации в вашем DAO.
Комментарии:
1. Должен ли я заменить
@Transactional
аннотацию на@Repository
annotation?2. Могу ли я также использовать
@Component
вместо двух вышеуказанных? Я действительно ценю ваш ответ!3. @Vivien Нет, вы можете добавить обе аннотации
4. @Vivien Plaese добавьте также ‘<context:component-scan base-package=»your.package»/>` к контексту вашего приложения.
5. @Vivien есть ли какие-либо исключения в файле журнала при запуске вашего приложения