@Column(s) not allowed on a @ManyToOne property

前端 未结 4 1661
忘了有多久
忘了有多久 2020-12-12 21:22

I have a JPA entity with a property set as

@ManyToOne
@Column(name=\"LicenseeFK\")
private Licensee licensee;

But when I deploy on JBoss 6

4条回答
  •  执笔经年
    2020-12-12 21:52

    @Column

    The JPA @Column annotation is for basic entity attributes, like String, Integer, Date.

    So, if the entity attribute name differs than the underlying column name, then you need to use the @Column annotation to specify the column name explicitly, like this:

    @Column(name="created_on")
    private LocalDate createdOn;
    

    @JoinColumn

    The @JoinColumn annotation is used to customize a Foreign Key column name, and it can only be used with an entity association.

    So, in your case, because you are using a @ManyToOne association, you need to use @JoinColumn:

    @ManyToOne(fetch=FetchTYpe.LAZY)
    @JoinColumn(name="LicenseeFK")
    private Licensee licensee;
    

    Notice that we set the fetch attribute to FetchType.LAZY because, by default, FetchType.EAGER is used, and that's a terrible strategy. For more details about why FetchType.LAZY is a much better default, check out this article.

提交回复
热议问题