简体   繁体   中英

Hibernate cannot get persist working for table with foreign key

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.

问题是没有通过休眠创建ID,解决方案是将注释@GeneratedValue(strategy=GenerationType.IDENTITY)到我的ID字段中

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM