samedi 29 novembre 2014

OneToMany realtion not load collection


Vote count:

0




I have a class Traveller with a relationship with TravellerVote. I wish retrieve the sending votes the travellers and the votes received.


Class Traveller



@Entity
@PrimaryKeyJoinColumn(name="user_id")
public class Traveller extends User{

@OneToMany(mappedBy="voter", cascade={CascadeType.PERSIST, CascadeType.REMOVE})
protected List<TravellerVote> myVotes;

@OneToMany(mappedBy="partner", cascade={CascadeType.PERSIST, CascadeType.REMOVE})
protected List<TravellerVote> receivedVotes;

public Traveller(){
super();
myVotes = new ArrayList<TravellerVote>();
receivedVotes = new ArrayList<TravellerVote>();
}

public void addVote(TravellerVote vote){
myVotes.add(vote);
}
}


Class TravellerVote



@Entity
public class TravellerVote {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(length=11)
protected long id;

@ManyToOne
@JoinColumn(name="voter_id")
protected Traveller voter;

@ManyToOne
@JoinColumn(name="partner_id")
protected Traveller partner;

@Column(nullable=false)
protected Date date;

@Column(length=300, nullable=true)
protected String comment;

@Column(nullable=false)

protected boolean positive;
public TravellerVote(Traveller voter, Traveller partner, String comment, boolean positive){
this.date = new Date();
this.voter = voter;
this.partner = partner;
this.comment = comment;
this.positive = positive;
}
}


The entities are persisted correctly, with this:



TravellerDao dao = new TravellerDao();
Traveller t1 = dao.findByUsername("ramiromd");
Traveller t2 = dao.findByUsername("danimd");
Traveller t3 = dao.findByUsername("famartinezd");

t1.addVote(new TravellerVote(t1, t2, "Test comment", true));
t2.addVote(new TravellerVote(t2, t1, "Test comment", true));
t3.addVote(new TravellerVote(t3, t1, "Test comment", false));

dao.save(t1);
dao.save(t2);
dao.save(t3);


But, when in other controller try get the votes, the collections have 0 elements.



TravellerDao dao = new TravellerDao();
Traveller t1 = dao.findByUsername("ramiromd");
t1.getReceivedVotes().size(); // Must be 2 not 0.
t1.getMyVotes().size(); // Must be 1 not 0.


Any ideas ?



asked 33 secs ago







OneToMany realtion not load collection

Aucun commentaire:

Enregistrer un commentaire