Изучаю вот такую связку. Перечитал много туториалов, пересмотрел примеров, вроде бы все понятно. Написал Вэб-приложение по следующей статье: , немного подкорректировал под свои нужды, все заработало. Но когда я попытался ввести отношение между таблицами, а именно добавил еще одну DTO, Service и DAO, компилятор начал ругаться:
[WARN] Failed startup of context com.google.gwt.dev.shell.jetty.JettyLauncher$WebAppContextWithReload@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.entityManagerFactory; 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
Для одной таблицы - всё работает!
Я понимаю что проблема где-то в конфигурации, думаю что нужно где-то писать @Controller или еще чего-то... Подскажите, пожалуйста, опытные и мудрые люди!! (:
Вот мои листинги:
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>();
// сеттеры и геттеры
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;
// сеттеры и геттеры
JpaDAO:
public abstract class JpaDAO<K, E> extends JpaDaoSupport {
protected Class<E> entityClass;
@SuppressWarnings("unchecked")
public JpaDAO() {
ParameterizedType genericSuperclass = (ParameterizedType) getClass()
.getGenericSuperclass();
this.entityClass = (Class<E>) genericSuperclass
.getActualTypeArguments()[1];
}
// и другие методы
}
public Integer removeAll() {
return (Integer) getJpaTemplate().execute(new JpaCallback() {
public Object doInJpa(EntityManager em) throws PersistenceException {
Query q = em.createQuery("DELETE FROM " +
entityClass.getName() + " h");
return q.executeUpdate();
}
});
}
}
RssChanelService:
@RemoteServiceRelativePath("springGwtServices/rssChanelService")
public interface RssChanelService extends RemoteService {
public RssChanelDTO findRssChanel(long id);
public void saveRssChanel(String chanelName, String chanelUrl) throws Exception;
public void updateRssChanel(long Id, String chanelName, String chanelUrl) throws Exception;
public void saveOrUpdateRssChanel(String chanelName, String chanelUrl) throws Exception;
public void deleteRssChanel(long id) throws Exception;
public void updateNewsOnRssChanel(long parseLong);
}
RssNewsService:
@RemoteServiceRelativePath("springGwtServices/rssNewsService")
public interface RssNewsService extends RemoteService {
public RssNewsDTO findRssNews(long id);
public void saveRssNews(String newsName, String title, String link,RssChanelDTO rssch) throws Exception;
public void updateRssNews(long Id, String newsName, String title, String link,RssChanelDTO rssch) throws Exception;
public void saveOrUpdateRssNews(String newsName, String title, String link,RssChanelDTO rssch) throws Exception;
public void deleteRssNews(long id) throws Exception;
}
Кусочек из 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);
//....
}
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);
}
}
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>
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>
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>
Подскажите, где нужно подправить, для того что-бы заработало...