弹簧+ JPA + DAO + Tomcat的

时间:2011-08-18 08:14:06

标签: spring tomcat openjpa

的persistence.xml

<persistence-unit name="myEntityManager" transaction-type="RESOURCE_LOCAL">
<class>com.epam.testapp.model.News</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>

新闻

private Integer newsId;
private String title;
private String brief;
private String content;
private Date created;
private String dateCreatedString;

public News() {
}

@Id
@Column(name = "NEWS_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "news_seq")
@SequenceGenerator(name = "news_seq", sequenceName = "NEWS_SEQ")
public Integer getNewsId() {
    return newsId;
}

public void setNewsId(Integer newsId) {
    this.newsId = newsId;
}

@Column(nullable = false, name = "TITLE")
public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

@Column(nullable = false, name = "BRIEF")
public String getBrief() {
    return brief;
}

public void setBrief(String brief) {
    this.brief = brief;
}

@Column(name = "CONTENT")
public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
}

@Temporal(value = TemporalType.DATE)
@Column(nullable = false, name = "CREATED")
public Date getCreated() {
    return created;
}

public void setCreated(Date created) {
    this.created = created;
}

@Transient
public String getDateCreatedString() {
    return dateCreatedString;
}

public void setDateCreatedString(String dateCreatedString) {
    this.dateCreatedString = dateCreatedString;
} 

等于和哈希码

xml for spring

    <bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="/WEB-INF/databaseConfiguration.properties"/>
</bean>

<bean  id="newsDAO"
    class="com.epam.testapp.database.NewsDaoJPA"> 
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>


<bean id="entityManagerFactory" class=
  "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
            <property name="showSql" value="true"/>     
            <property name="database" value="ORACLE"/>
        </bean>
    </property>
    <property name="loadTimeWeaver">
        <bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"/>
    </property>
</bean>

<bean id="dataSource" 
 class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driver}"/>
    <property name="url" value="${database.url}"/>
    <property name="username" value="${database.user}"/>
    <property name="password" value="${database.password}"/>
</bean>

<bean id="transactionManager" 
 class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    <property name="dataSource" ref="dataSource"/>
</bean>

但是当调用dao方法时,例如

public List<News> getList() throws DAOException {
    return getJpaTemplate().find("select n from News n");
}

我得到了

java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:423)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:485)
at $Proxy4.createEntityManager(Unknown Source)
at org.springframework.orm.jpa.EntityManagerFactoryAccessor.createEntityManager(EntityManagerFactoryAccessor.java:116)
at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:176)
at org.springframework.orm.jpa.JpaTemplate.contains(JpaTemplate.java:245)
at com.epam.testapp.database.NewsDaoJPA.getList(NewsDaoJPA.java:26)
at com.epam.testapp.presentation.action.NewsAction.list(NewsAction.java:95)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
at org.apache.struts.actions.LookupDispatchAction.execute(LookupDispatchAction.java:146)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.epam.testapp.utils.filter.EncodingFilter.doFilter(EncodingFilter.java:45)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)

有一些想法吗?

1 个答案:

答案 0 :(得分:1)

您的loadTimeWeaver是SimpleLoadTimeWeaver,通常用于测试环境。来自javadocs

  

构建和公开a的LoadTimeWeaver   SimpleInstrumentableClassLoader。

     

主要用于测试环境,足以满足   在新创建的ClassLoader上执行所有类转换   实例

根据Spring JPA手册中的this entry,我怀疑你需要更像这样的东西:

<context:load-time-weaver/>
<bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
            <property name="showSql" value="true"/>     
            <property name="database" value="ORACLE"/>
        </bean>
    </property>
</bean>

并且您根据this可能必须修改tomcat上下文以告诉tomcat使用自定义类加载器:

<Context path="/myWebApp" docBase="/my/webApp/location">
    <Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
</Context>