美好的一天,EntityManager始終爲NULL使用SpringFramework
我一直在研究Maven + Spring + Hibernate項目。實際上,這只是一個測試項目,只是爲了熟悉Spring如何使用Hibernate(+ Maven)。我已經設置並準備了必要的依賴關係。即Spring的appcontext.xml
,Hibernate的persistence.xml
,JPA/Persistence/Hibernate的實體和DAO對象。
在調試期間,觀察到EntityManager
總是null
。我不知道是什麼導致了這一點,因爲我已經做了FF:
- 自動裝配它在我的控制器上
- 宣佈它作爲一個bean我
applicationContext.xml
- 我的註釋DAO對象
@Repository
- 定義
entityManagerFactory
,transactionManager
和vendorAdapter
如豆類我applicationContext.xml
我一直在調試,並試圖解決方法一整天。不幸的是,我還沒有解決這個問題。希望有人可以在這個問題上提出一些看法。
下面是我的項目的代碼和配置:
< --- 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_1_0.xsd"
version="1.0">
<persistence-unit name="msh" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.msh.TblFileinfo</class>
</persistence-unit>
</persistence>
< --- applicationContext.xml的--->
<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:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.springframework.org/schema/security">
<!-- need to create database.properties file -->
<context:property-placeholder location="classpath:database.properties"/>
<context:component-scan base-package="com.msh"/>
<!-- tell Spring that it should act on any @PersistenceContext and @Transactional annotations found in bean classes -->
<!-- <tx:annotation-driven/> -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean class="com.msh.TblFileinfoHome" />
<bean class="com.msh.TblFileinfo" />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!-- <property name="databasePlatform" value="${platform}" /> -->
<property name="showSql" value="${database.showSql}" />
<property name="generateDdl" value="${database.generateDdl}" />
</bean>
<bean id="entityManagerFactory" class="org.org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="msh" />
<property name="dataSource" ref="dataSource" />
<!-- <property name="loadTimeWeaver" class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /> -->
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driverClassName}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
</beans>
< ---主要java代碼--->
package com.msh;
public class MavenSpringHibernate {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Sample s = new Sample();
s.persist();
}
}
< --- --- DAO>
package com.msh;
import javax.ejb.Stateless;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.PersistenceUnit;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Home object for domain model class TblFileinfo.
* @see com.trendmicro.grid.mshPackage.TblFileinfo
* @author Hibernate Tools
*/
/**
@Stateless
@Repository
@Transactional
*/
@Repository
public class TblFileinfoHome {
private static final Log log = LogFactory.getLog(TblFileinfoHome.class);
@PersistenceContext(unitName="msh")
private EntityManager entityManager;
@Transactional
public void persist(TblFileinfo transientInstance) {
log.debug("persisting TblFileinfo instance");
try {
entityManager.persist(transientInstance);
log.debug("persist successful");
}
catch (RuntimeException re) {
log.error("persist failed", re);
throw re;
}
}
@Transactional
public void remove(TblFileinfo persistentInstance) {
log.debug("removing TblFileinfo instance");
try {
entityManager.remove(persistentInstance);
log.debug("remove successful");
}
catch (RuntimeException re) {
log.error("remove failed", re);
throw re;
}
}
@Transactional
public TblFileinfo merge(TblFileinfo detachedInstance) {
log.debug("merging TblFileinfo instance");
try {
TblFileinfo result = entityManager.merge(detachedInstance);
log.debug("merge successful");
return result;
}
catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
@Transactional
public TblFileinfo findById(Long id) {
log.debug("getting TblFileinfo instance with id: " + id);
try {
TblFileinfo instance = entityManager.find(TblFileinfo.class, id);
log.debug("get successful");
return instance;
}
catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
}
--- ---實體
package com.msh;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* TblFileinfo generated by hbm2java
*/
@Entity
@Table(name="tbl_fileinfo"
,catalog="behavior"
)
public class TblFileinfo implements java.io.Serializable {
private Long fileId;
private String filename;
private String filetype;
public TblFileinfo() {
}
public TblFileinfo(String filename, String filetype) {
this.filename = filename;
this.filetype = filetype;
}
@Id @GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="file_id", unique=true, nullable=false)
public Long getFileId() {
return this.fileId;
}
public void setFileId(Long fileId) {
this.fileId = fileId;
}
@Column(name="filename", length=200)
public String getFilename() {
return this.filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
@Column(name="filetype", length=50)
public String getFiletype() {
return this.filetype;
}
public void setFiletype(String filetype) {
this.filetype = filetype;
}
}
< --- Sample類控制器--->
package com.msh;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.trendmicro.grid.msh.TblFileinfo;
import com.trendmicro.grid.msh.TblFileinfoHome;
@Transactional
public class Sample {
@Autowired
private TblFileinfo tinfo;
private TblFileinfoHome tinfoh;
public Sample()
{
tinfo = new TblFileinfo("c:/jayson/murillo/pryde.exe", "uv_win32");
tinfoh = new TblFileinfoHome();
}
public void persist()
{
tinfoh.persist(tinfo);
}
}
再次,希望有人可以提供這方面的反饋意見。提前致謝!
感謝。但是,「Sample s = context.getBean(Sample.class);」不允許...我的意思是我有一些錯誤 – jaysonpryde 2012-02-16 11:47:49
@jaysonpryde:什麼錯誤?您使用的是哪個版本的Spring和Java?編譯錯誤或運行時異常? – 2012-02-16 12:00:48
@ Tomasz - 由於我沒有聲明Sample class爲applicationContext.xml中的bean,我想我不能這樣做......錯誤是這樣的:「類型BeanFactory中的方法getBean(String)不適用爲參數(類)「 –
jaysonpryde
2012-02-16 12:04:02