Answer the question
In order to leave comments, you need to log in
GWT+Spring+Hibernate deal with JPA?
I'm studying this link. I re-read a lot of tutorials, revised the examples, everything seems to be clear. Wrote a Web application according to the following article: , slightly corrected it for my needs, everything worked. But when I tried to introduce a relationship between tables, namely, I added one more DTO, Service and DAO, the compiler started to swear:
[WARN] Failed startup of context com.g[email protected]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.
@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>();
// сеттеры и геттеры
@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;
// сеттеры и геттеры
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();
}
});
}
}
@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);
}
@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;
}
@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);
//....
}
@Repository("rssChanelDAO")
public class RssChanelDAO extends JpaDAO<Long, RssChanelDTO> {
@Autowired
EntityManagerFactory entityManagerFactory;
@PostConstruct
public void init() {
super.setEntityManagerFactory(entityManagerFactory);
}
}
@Repository("rssNewsDAO")
public class RssNewsDAO extends JpaDAO<Long, RssNewsDTO> {
@Autowired
EntityManagerFactory entityManagerFactory;
@PostConstruct
public void init() {
super.setEntityManagerFactory(entityManagerFactory);
}
}
<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>
<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>
<?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>
Answer the question
In order to leave comments, you need to log in
Instead of "friending" frameworks yourself, especially without rich experience, try to take a ready-made meta-framework, such as https://www.cuba-platform.com/ for example.
What are the advantages:
1. It works and there is support
2. the cube is open source
3. you look at you and you won’t need anything else
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question