简体   繁体   English

如何在Hibernate中将字段添加到POJO?

[英]How to add fields to POJO in Hibernate?

I have three tables: 我有三个表:

1. Catalog (PK: id, name, FK: genre_id, FK: type_id)
2. Type (PK: id, name)
3. Genre (PK: id, name)

How to use Hibernate to connect Genre and Type to Catalog by using annotation? 如何通过注释使用HibernateGenreType连接到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

    ...
}

If you want to connect Genre and Type to Catalog (is this what you mean?), you should include in both Genre and Type entities a Collection<Catalog> -typed field and annotate it with @OneToMany . 如果要将GenreType连接 Catalog (这是什么意思?),则应在GenreType实体中都包含一个Collection<Catalog>类型的字段,并使用@OneToMany对其进行@OneToMany

If not, the straightest way is to add the @ManyToOne annotation to Genre and Type fields in Catalog . 如果不是这样,最直接的办法就是添加@ManyToOne注释GenreType的字段Catalog

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM