Hibernate配置entity关联关系不生成数据库外键

2021/10/30 19:16:26

本文主要是介绍Hibernate配置entity关联关系不生成数据库外键,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

两种方案
一、使用@org.hibernate.annotations.ForeignKey(name=“none”)
该注解已废弃
二、使用@JoinColumn(name=“frame_id”,foreignKey = @ForeignKey(name=“none”,value= ConstraintMode.NO_CONSTRAINT))
ForeignKey是javax.persistence包下的,该方案有缺陷:
1)oneToMany一方不配置(没有这一属性
2)如果存在oneToMany配置则需要使用方案1中的注解
否则还是会生成外键关系

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "taste_group_id", insertable = false, updatable = false)
    @Fetch(FetchMode.SUBSELECT)
    @org.hibernate.annotations.ForeignKey(name = "none")
    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
    @JoinColumn(name = "taste_group_id", insertable = false, updatable = false, foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
    @NotFound(action = NotFoundAction.IGNORE)
    @JsonIgnore


这篇关于Hibernate配置entity关联关系不生成数据库外键的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程