我有两个表和相关的javaMap。
CREATE TABLE country (
code VARCHAR(3) PRIMARY KEY NOT NULL,
name VARCHAR(100) NOT NULL
);
CREATE TABLE user (
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
country_code VARCHAR(3),
FOREIGN KEY ( country_code ) REFERENCES country ( code )
);
这是我的java实体。国家pojo:
@Entity
@Table(name = "country")
public class Country {
@Id
@Column (name = "code")
private String code;
@Column (name = "name")
private String name;
和用户pojo:
@Entity
@Table(name = "user")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "country_code")
private String countryCode;
问题是我怎样才能加入 Contry.code
至 User.countryCode
在hibernate中使用注解?当我用hibernate创建用户对象时,我需要自动绑定这两个字段(code和countrycode)。
1条答案
按热度按时间nuypyhwy1#
你需要
@OneToMany
Map自Country
至User
实体及对应@ManyToOne
Map自User
至Country
: