开发者

Working with hibernate/DAO problems

Hello everyone here is my DAO class :

public class UsersDAO extends HibernateDaoSupport {

    private static final Log log = LogFactory.getLog(UsersDAO.class);

    protected void initDao() {
        //do nothing
    }

    public void save(User transientInstance) {
        log.debug("saving Users instance");
        try {
            getHibernateTemplate().saveOrUpdate(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

public void update(User transientInstance) {
        log.debug("updating User instance");
        try {
            getHibernateTemplate().update(transientInstance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public void delete(User persistentInstance) {
        l开发者_JS百科og.debug("deleting Users instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public User findById( java.lang.Integer id) {
        log.debug("getting Users instance with id: " + id);
        try {
            User instance = (User) getHibernateTemplate()
                    .get("project.hibernate.Users", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

}

Now I wrote a test class(not a junit test) to test is everything working, my user has these fields in the database : userID which is 5characters long string and unique/primary key, and fields such as address, dob etc(total 15 columns in database table). Now in my test class I intanciated User added the values like :

User user = new User;
user.setAddress("some address");

and so I did for all 15 fields, than at the end of assigning data to User object I called in DAO to save that to database UsersDao.save(user); and save works just perfectly. My question is how do I update/delete users using the same logic?

Fox example I tried this(to delete user from table users):

User user = new User;
user.setUserID("1s54f"); // which is unique key for users no two keys are the same
UsersDao.delete(user); 

I wanted to delete user with this key but its obviously different can someone explain please how to do these. thank you

UPDATE :

Do I need to set all 15 fields on User object to delete it like I did with save method ?


Having not looked at Hibernate for quite a while now, I can only hazard a guess at the problem.

It seems that you are creating a User object, but only populating the User ID field, so the persistence layer knows nothing about the actual User.

I would recommend using a retrieve function that can find the User with the given ID, and then pass that User into the delete method.

User u = UsersDao.findById("1s54f");
UsersDao.delete(u);

This should work, as the persistence layer will know about the User, so it has all of the details it needs to perform the delete.

However, a more efficient method would be to find a way of deleting a user by ID, so you do not have to query the database to get the instance of the User and then delete it.

Hope this helps.

Chris


In an ideal world you will have your model's business key as the database primary key and you'll not have this problem. But it ain't so, isn't it?

For you particular problem if you are very much sure that the userID is going to be unique then you can try this (taken from here):

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

String hqlDelete = "delete User u where u.userID = :id";

int deletedEntities = s.createQuery( hqlDelete )
        .setString( "id", userID )
        .executeUpdate();
tx.commit();
session.close();

But let me warn you. This kind of code is not good at all. For example what happens if you decide in future that the column you used in delete is no longer unique? Then you'll run into a very serious bug or a very bad case of refactoring. Either way the fool-proof (may not be efficient & may not be feasible) way is to delete records based on their primary key.


Check out the documentation. Get used to the concept of persistent, transient, and detached instances. To delete an instance, you call

session.delete(persistentInstance)

and to update (although you probably shouldn't need to use it), call

persistentInstance = session.merge(detachedInstance)

Shouldn't need to use update? No, because you just need to load/find an object first, and then modify it. Any modifications you make to a persistent object will automatically be saved back to the database.


In order to delete the user that its ID is "1s54f" you should create a delete HQL as follows:

public void delete(String id) {
        log.debug("deleting Users instance");
        try {
            final String deleteQuery = "delete from User where id = :id";
            final Query query = getSession().createQuery(deleteQuery);
            final query.setString("id",id);
            final int rowCount = query.executeUpdate(); // check that the rowCount is 1
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
 }

Then you can use this method like:

userDao.delete("1s54f");

Hope this helps.


Hibernate is an object-relational mapper, that is, it translates between the world of relational databases and object-oriented Java code. Since primary keys are a database concept, it is hibernate's job to translate them into object-oriented terms (in this case: object identity).

That is, if you pass primary keys to hibernate, you are not using hibernate as intended; calling code should represent persistent data with mapped objects, not primary keys. This also allows hibernate to automatically guard against lost updates by checking version numbers.

The typical pattern therefore is to have the following signature:

interface UserDAO {
    void delete(User user);
}

and require the DAOs caller to come up with a persistent object to pass to it. The caller might have such an object lying about from the current or a previous (now closed) session, after all, he did somehow learn about its primary key. If all else fails, you can use session.load(User.class, id) to ask hibernate for a proxy to pass to the delete method. (Note that one shouldn't use session.load if the object might no longer exist in the database.)


It's not necessary fetch a whole entity before removing it, neither create a hardcoded query for delete, nor setting every field in entity.

Maybe better way to do that is setting the id for entity and use Hibernate API itself.

If a specific dao is used to entity User, as described in question, try:

public void remove(Serializable id) throws InstantiationException, IllegalAccessException {
    User user = new User();
    getSessionFactory().getClassMetadata(getEntityClass()).setIdentifier(user, id, (SessionImplementor) getSessionFactory().getCurrentSession());
    getHibernateTemplate().delete(entity);
}

As can be seen, neither unnecessary operation in database is made.

And this can be used in generic flavor if GenericDao is implemented like:

public void remove(Serializable id) throws InstantiationException, IllegalAccessException {
    Model entity = entityClass.newInstance();
    getSessionFactory().getClassMetadata(getEntityClass()).setIdentifier(entity, id, (SessionImplementor) getSessionFactory().getCurrentSession());
    getHibernateTemplate().delete(entity);
}

Both ways, Dao must extend org.springframework.orm.hibernate4.support.HibernateDaoSupport to get advantages.

Here's a fragment of generic:

public class GenericDaoImpl<Model> extends HibernateDaoSupport implements GenericDao<Model> {

   private Class<Model> entityClass;

   public GenericDaoImpl() {
       this.entityClass = (Class<Model>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    /* CRUD are implemented here  */


    public void remove(Serializable id) throws InstantiationException, IllegalAccessException {
        Model entity = entityClass.newInstance();
        getSessionFactory().getClassMetadata(getEntityClass()).setIdentifier(entity, id, (SessionImplementor) getSessionFactory().getCurrentSession());
        getHibernateTemplate().delete(entity);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜