spring jpa @ManyToOne没有生成外键

时间:2018-03-29 08:43:36

标签: mysql hibernate spring-data-jpa

当我生成表格时,它会生成它,但JPA不生成任何FK。

当我告诉mysql向我展示创建表时它做了什么时,它显示缺少外键约束。它只生成KEY,而不是添加外键。

的MySQL> show create table view_display;

  

| view_display | CREATE TABLE view_display(     dtype varchar(31)NOT NULL,     id bigint(20)NOT NULL,     col int(11)NOT NULL,     row int(11)NOT NULL,     chart_id bigint(20)DEFAULT NULL,     view_id bigint(20)NOT NULL,     data_source_id bigint(20)DEFAULT NULL,     PRIMARY KEY(id),     KEY FKqllm025dtc51xf38qdr5je52qchart_id),     KEY FKb1gu01sld2cm281oa88pjq9iaview_id),     KEY FK2hljr3ohtf3vql7hhvyqbm2mcdata_source_id)   )ENGINE = MyISAM DEFAULT CHARSET = latin1 |

View.java

@Entity
@Table(name="view")
public class View {
    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "view_seq")
    @SequenceGenerator(name = "view_seq", sequenceName = "view_seq", allocationSize = 1)
    private Long id;

    private String name;

    @Column(name ="is_default",nullable= false,columnDefinition="tinyint(1) default 0")
    private boolean isDefault;

    @ManyToOne
    @JoinColumn(name="userId",insertable=true, updatable= false,nullable=true)
    private User user;

    @OneToOne
    @JoinColumn(name="cvId",insertable=true, updatable= false,nullable=true,unique=true)
    private CV cv;

    @OneToMany(mappedBy="view")
    private List<ViewDisplay> viewDisplay;
    // getters and setters....
}

ViewDisplay.java

@Entity
@Table(name = "view_display")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class ViewDisplay {
    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "view_display_seq")
    @SequenceGenerator(name = "view_display_seq", sequenceName = "view_display_seq", allocationSize = 1)
    private Long id;

    @ManyToOne
    @JoinColumn(name="chartId",insertable=true, updatable= true, nullable = true)
    private Chart chart;

    @ManyToOne
    @JoinColumn(name = "viewId", insertable = true, updatable = true, nullable=false)
    private View view;

    private int col;

    private int row;

    public ViewDisplay() {}

    public ViewDisplay(Long id, Chart chart, View view, int col, int row) {
        super();
        this.id = id;
        this.chart = chart;
        this.view = view;
        this.col = col;
        this.row = row;
    }
    // getters and setters...



}

0 个答案:

没有答案