开发者

Controlling result list from org.hibernate.Query.list when using joined subclass

开发者 https://www.devze.com 2023-02-12 08:49 出处:网络
I have a User class, which is mapped to the \"USERS\" table in database with Hibernate. There is also a class ApplicationUser, which extends the User class and is mapped as a joined subclass in Hibern

I have a User class, which is mapped to the "USERS" table in database with Hibernate. There is also a class ApplicationUser, which extends the User class and is mapped as a joined subclass in Hibernate to database table "APPLICATIONUSERS". Like this:


public class User implements Serializable {
   //properties, getters, setters omitted for readability
}

public class ApplicationUser extends User implements Serializable {
   //properties, getters, setters omitted for readability
}

<hibernate-mapping package="com.initech.domain">
 <class name="com.initech.domain.User" table="USERS">
   <!-- properties omitted for readability -->
   <joined-subclass name="com.initech.domain.ApplicationUser" table="APPLICATIONUSERS">
    <!-- properties omitted for readability -->
   </joined-subclass>        
  </class>
</hibernate-mapping>

The problem I have is that when I get a list of users (some of them may be "normal" users and some application users), everything works fine - I get a List object, where each item is eith开发者_Go百科er a User or ApplicationUser object. The method (simplified a bit, may contain typos):


public  List findUsersByLoginName(String argLoginName){
  StringBuilder sb = new StringBuilder("from User user where lower(user.loginName) = :argLoginName");
  Query myQuery = this.sessionFactory.getCurrentSession().createQuery(sb.toString());
  myQuery.setString("argLoginName", argLoginName.toLowerCase());
  List userList = myQuery.list();
  return userList;
}

However, if there is only one user found, who is an ApplicationUser, the list contains two objects: one User object and one ApplicationUser object, representing the same user. This is a problem e.g. when the calling method is expecting each list item to be a different, unique User object.

Is there some alternative to checking for this case manually, e.g. using some other method of returning a list, where each object represents a different user also when the list size is one?

0

精彩评论

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

关注公众号