簡體   English   中英

GWT Spring Hibernate Jpa,自動連接依賴項注入失敗

[英]GWT Spring Hibernate Jpa, Injection of autowired dependencies failed

我創建了一個GWT-Spring-Hibernate-JPA Web應用程序。 當我僅使用一個數據庫表時,它就可以工作。 但是當我創建一對多關系時,我看到一個錯誤!

        [WARN] Failed startup of context com.google.gwt.dev.shell.jetty.JettyLauncher$WebAppContextWi thReload@626ef808{/,F:\Documents\desktop\WorkSpace\RssReader\war}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rssChanelDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: javax.persistence.EntityManagerFactory com.javacodegeeks.server.dao.RssChanelDAO.entityManagerFacto ry; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: Explicit persistence provider error(s) occurred for "MyPersistenceUnit" after trying the following discovered implementations: org.datanucleus.api.jpa.PersistenceProviderImpl, org.hibernate.ejb.HibernatePersistence from provider: org.hibernate.ejb.HibernatePersistence

我認為文件persistence.xml或applicationContex.xml中有問題。 也許我使用注釋是錯誤的。 也許我需要使用@ Controller?

請幫助我修復代碼。

applicationContex.xml

<beans >

 <context:component-scan base-package="com.javacodegeeks" />

 <!-- pour les @Service
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Service" />

        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
 </context:component-scan>-->
 <tx:annotation-driven />

 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
  <property name="persistenceUnitName" value="MyPersistenceUnit" />
 </bean>

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

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_2_0.xsd"
 version="2.0">

 <persistence-unit name="MyPersistenceUnit" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>

  <properties>
   <property name="hibernate.hbm2ddl.auto" value="update" />
   <property name="hibernate.show_sql" value="false" />
   <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
   <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
   <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/User" />
   <property name="hibernate.connection.username" value="postgres" />
   <property name="hibernate.connection.password" value="1111" />

   <property name="hibernate.c3p0.min_size" value="5" />
   <property name="hibernate.c3p0.max_size" value="20" />
   <property name="hibernate.c3p0.timeout" value="300" />
   <property name="hibernate.c3p0.max_statements" value="50" />
   <property name="hibernate.c3p0.idle_test_period" value="3000" />

  </properties>

 </persistence-unit>

</persistence>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
              http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee">

   <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>


  <listener>
 <listener-class>
  org.springframework.web.context.ContextLoaderListener
 </listener-class>
</listener>
<servlet>
 <servlet-name>springGwtRemoteServiceServlet</servlet-name>
 <servlet-class>org.spring4gwt.server.SpringGwtRemoteServiceServlet
 </servlet-class>

</servlet>


<servlet-mapping>

  <servlet-name>springGwtRemoteServiceServlet</servlet-name>
 <url-pattern>/gwtspringtestnomaven/springGwtServices/*</url-pattern>

</servlet-mapping> 

  <!-- Default page to serve -->
  <welcome-file-list>
    <welcome-file>Gwtspringtestnomaven.html</welcome-file>
  </welcome-file-list>

</web-app>

RsChanelDTO:

@Entity
@Table(name = "RSS_CHANEL")
public class RssChanelDTO implements java.io.Serializable {

 private static final long serialVersionUID = 7440297955003302414L;

 @Id
 @GeneratedValue(generator="increment")
 @GenericGenerator(name="increment", strategy = "increment")
 @Column(name="chanel_id")
 private long id;

 @Column(name="chanel_name", nullable = false, length=30)
 private String chanelName;

 @Column(name="chanel_url", nullable = false, length=30)
 private String chanelUrl;

 @OneToMany
 @JoinTable(name = "RSS_NEWS")
 Set<RssNewsDTO> news = new HashSet<RssNewsDTO>();

// setters, getters

RssNewsDTO:

@Entity
@Table(name = "RSS_NEWS")
public class RssNewsDTO {

    @Id
    @GeneratedValue(generator="increment")
    @GenericGenerator(name="increment", strategy = "increment")
    @Column(name="news_id")
    private Long id;  
    @Column(name="news_name", nullable = false, length=30)
    private String newsName;
    @Column(name="news_title", nullable = false, length=400)
    private String title; 
    @Column(name="news_link", nullable = false, length=50)
    private String link; 

    @ManyToOne
    @JoinTable(name = "RSS_CHANEL")
    private RssChanelDTO rssch;

// getters , setters

RssChanelDAO:

@Repository("rssChanelDAO")
public class RssChanelDAO extends JpaDAO<Long, RssChanelDTO> {

 @Autowired
 EntityManagerFactory entityManagerFactory;

 @PostConstruct
 public void init() {
  super.setEntityManagerFactory(entityManagerFactory);
 }
  }

RssNewsDAO:

@Repository("rssNewsDAO")
public class RssNewsDAO extends JpaDAO<Long, RssNewsDTO> {

 @Autowired
 EntityManagerFactory entityManagerFactory;

 @PostConstruct
 public void init() {
  super.setEntityManagerFactory(entityManagerFactory);
 }

}

RssChanelServiceImpl(類似於RssNewsServiceImpl)

@Service("rssChanelService")

public class RssChanelServiceImpl implements RssChanelService { 

 @Autowired 
 private RssChanelDAO rssChanelDAO; 

 @PostConstruct 
 public void init() throws Exception { 
 } 

 @PreDestroy 
 public void destroy() { 
 } 

 public RssChanelDTO findRssChanel(long id) { 

  return rssChanelDAO.findById(id); 

 } 

 @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
 public void saveRssChanel(String chanelName, String chanelUrl) throws Exception { 

   RssChanelDTO rssChanelDTO = new RssChanelDTO(chanelName,chanelUrl); 
   rssChanelDAO.persist(rssChanelDTO); 
  //....
 }

該錯誤可能是由於某些JPA映射問題所致。 我發現有人認為這可能不是您想要的,但是我不確定這是否是導致異常的原因。

在映射中,您可能想在RssNewsDTORssChanelDTORssChanelDTO RssNewsDTO.rssch <-> Set<RssNewsDTO> RssChanelDTO.news )之間RssNewsDTO雙向關系。 但是,您映射的是兩個不同的,不相關的關系:1:N和N:1關系。

如果希望它們是雙向關系,請使用以下映射:

@Entity
@Table(name = "RSS_CHANEL")
public class RssChanelDTO implements java.io.Serializable {
    ...

    @OneToMany(mappedBy="rssch")
    Set<RssNewsDTO> news = new HashSet<RssNewsDTO>();
}


@Entity
@Table(name = "RSS_NEWS")
public class RssNewsDTO {
//try implements java.io.Serializable
    ...
    @ManyToOne
    @JoinTable(name = "RSS_CHANEL")
    private RssChanelDTO rssch;
}

然后RssNewsDTO.rssch是控制(保存)關系船的一方。 RssChanelDTO.news是只讀的)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM