Hello i have the problem, that i get no object retrieved for the one-side of a one-to-many relationship. Following example-structure:
Experiment.class:
Code:
private long id;
privat Set<DaoSamples> samples = new HashSet<DaoSample>();
@Id(generate = GeneratorType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@OneToMany(mappedBy = "experiment")
public Set<DaoSample> getSamples() {
return samples;
}
public void setSamples(Set<DaoSamples> samples) {
this.samples= samples;
}
Sample.classCode:
private long id;
private DaoSample experiment;
@Id(generate = GeneratorType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToOne (targetEntity = DaoExperiment.class)
public DaoExperiment getExperiment() {
return experiment;
}
public void setExperiment(DaoExperiment exp) {
this.experiment= exp;
}
If i hava an Experiment-Instance and call
Code:
List<DaoSample> listSamples = exp.getSamples();
all is fine. I get a List of Samples for this experiment. But if i then went through all samples like this, to get the experiment for this sample again, i get only a null-pointer returned:
Code:
for (DaoSample sample:exp.getSamples()) {
DaoExperiment newExperiment = sample.getExperiment() ; // only a null-Pointer returned :-(
}
What can be the mistake ? Have i misunderstud the logic of hibernate ?