0

我有以下情况:

class GraphUser {

    @Column(name="first_name")
    private String firstName = "";

    @Column(name="middle_name")
    private String middleName = "";

    @Column(name="last_name")
    private String lastName = "";

    @Column(name="start_point")
    private String startPointId = "";

    @ManyToMany(fetch = FetchType.EAGER, mappedBy = "points")

    private HashMap<Point> points= new HashMap<Point>();

    private Point startPoint = points.get(startPointId);

}

有什么方法可以强制休眠在创建 startPoint 属性之前获取 startPointId 点以获得正确的值?

4

2 回答 2

1

您不需要 property,只需使用瞬态 getter 即可。每次需要获取起点时都调用它。

@Transient
public Point getStartPoint() {
   return points.get(startPointId);
}

这将毫不费力地工作(前提FetchTypepointsis EAGER),并且 Hibernate 将安全地忽略它。实际上,每次
查询可能会有轻微的性能损失,但我不会为此烦恼。HashMap

此外,我建议您HashMap在声明中替换pointswith Map,即:

@ManyToMany(fetch = FetchType.EAGER, mappedBy = "points")
private Map<Point> points = new HashMap<Point>();

(相应地更新 getter 和 setter)

于 2013-02-25T22:02:34.397 回答
0

startPointsetter for 中进行赋值startPointId,或者在 getter for 中进行赋值startPoint。并且startPoint可能应该是这样transient@TransientHibernate 不会对此感到厌烦

public void setStartPointId(String startPointId) {
    this.startPointId = startPointId;
    this.startPoint = points.get(startPointId);
}

// or

public Point getStartPoint() {
    if (this.startPoint == null) {
        this.startPoint = points.get(startPointId);
    }
    return this.startPoint;
}

public void setStartPointId(String startPointId) {
    this.startPointId = startPointId;
    this.startPoint = null;
}
于 2013-02-25T22:01:56.100 回答