我有三个表:
1. Catalog (PK: id, name, FK: genre_id, FK: type_id)
2. Type (PK: id, name)
3. Genre (PK: id, name)
如何使用Hibernate
通过注解将Genre
和Type
连接到Catalog
?
@Entity
@Table(name = "catalog")
public class Catalog implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "product_name")
private String productName;
private Genre genre; // to add
private Type type; // to add
...
}
最佳答案
如果你想连接Genre
和Type
至 Catalog
(这是你的意思吗?),你应该包括在 Genre
中和Type
实体a Collection<Catalog>
输入字段并用 @OneToMany
对其进行注释.
如果没有,最直接的方法是添加 @ManyToOne
注释 Genre
和Type
Catalog
中的字段.
关于java - 如何在Hibernate中向POJO添加字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22249187/