GWT Spring Hibernate Jpa,自动连接依赖项注入失败

时间:2013-12-03 20:03:09

标签: java spring hibernate gwt jpa

我创建了一个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>

RssChanelDTO:

@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); 
  //....
 }

1 个答案:

答案 0 :(得分:0)

由于一些JPA映射问题,错误接缝。我发现有人认为这可能不是你想要的,但我不确定这是否是造成例外的原因。

在您的映射中,您可能希望在RssNewsDTORssChanelDTORssChanelDTO RssNewsDTO.rssch&lt; - &gt; Set<RssNewsDTO> RssChanelDTO.news)之间建立双向关系。但你所绘制的是两种不同的,不相关的关系,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或多或少只读)