无法在spring boot中更新数据hibernateTemplate.update()

时间:2017-04-15 11:39:39

标签: spring hibernate java-ee spring-boot

我正在开发java web项目。但我无法在春季启动时使用更新方法。我的代码如下所示。我需要你的想法。怎么能解决这个问题?我已经感谢你所有的想法了。好工作:))

AppConfig.java



import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.toyota.project.dao.ITravelsDao;
import com.toyota.project.dao.TravelsDao;
import com.toyota.project.data.Travels;

@Configuration
@EnableTransactionManagement
public class TravelsAppConfig {
    @Bean
    public ITravelsDao personDao() {
        return new TravelsDao();
    }
    @Bean
    public HibernateTemplate hibernateTemplate() {
        return new HibernateTemplate(sessionFactory());
    }

    @Bean
    public SessionFactory sessionFactory() {
        return new LocalSessionFactoryBuilder(getDataSource())
                .addAnnotatedClasses(Travels.class)
                .buildSessionFactory();
    }
    @Bean
    public DataSource getDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("org.postgresql.Driver");
        dataSource.setUrl("jdbc:postgresql://localhost:5432/toyota");
        dataSource.setUsername("postgres");
        dataSource.setPassword("1234");
        return dataSource;
    }
    @Bean
    public HibernateTransactionManager hibTransMan(){
        return new HibernateTransactionManager(sessionFactory());
    }

}




TravelDao.java



import javax.transaction.Transactional;

import com.toyota.project.data.Travels;
import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.hibernate.criterion.Restrictions;
import org.hibernate.Criteria;

import java.util.List;
import  java.util.Date;
import java.util.Queue;

@Transactional
public class TravelsDao extends AbstractDao implements ITravelsDao{

    @Autowired
    private HibernateTemplate hibernateTemplate;

    public void save(Travels travels){
        hibernateTemplate.save(travels);
    }

    public List<Travels> findTravels(String username){
        Criteria criteria = getSession().createCriteria(Travels.class);
        criteria.add( Restrictions.like("username", username) );
        return (List<Travels>) criteria.list();
    }
    public void update(Travels travels){
        hibernateTemplate.update("Travels",travels);
    }
    public List<Travels> findAllTravels(){
        Criteria criteria = getSession().createCriteria(Travels.class);
        return (List<Travels>) criteria.list();
    }
}
&#13;
&#13;
&#13;

TravelController.java

&#13;
&#13;
public void Update(String username, String placeTravel, String purposeGoing, String projectCode, String travelStart, String travelFinish,int count) throws ParseException {
        ctx = new AnnotationConfigApplicationContext();
        ctx.register(TravelsAppConfig.class);
        ctx.refresh();
        ITravelsDao tdao=ctx.getBean(ITravelsDao.class);
        List<Travels> travels=tdao.findTravels(username);
        Travels travels1=new Travels();
        DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        Date start = df.parse(travelStart);
        Date finish = df.parse(travelFinish);
        travels1=travels.get(count);
            if(placeTravel!=travels.get(count).getTravelPlace()){
                travels1.setTravelPlace(placeTravel);
            }
            else if(purposeGoing!=travels.get(count).getPurpose()){
                travels1.setPurpose(purposeGoing);
            }
            else if(projectCode!=travels.get(count).getProjectCode()){
                travels1.setProjectCode(projectCode);
            }
            else if(start!=travels.get(count).getStart()){
                travels1.setStart(start);
            }
            else if(finish!=travels.get(count).getFinish()){
                travels1.setFinish(finish);
            }
            tdao.update(travels1);
        }
&#13;
&#13;
&#13;

0 个答案:

没有答案