开发者

Spring+JPA+DAO+Tomcat

persistence.xml

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

News

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;
} 

equals and hashcode

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>

but when call dao method for example

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

i get

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.pr开发者_如何转开发ocess(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)

Have some idea?


Your loadTimeWeaver is a SimpleLoadTimeWeaver, which is usually used in testing environments. From the javadocs

LoadTimeWeaver that builds and exposes a SimpleInstrumentableClassLoader.

Mainly intended for testing environments, where it is sufficient to perform all class transformation on a newly created ClassLoader instance.

According to this entry in the manual on Spring JPA I suspect you'll need something more like this:

<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>

and you according to this may have to modify the tomcat context to tell tomcat to use a custom classloader:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜