简体   繁体   English

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

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

I created a GWT-Spring-Hibernate-JPA web application. 我创建了一个GWT-Spring-Hibernate-JPA Web应用程序。 When I use only one database table, it works. 当我仅使用一个数据库表时,它就可以工作。 But when I created a one-to-many relationship, I see an error! 但是当我创建一对多关系时,我看到一个错误!

        [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

I think something is wrong in the file persistence.xml, or applicationContex.xml. 我认为文件persistence.xml或applicationContex.xml中有问题。 Maybe I'm wrong to use annotations. 也许我使用注释是错误的。 Maybe I need to use @ Controller? 也许我需要使用@ Controller?

Please help me fix them code. 请帮助我修复代码。

applicationContex.xml 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.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 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: 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: 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: RssChanelDAO:

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

 @Autowired
 EntityManagerFactory entityManagerFactory;

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

RssNewsDAO: RssNewsDAO:

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

 @Autowired
 EntityManagerFactory entityManagerFactory;

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

}

RssChanelServiceImpl (is similar to RssNewsServiceImpl ) 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); 
  //....
 }

The error seams to be because of some JPA mapping problem. 该错误可能是由于某些JPA映射问题所致。 I found one think that is likely not what you want, but I am not sure if this is the cause for the exception. 我发现有人认为这可能不是您想要的,但是我不确定这是否是导致异常的原因。

In your mapping it is likely that you want to have an bidirectional relationship between RssNewsDTO and RssChanelDTO ( RssChanelDTO RssNewsDTO.rssch <--> Set<RssNewsDTO> RssChanelDTO.news ). 在映射中,您可能想在RssNewsDTORssChanelDTORssChanelDTO RssNewsDTO.rssch <-> Set<RssNewsDTO> RssChanelDTO.news )之间RssNewsDTO双向关系。 But what you mapped are two different, not related relations, a 1:N and a N:1 relation. 但是,您映射的是两个不同的,不相关的关系:1:N和N:1关系。

If you want them to be a bidirectional relationship then use this mapping: 如果希望它们是双向关系,请使用以下映射:

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

Then RssNewsDTO.rssch is the side that controlles (save) the relation ship. 然后RssNewsDTO.rssch是控制(保存)关系船的一方。 ( RssChanelDTO.news is more or less read only) RssChanelDTO.news是只读的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM