cecce88
05-08-2011, 14:30
Salve raga, ho un piccolo problema con HIBERNATE. Quando il controller viene chiamato per eliminare un Poiext dal DB, la pagina di "SucceView" mi compare correttamente, mentre il Poiext non viene cancellato dal DB.
Forse dovrei usare le transazioni\commit ma non mi parte!!
Attendo un vostro aiuto
Grazie in anticipo
Posto qui i file:
PoiextDaoImpl.java
package dao;
import dto.Poiext;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.hql.ast.util.SessionFactoryHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/** @author vincenzo */
@Component(value = "poiextDao")
public class PoiextDaoImpl implements PoiextDao {
@Autowired
private SessionFactory sessionFactory;
public void insert(Poiext poi){
Session session= sessionFactory.getCurrentSession();
session.saveOrUpdate(poi);
}
public void delete(int idpoiext){
String query = "delete from Poiext where idpoiext = "+idpoiext;
Session session= sessionFactory.getCurrentSession();
session.createQuery(query);
}
}
Poiext.java
package dto;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/** @author vincenzo */
@Entity
@Table(name = "poiext")
public class Poiext implements Serializable{
@Id
@Column(name = "idpoiext")
private int idpoiext;
@Column(name = "latitudine")
private float latitudine;
@Column(name = "longitudine")
private float longitudine;
@Column(name = "nome")
private String nome;
@Column(name = "info")
private String info;
@Column(name = "img")
private String img;
@Column(name = "indirizzo")
private String indirizzo;
@Column(name = "zona")
private int zona;
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getIndirizzo() {
return indirizzo;
}
public void setIndirizzo(String indirizzo) {
this.indirizzo = indirizzo;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public int getZona() {
return zona;
}
public void setZona(int zona) {
this.zona = zona;
}
public int getIdpoiext() {
return idpoiext;
}
public void setIdpoiext(int idpoiext) {
this.idpoiext = idpoiext;
}
public float getLatitudine() {
return latitudine;
}
public void setLatitudine(float latitudine) {
this.latitudine = latitudine;
}
public float getLongitudine() {
return longitudine;
}
public void setLongitudine(float longitudine) {
this.longitudine = longitudine;
}
}
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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Dice di reperire la configurazione di JDBC nel file /WEB-INF/jdbc.properties -->
<context:annotation-config />
<!-- Bean dedicato a leggere le proprietà di connessione contenute all'interno
del file jdbc.properties -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/poi" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- Inietta il dataSource nella sessionFactory -->
<property name="dataSource" ref="dataSource" />
<!-- Inietta le annotatedClasses cioè quali classi sono da usare per le
operazioni di CRUD (Create, Read, Update & Delete) nel db, al
caricamento dell'applicazione hibernate se non trova le tabelle se
le crea automaticamente in base ai dati nella tue classi -->
<property name="annotatedClasses">
<list>
<value>dto.Poiext</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.use_sql_comments">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
</props>
</property>
</bean>
<!-- Enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" />
<!-- The services are autodetected labeled with the @Service annotation -->
<context:component-scan base-package="service" />
<!-- The daos are autodetected labeled with the @Component annotation -->
<context:component-scan base-package="dao" />
<bean id="poiextService" class="service.PoiextServiceImpl" />
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
<bean id="insertpoiextController" class="controller.InsertpoiextController"
p:poiService-ref="poiextService" />
<bean id="deletepoiextController" class="controller.DeletepoiextController"
p:poiService-ref="poiextService" />
</beans>
PoiextServiceImpl.java
package service;
import dto.Poiext;
import dao.PoiextDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/** @author vincenzo */
@Service(value = "poiextService")
@Transactional(readOnly = false)
public class PoiextServiceImpl implements PoiextService {
/* Richiama ed inietta un PoiDao */
@Autowired
@Qualifier("poiextDao")
private PoiextDao poiextDao;
/* Implementa il metodo astratto dell'interfaccia PoiService: invoca il
* metodo insert sul dao corrente e gli passa l'oggetto poi ricevuto come
* parametro di input */
public void insertPOI(Poiext poi){
poiextDao.insert(poi);
}
//public Poiext searchById(int idpoiext){
// return this.poiextDao.searchById(idpoiext);
//}
public void deletePOI(int idpoiext){
poiextDao.delete(idpoiext);
}
}
DeletepoiextController.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import service.*;
import dto.*;
public class DeletepoiextController extends SimpleFormController {
private PoiextService poiService;
public void setPoiService(PoiextService poiService){
this.poiService = poiService;
}
public DeletepoiextController() {
setCommandClass(Poiext.class);
setCommandName("poi");
setSuccessView("helloviewdelete");
setFormView("deletepoiext");
}
@Override
protected ModelAndView onSubmit(Object command) throws Exception {
Poiext poi = (Poiext) command;
int idpoiext=poi.getIdpoiext();
this.poiService.deletePOI(idpoiext);
ModelAndView mv = new ModelAndView(getSuccessView());
return mv;
}
}
Forse dovrei usare le transazioni\commit ma non mi parte!!
Attendo un vostro aiuto
Grazie in anticipo
Posto qui i file:
PoiextDaoImpl.java
package dao;
import dto.Poiext;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.hql.ast.util.SessionFactoryHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/** @author vincenzo */
@Component(value = "poiextDao")
public class PoiextDaoImpl implements PoiextDao {
@Autowired
private SessionFactory sessionFactory;
public void insert(Poiext poi){
Session session= sessionFactory.getCurrentSession();
session.saveOrUpdate(poi);
}
public void delete(int idpoiext){
String query = "delete from Poiext where idpoiext = "+idpoiext;
Session session= sessionFactory.getCurrentSession();
session.createQuery(query);
}
}
Poiext.java
package dto;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/** @author vincenzo */
@Entity
@Table(name = "poiext")
public class Poiext implements Serializable{
@Id
@Column(name = "idpoiext")
private int idpoiext;
@Column(name = "latitudine")
private float latitudine;
@Column(name = "longitudine")
private float longitudine;
@Column(name = "nome")
private String nome;
@Column(name = "info")
private String info;
@Column(name = "img")
private String img;
@Column(name = "indirizzo")
private String indirizzo;
@Column(name = "zona")
private int zona;
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getIndirizzo() {
return indirizzo;
}
public void setIndirizzo(String indirizzo) {
this.indirizzo = indirizzo;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public int getZona() {
return zona;
}
public void setZona(int zona) {
this.zona = zona;
}
public int getIdpoiext() {
return idpoiext;
}
public void setIdpoiext(int idpoiext) {
this.idpoiext = idpoiext;
}
public float getLatitudine() {
return latitudine;
}
public void setLatitudine(float latitudine) {
this.latitudine = latitudine;
}
public float getLongitudine() {
return longitudine;
}
public void setLongitudine(float longitudine) {
this.longitudine = longitudine;
}
}
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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Dice di reperire la configurazione di JDBC nel file /WEB-INF/jdbc.properties -->
<context:annotation-config />
<!-- Bean dedicato a leggere le proprietà di connessione contenute all'interno
del file jdbc.properties -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/poi" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- Inietta il dataSource nella sessionFactory -->
<property name="dataSource" ref="dataSource" />
<!-- Inietta le annotatedClasses cioè quali classi sono da usare per le
operazioni di CRUD (Create, Read, Update & Delete) nel db, al
caricamento dell'applicazione hibernate se non trova le tabelle se
le crea automaticamente in base ai dati nella tue classi -->
<property name="annotatedClasses">
<list>
<value>dto.Poiext</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.use_sql_comments">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
</props>
</property>
</bean>
<!-- Enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" />
<!-- The services are autodetected labeled with the @Service annotation -->
<context:component-scan base-package="service" />
<!-- The daos are autodetected labeled with the @Component annotation -->
<context:component-scan base-package="dao" />
<bean id="poiextService" class="service.PoiextServiceImpl" />
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
<bean id="insertpoiextController" class="controller.InsertpoiextController"
p:poiService-ref="poiextService" />
<bean id="deletepoiextController" class="controller.DeletepoiextController"
p:poiService-ref="poiextService" />
</beans>
PoiextServiceImpl.java
package service;
import dto.Poiext;
import dao.PoiextDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/** @author vincenzo */
@Service(value = "poiextService")
@Transactional(readOnly = false)
public class PoiextServiceImpl implements PoiextService {
/* Richiama ed inietta un PoiDao */
@Autowired
@Qualifier("poiextDao")
private PoiextDao poiextDao;
/* Implementa il metodo astratto dell'interfaccia PoiService: invoca il
* metodo insert sul dao corrente e gli passa l'oggetto poi ricevuto come
* parametro di input */
public void insertPOI(Poiext poi){
poiextDao.insert(poi);
}
//public Poiext searchById(int idpoiext){
// return this.poiextDao.searchById(idpoiext);
//}
public void deletePOI(int idpoiext){
poiextDao.delete(idpoiext);
}
}
DeletepoiextController.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import service.*;
import dto.*;
public class DeletepoiextController extends SimpleFormController {
private PoiextService poiService;
public void setPoiService(PoiextService poiService){
this.poiService = poiService;
}
public DeletepoiextController() {
setCommandClass(Poiext.class);
setCommandName("poi");
setSuccessView("helloviewdelete");
setFormView("deletepoiext");
}
@Override
protected ModelAndView onSubmit(Object command) throws Exception {
Poiext poi = (Poiext) command;
int idpoiext=poi.getIdpoiext();
this.poiService.deletePOI(idpoiext);
ModelAndView mv = new ModelAndView(getSuccessView());
return mv;
}
}