Hibernate ForeignKey 映射注解

Hibernate ForeignKey mapping annotations

我想让hibernate生成一些带外键的表等等。我会给你一个我希望休眠生成的查询的例子:

create table RealtimeCost(id INTEGER not null primary key Autoincrement,
    mnemonic varchar(50)not null references Exchange(mnemonic),
    sid int not null references License(sid),
    price numeric(10,2) not null)

所以这个查询应该由 hibernate 通过注释生成。对应的class为:

@Entity
@Table
public class RealtimeCost {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @MapsId("mnemonic")
    @JoinColumn(referencedColumnName="sid")
    private String mnemonic;
    @MapsId("sid")
    @JoinColumn(referencedColumnName="sid")
    private Integer sid;
    @Column
    private Double price;

RealtimeCost 中的 mnemonic 应映射到的示例(RealtimeCost 中的每个助记符在 Exchange 中恰好有 1 个值):

@Entity
@Table
public class Exchange {
    @Id
    @Column(name="mnemonic")
    private String exchange;
    @Column
    private String description;

如您所见,我在文档的帮助下做了一些尝试,但我无法让休眠生成外键。如果有人能告诉我这个 class 所需的注释和值,那将是非常友好的,这样我也可以为其他 classes 自己做。另外请告诉我是否需要更改 Exchange class 中的任何内容以使映射正常工作。提前致谢

我不是专家,但我们让 Hibernate 使用 javax.persistence 连接实体的注释完成所有工作。

  @javax.persistence.ManyToOne( fetch = javax.persistence.FetchType.EAGER, optional = true )
  @javax.persistence.JoinColumn( name = "VIEWTYPE_ID", nullable = true, unique = false, insertable = true, updatable = true )
  private com.company.other.subproject.ViewType viewType;

也许这就是您所需要的。由于这让我们休眠关心必须创建或不创建的表,并且使用您与之通信的数据库的方言自动创建外键。

您应该在一个实体中设置关联并在另一个实体中使用 mappedBy。您不需要 @MapsId 因为您没有使用嵌入式实体(阅读文档)。看看 @OneToMany@ManyToOne 的关系:

@Entity
@Table
public class RealtimeCost {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @OneToMany
    @JoinColumn(name="mnemonic")
    private Exchange exchange;
    ...
}

@Entity
@Table
public class Exchange {
    @Id
    @Column(name="mnemonic")
    private String mnemonic;

    @Column
    private String description;

    @ManyToOne(mappedBy="exchange")
    private RealtimeCost realtimeCost;
    ...
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "accommodation_type", unique = true, nullable = false)
private AccommodationType accommodationType;

@ManyToOne()根据@JoinColumn()建立关系 @JoinColumn() 中的 name 是您要建立连接的 table 名称。

然后当你创建一个class连接到主class时,你首先需要在@Entity下面给它一个table名称,例如@Table(name="accommodation_types")

然后创建变量。

//bi-directional many-to-one association to Accommodation
@OneToMany(mappedBy="accommodationType", fetch=FetchType.EAGER)
private List<Accommodation> accommodations;

mappedBy的值是main中的变量名class。

这里发布的每个答案都得到了我的赞成票,因为每个人都说对了,但这不是我 100% 想要的,但它帮助我自己解决了我的问题。对于我发布的示例,我正在寻求的解决方案如下(我还添加了不可为空):

@Entity
@Table
public class RealtimeCost {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @ManyToOne
    @JoinColumn(name = "mnemonic",nullable=false)
    private Exchange exchange;
    @ManyToOne
    @JoinColumn(name = "sid",nullable=false)
    private License license;
    @Column(nullable=false)
    private Double price;

这些是我正在寻找 RealtimeCost class 的注释。我在 Exchange class 中不需要任何特殊注释。 @Nico 的回答最接近我的需要,因此他的回答将被接受