0

I have the below scenario :

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);

}

there are any way to force hibernate to fetch the startPointId , points before creation of startPoint attribute to get the right value?

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
Said Gamal
  • 65
  • 1
  • 1
  • 13

2 Answers2

1

You don't need a property, just use a transient getter. You call it each time you need to get the start point.

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

This will work with no hassle (provided the FetchType of points is EAGER), and Hibernate will safely ignore it.
Actually, there might be a slight performance loss in querying the HashMap each time, but I wouldn't bother with that.

Furthermore, I suggest you to replace HashMap in the declaration of points with Map, i.e.:

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

(update getters and setters accordingly)

gd1
  • 11,300
  • 7
  • 49
  • 88
0

Do the startPoint assignment in the setter for startPointId, or just in the getter for startPoint. And startPoint should probably be transient or @Transient so Hibernate doesn't muck with it.

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;
}
Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • the problem is in some times the points map is still empty and not fetched from the data base so i am need any way to force it to be filled before calling getStartPoint(). – Said Gamal Feb 25 '13 at 22:16