2014年2月7日星期五

Hibernate problem

My program select statement can be issued , but will not issue a delete and update statements , nor error , neighborhoods you probably why.
This is my program
package com.zhg.oa.base;

import java.lang.reflect.ParameterizedType;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

@SuppressWarnings("unchecked")
public class DaoSupportImpl<T> implements DaoSupport<T> {

@Resource
private SessionFactory sessionFactory;

Class<T> clazz;

public DaoSupportImpl() {
// 使用反射技术得到T的真实类型
ParameterizedType pt = (ParameterizedType) this.getClass()
.getGenericSuperclass();// 获取当前new的对象的泛型的父类类型
this.clazz = (Class<T>) pt.getActualTypeArguments()[0];
}

protected Session getSession() {
return sessionFactory.getCurrentSession();
}

@Override
public void save(T entity) {
getSession().save(entity);
}

@Override
public void delete(Long id) {
Object obj = getById(id);
System.out.println(obj);
if (obj != null) {
getSession().delete(obj);
System.out.println("====================");
}

}

@Override
public void update(T entity) {
System.out.println("====================");
getSession().update(entity);
System.out.println("==============================");
}

@Override
public T getById(Long id) {
if (id == null) {
return null;
} else {
return (T) getSession().get(clazz, id);
}
}

@Override
public List<T> getByIds(Long[] ids) {
return getSession().createQuery( //
"FROM " + clazz.getSimpleName() + " WHERE id IN (:ids)") //
.setParameter("ids", ids)//
.list();
}

@Override
public List<T> findAll() {
return getSession().createQuery( //
"FROM " + clazz.getSimpleName()) //
.list();
}
}

------ Solution ------------------------------------- -------
can send select, the greatest likelihood is that the relationship and transaction .
------ For reference only -------------------------------------- -
    public void delete(Long id) {
        Object obj = getById(id);
System.out.println(obj);
        if (obj != null) {
            getSession().delete(obj);
System.out.println("====================");
        }
    }
    @Override
    public void update(T entity) {
System.out.println("====================");
        getSession().update(entity);
System.out.println("==============================");
    }

these two codes , " ============ " All energy output, but the delete and update statements directly ignored, there is no data in the database changes
----- - For reference only ---------------------------------------

Well, yes forgot to add affairs , and preceded by the @ Transactional just fine , thank you
------ For reference only ---------------------------------------

没有评论:

发表评论