jpa不生成外键

1、oneToOne关系不生成外键
在引入外键的一方加入下面注解:

    @JoinColumn(foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))

在被引入外键的一方加入下面注解:

    @org.hibernate.annotations.ForeignKey(name = "none")

2、oneToMany/manyToOne关系不产生外键

在引入外键的一方加入下面注解:

    @JoinColumn(foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))

在被引入外键的一方加入下面注解:

    @org.hibernate.annotations.ForeignKey(name = "none")

3、manyToMany关系不产生外键

在引入外键的一方加入下面注解:

    @JoinTable(
            name = "aaa_to_bbb"
            , joinColumns = @JoinColumn(
                    name = "aaa_id"
                    , referencedColumnName = "id",columnDefinition = "bigint(18)"
                    ,foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT)
            )
            , inverseJoinColumns = @JoinColumn(
                    name = "bbb_id",nullable = false,updatable = false
                    , referencedColumnName = "id",columnDefinition = "bigint(18)"
                    , foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT)
            )
            , uniqueConstraints = {@UniqueConstraint(columnNames={"aaa_id", "bbb_id"})}
    )
    @org.hibernate.annotations.ForeignKey(name = "none") //重点:这个必须加,不然依然会产生注解。原因是多对多关系中两个实体表相对于中间表来说实际上是一对多的关系,实体表是被引用id的一方,所以两个实体类上的对应属性都必须加上外键忽略。

在被引入外键的一方加入下面注解:

    @org.hibernate.annotations.ForeignKey(name = "none")

下面是各种数据类型的属性注解,这些注解包含validation或者validator的校验注解。

1、boolean属性

    @Column(name = "configurable", columnDefinition = "bit(1) COMMENT 'name:xxx'")
    @NotNull(message = "xxx 不能为空", groups = {Default.class})
    private Boolean configurable;

2、int属性

    @Column(name = "xxx", columnDefinition = "int(9) DEFAULT 0 COMMENT 'name:xxx")
    @NotNull(message = "xxx 不能为空", groups = {Default.class})
    @Range(min = 0, message = "xxx 不小于{min}", groups = {Default.class})
    private Integer faultCount;

3、long属性

    @Column(name = "id", columnDefinition = "bigint(18) COMMENT 'name:id'")
    @NotNull(message = "xxx 不能为空", groups = {Default.class})
    @Range(min = 0, message = "xxx 应不小于{min}", groups = {Default.class})
    private Long id;

4、double属性

    @Column(name = "xxx", columnDefinition = "double DEFAULT 0 COMMENT 'name:xxx'")
    @NotNull(message = "xxx 不能为空", groups = {Default.class})
    @Range(min = 0, message = "xxx 不能小于{min}", groups = {Default.class})
    private Double meanTimeFailure;

5、string属性

    @Column(name = "description", columnDefinition = "varchar(255) COMMENT 'name:xxx'")
    @NotBlank(message = "xxx 不能为空",groups = {Default.class})
    @Length(min = 0, max = 255, message = "xxx 字数:{min}~{max}", groups = {Default.class})
    private String name;

6、date属性

    @Column(name = "create_time", insertable = true, columnDefinition = "datetime(0) COMMENT 'name:创建时间'")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @Temporal(TemporalType.TIMESTAMP)
    @NotNull(message = "xxx 不能为空", groups = {Default.class})
    @Past(message = "xxx 应为过去", groups = {Default.class})
    private Date createTime;

7、枚举属性

    @Column(name = "xxx", columnDefinition = "tinyint(4) COMMENT 'name:xxx 类型'")
    @NotNull(message = "xxx 类型 不能为空", groups = {Default.class})
    @Enumerated(EnumType.ORDINAL)
    @Enumerated(EnumType.STRING)
    private ParamChangeDescEnum paramChangeDescEnum;

8、一对一属性(主体)

    @OneToOne(mappedBy = "equipPosition", fetch = FetchType.LAZY)
    @org.hibernate.annotations.ForeignKey(name = "none")
    @JSONField(serialize = false)
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private FrameMap frameMap;//xxx

9、一对一属性(关系拥有者)

    @OneToOne()
    @JoinColumn(foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT), name = "xxx_id", unique = true
, columnDefinition = "bigint(18) COMMENT 'name:xxx;ref:xxx.id;refName:xxx.name'", referencedColumnName = "id")
    @JSONField(serialize = false)
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @NotNull(message = "xxx 不能为空", groups = {Default.class})
    private EquipPosition equipPosition;

10、一对多属性(主体)

    @OneToMany(mappedBy = "supEquipPosition")
    @org.hibernate.annotations.ForeignKey(name = "none")
    @JSONField(serialize = false)
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private Set xxx Set;//xxx

11、多对一属性(关系拥有者)

    @ManyToOne()
    @JoinColumn(foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT), name = "xxx_id"
, columnDefinition = "bigint(18) COMMENT 'name:xxx'", referencedColumnName = "id")
    @JSONField(serialize = false)
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @NotNull(message = "xxx 不能为空", groups = {Default.class})
    private EquipType equipType;

12、多对多属性(主体)

    @ManyToMany(mappedBy = "equipPositionSet")
    @org.hibernate.annotations.ForeignKey(name = "none")
    @JSONField(serialize = false)
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private Set xxx Set;//xxx

13、多对多属性(关系拥有者)

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name = "xxx_to_xxx"
            , joinColumns = @JoinColumn(
                    name = "xxx_id",nullable = false,updatable = false
                    , referencedColumnName = "id",columnDefinition = "bigint(18) COMMENT 'name:xxx;ref:xxx.id;refName:xxx.name'"
                    ,foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT)
            )
            , inverseJoinColumns = @JoinColumn(
                    name = "xxx_id",nullable = false,updatable = false
                    , referencedColumnName = "id",columnDefinition = "bigint(18) COMMENT 'name:xxx;ref:xxx.id;refName:xxx.name'"
                    , foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT)
            )
            , uniqueConstraints = {@UniqueConstraint(columnNames={"course_id", "teacher_id"})}
    )
    @org.hibernate.annotations.ForeignKey(name = "none")
    @JSONField(serialize = false)
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private Set xxx Set;

你可能感兴趣的:(jpa不生成外键)