【问题标题】:Foreign key not saved by hibernate休眠未保存外键
【发布时间】:2018-11-02 20:58:35
【问题描述】:

在一个带有hibernate和jpa的spring boot 2项目中,

我尝试保存一些对象

@Embeddable
public class EmbedddedSamplesKey implements Serializable {

    private Integer id;
    private int year;
    ...
}

@Entity
@IdClass(EmbedddedSamplesKey.class)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Samplings {

    @Id
    @GeneratedValue
    private Integer id;

    @Id
    private int year;

    @OneToMany(mappedBy = "sampling", cascade = CascadeType.PERSIST, orphanRemoval = true)
    private List<Samples> samples = new ArrayList<>();

    public void addSample(Samples sample) {
      samples.add(sample);
      sample.setSampling(this);
    }

    public void removeSample(Samples sample) {
      samples.remove(sample);
      sample.setSampling(null);
    }

    ...
}

@Entity
public class Samples extends BaseEntity {
    @Id
    @SequenceGenerator(name = "samples_id_seq", sequenceName = "samples_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "samples_id_seq")
    private Integer id;

    private String letter;

    //@OneToOne(optional = false, cascade = CascadeType.PERSIST)
    //private TestSamples testSamples;

    @ManyToOne
    private Samplings sampling;
    ...
}

当我保存采样时,我会在样本表中看到

sampling_id 采样年

不是饲料

这应该是 orm 的工作吗?

在 Samples 中,orm 已经生成了这个

ALTER TABLE samples
  ADD CONSTRAINT fkq5if151jgtlcy7yfp55ffvf47 FOREIGN KEY (sampling_id, sampling_year)
  REFERENCES samplings (id, year)
  ON UPDATE NO ACTION
  ON DELETE NO ACTION;

编辑,尝试

@ManyToOne
@JoinColumns({
    @JoinColumn(name = "sampling_id", referencedColumnName = "id"),
    @JoinColumn(name = "sampling_year", referencedColumnName = "year")})
private Samplings sampling;

但结果相同

另外,如果在 Samples 中我想使用像 Sampling primary + field letter 这样的主键,有没有办法做到这一点?

【问题讨论】:

    标签: java hibernate spring-boot jpa spring-data


    【解决方案1】:

    这可能与尼古拉斯所说的相反

    看看这个例子

    https://github.com/hibernate/hibernate-orm/blob/ceaeb81e3362ff187004ea3479b2afeeba5aa8a6/documentation/src/test/java/org/hibernate/userguide/mapping/identifier/IdClassGeneratedValueTest.java#L77

    更改为 cascade.all,应该会有所帮助...可能会将数据发送到合并而不是持久化。

    可能是弹簧启动问题?

    【讨论】:

      【解决方案2】:

      粗略一看,我认为您不能将@GeneratedValue 与@IdClass 一起使用。

      Auto Increment not working for Entity class with composite Key

      【讨论】:

        【解决方案3】:

        也许不是最好的答案,但最好等 3 个月才能得到答案....时间就是金钱

        如果不知道需要对序列做任何特殊操作......

        只需使用序列,添加一个瞬态字段,即序列+年份....

        【讨论】:

        • 不疯狂,但肯定可以做我想做的事......不像功能那么复杂
        【解决方案4】:

        我认为您缺少@JoinColumn 注释:

        @ManyToOne
        @JoinColumn(name = "sampling")
        private Samplings sampling;
        

        【讨论】:

        • 已尝试...但列数错误。应该是 2
        • Samples 表中有名为 sampling 的列吗??
        • 因为使用了IdClass,在samples中:创建了两列sampling_id,sampling_year
        猜你喜欢
        • 1970-01-01
        • 2021-06-15
        • 2011-12-16
        • 2018-07-11
        • 2017-03-18
        • 2012-02-05
        • 2013-02-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多