Reputation: 587
I have 2 tables train_header
and train_movement
. Train header has a primary key on the column id.
train movement
has a foriegn key on the column header_id that links to train_header
ALTER TABLE `trains`.`train_movement`
ADD CONSTRAINT `foobar`
FOREIGN KEY (`header_id`)
REFERENCES `trains`.`train_headers` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
my java class for the train header is annotated like so
@Entity
@Table(name="train_headers")
public class TrainHeader implements Serializable {
private int id;
private List<TrainMovement> trainMovements;
@Id
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
//bi-directional many-to-one association to TrainMovement
@OneToMany(mappedBy="trainHeader", cascade = {CascadeType.ALL})
public List<TrainMovement> getTrainMovements() {
return this.trainMovements;
}
public void setTrainMovements(List<TrainMovement> trainMovements) {
this.trainMovements = trainMovements;
}
public TrainMovement addTrainMovement(TrainMovement trainMovement) {
getTrainMovements().add(trainMovement);
trainMovement.setTrainHeader(this);
return trainMovement;
}
}
my java class for the train movement is annotated like so
@Entity
@Table(name="train_movement")
public class TrainMovement implements Serializable {
private int id;
private TrainHeader trainHeader;
@Id
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@ManyToOne
@JoinColumn(name="header_id")
public TrainHeader getTrainHeader() {
return this.trainHeader;
}
public void setTrainHeader(TrainHeader trainHeader) {
this.trainHeader = trainHeader;
}
}
finally the code to save it all looks like this
TrainMovement m = new TrainMovement();
TrainHeader h = new TrainHeader();
h.setTrainMovements(new LinkedList<TrainMovement>()); //otherwise get null pointer exception
h.addTrainMovement(m);
entityManager.persist(h);
my problem is when this happens I get an error
Caused by: org.hibernate.exception.ConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`trains`.`train_movement`, CONSTRAINT `header_link_movement` FOREIGN KEY (`header_id`) REFERENCES `train_headers` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
why is this happening? doesn't this error only happen as train movement has a header_id value that is not in the table? shouldn't hibernate be taking care of this for me? how can I solve this issue? I've tried removing the cascade (in case it was an error in the order it was saving the objects) but saving the header object followed by the movement object still causes this same error!.
any help on the matter would be great.
Upvotes: 0
Views: 115
Reputation: 587
the problem was that an id was not being created by hibernate the solution to this was to add the annotation
@GeneratedValue(strategy=GenerationType.IDENTITY)
to my id field
Upvotes: 1
Reputation: 99
I suppose you are using spring. Your entity relation are bidirectional, so try to set TrainHeader in TrainMovement object. This is one of differences in comparision to EJB container. Container manages bidirectional references like this.
Try to add this before persist:
m.setTrainHeader(h);
Upvotes: 0