Wednesday, 11 September 2013

Spring 3 Hibernate 4 and Jsf Integration not working

Spring 3 Hibernate 4 and Jsf Integration not working

I am trying of integration spring 3.2 hibernate 4.1 and jsf 2.1, but when
i run application and i do click in search does not go into the service
layer and dao. This's my code
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
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_3_0.xsd"
version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-
class>
</listener>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd"
xmlns:cache="http://www.springframework.org/schema/cache">
<tx:annotation-driven transaction-manager="transactionManager"/>
<context:component-scan
base-package="com.example" />
<context:property-placeholder
location="classpath:application.properties"/>
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/example"/>
<property name="lookupOnStartup" value="false"/>
<property name="cache" value="true" />
<property name="proxyInterface" value="javax.sql.DataSource" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:dataSource-ref="dataSource"
p:packagesToScan="com.example.entity">
<property name="hibernateProperties">
<props>
<prop
key="hibernate.dialect">${jdbc.hibernate.dialect}
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop
key="hibernate.cache.use_second_level_cache">true</prop>
<prop
key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop
key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
<prop
key="hibernate.cache.provider_configuration_file_resource_path">classpath:ehcache.xml</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory">
</bean>
</beans>
entity Chart
public class Chart {
private String status;
private int count;
public Chart() {
}
public Chart(String status, int count) {
this.status = status;
this.count = count;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
interface dao
public interface IChartDAO {
List<Chart> getData();
}
implements dao
@Repository("chartDAOImpl")
public class ChartDAOImpl implements IChartDAO, Serializable {
@Autowired
SessionFactory sessionFactory;
Session session;
@Override
public List<Chart> getData() {
List<Chart> dataList = null;
try {
session = sessionFactory.openSession();
session.beginTransaction();
Query query = session.createSQLQuery("query")
.addScalar("status").addScalar("count")
.setResultTransformer(Transformers.aliasToBean(Chart.class));
dataList = query.list();
session.getTransaction().commit();
} catch (Exception e) {
System.out.println(e.getMessage());
session.getTransaction().rollback();
}
finally
{
session.close();
}
return dataList;
}
interface service
public interface IChartService {
List<ChartPie> getData();
}
implements service
@Service("chartServiceImpl")
@Transactional(readOnly=true)
public class ChartServiceImpl implements IChartService, Serializable {
@Autowired
IChartDAO chartDao;
List<Chart> dataList;
@Transactional(propagation= Propagation.REQUIRED, readOnly=true)
@Override
public List<Chart> getData() {
try {
dataList = chartDao.getData();
} catch (Exception e) {
System.out.println("fallo service: "+e.getMessage());
}
return dataList;
}
my bo
public class ChartBO implements Serializable {
@Autowired
IChartService chartService;
List<ChartPie> dataList;
public List<Chart> getData() {
try {
dataList = chartService.getData();
} catch (Exception e) {
}
return dataList;
}
jsf bean
@ManagedBean(name = "beanChart")
@ViewScoped
public class ChartBean implements Serializable {
private PieChartModel data;
ChartBO chartBo;
public PieChartModel getData() {
return data;
}
public ChartBean() {
chartBo = new ChartBO();
data = new PieChartModel();
buildData();
}
public void buildData() {
try {
List<Chart> dataList = chartBo.getData();
for (Chart cq : dataList) {
data.set(cq.getStatus(), cq.getCount());
}
} catch (Exception e) {
System.out.println("Fallo en bean: "+e.getMessage());
}
}

No comments:

Post a Comment