没有懒惰地初始化一个角色集合

时间:2013-09-10 02:11:30

标签: spring hibernate dao

@Entity
@Table(name="dt_user" , uniqueConstraints = {@UniqueConstraint(columnNames="user_no"), @UniqueConstraint(columnNames="account")}
)

public class User  implements java.io.Serializable {

/**
 * 
 */
private static final long serialVersionUID = -8149578944942492965L;
// Fields    

 private long id;
 private String userNo;
 private Set<UserRelative> userRelatives = new HashSet<UserRelative>(0);
// Constructors
/** default constructor */
public User() {
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="user")
public Set<UserRelative> getUserRelatives() {
    return this.userRelatives;
}
}

以上是用户实体;下面是另一个UserRelative实体:当想要getUserRelatives()时,问题就发生了。

@Entity
@Table(name="dt_user_relative")
public class UserRelative  implements java.io.Serializable {
/**
 * 
 */
private static final long serialVersionUID = 5035928604641787267L;
// Fields    

 private long id;
 private User user;
.....

@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="id", unique=true, nullable=false)
public long getId() {
    return this.id;
}

public void setId(long id) {
    this.id = id;
}
@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="user_id")

public User getUser() {
    return this.user;
}

public void setUser(User user) {
    this.user = user;
}
.....
}

我的问题是当我执行此代码时发生错误:如何通过userNo获取getRelativeTickets()?

userDAO.getUserByNo("10001148").getRelativeTickets().size();

public User getUserByNo(String userNo) {
    // TODO Auto-generated method stub
    String query = "from User t where t.userNo=?1";
    List<User> ls = this.findByQuery(query, userNo);
    if (ls.size() > 0)
        return ls.get(0);
    return null;
}

1 个答案:

答案 0 :(得分:5)

它没有懒惰地初始化集合,因为Hibernate会话在getUserByNo方法结束时关闭 - 数据库连接不再可用,并且对象图被分离。现在,当您调用getRelativeTickets()方法时,Hibernate无法获取关联,因为会话不再可用,并且Hibernate不会隐式地打开新连接(或会话)来延迟加载关联。

建议的解决方案是确保在从User方法返回之前完全初始化getUserByNo所需的所有关联。在HQL中,您可以指定使用from子句fetch join来急切地获取关联:

String query = "from User t left join fetch t.relativeTickets where t.userNo=?1";

这将返回完全初始化User关联的relativeTickets。这样,关联的初始化发生在单个SQL语句中,因此不需要再次命中DB来初始化关联。