質問

I am using org.appfuse.dao.hibernatepackage and I have used all the method in the GenericDaoHibernate<T,PK> class.

I found these methods

public List<T> getAll();
public List<T> getAllDistinct();
public List<T> search(String searchTerm);
public T get(PK id);
public boolean exists(PK id);
public T save(T object);
public void remove(T object);
public void remove(PK id);
public List<T> findByNamedQuery(String queryName, Map<String, Object> queryParams);
public void reindex();
public void reindexAll(boolean async);

I have some model classes, services and methods.

Now I want to get list of object using some other fieled in the model class other than id(I have some common fields in many model classes). I need to write similar methods in all the services and daos. So i was thinking is it possible to create a common method in generic dao.

The following I tried, but it didn't work.

public T getbyClientKey(Long clientkey) {
        Session sess = getSession();
        IdentifierLoadAccess byId = sess.byId(persistentClass);
        List<T> entity = (List<T>) byId.load(clientkey);

        if (entity == null) {
            log.warn("Uh oh, '" + this.persistentClass + "' object with client '" + clientkey + "' not found...");
            throw new ObjectRetrievalFailureException(this.persistentClass, clientkey);
        }

        return entity;
    }

I knew this will be error. and it showed TypeCastingException, because return type of byId.load(id) is object only, not List.

So how can I create a method like that? If so, I think I can create method for remove() also(But that's not necessary for me now, may be in future).

役に立ちましたか?

解決

The Javadoc for IdentifierLoadAccess is pretty clear in how the load method should behave:

Return the persistent instance with the given identifier, or null if there is no such persistent instance.

This means it should return just one object, not a List of objects. Try casting it to T instead.

If you want to query your entity (that is, retrieve items by any other means than primary key), you most likely want to implement the search(String) method.

If you want to query your entity (that is, retrieve items by any other means than primary key), take a look at the UserDaoHibernate that is shipped with AppFuse. It contains a method loadUserByUsername() which is implemented like this:

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    List users = getSession().createCriteria(User.class).add(Restrictions.eq("username", username)).list();
    if (users == null || users.isEmpty()) {
        throw new UsernameNotFoundException("user '" + username + "' not found...");
    } else {
        return (UserDetails) users.get(0);
    }
}

Obviously, if you want to return all items, it should be modified a bit (this one is made up):

public List<UserDetails> loadLockedUsers() {
    List<UserDetails> users = (List<UserDetails>) getSession().createCriteria(User.class).add(Restrictions.eq("account_locked", true)).list();
    return users;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top