如何使用Hibernate连接两个表的字段? [英] How to join fields of two tables using Hibernate?

查看:78
本文介绍了如何使用Hibernate连接两个表的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  CREATE TABLE country(
code VARCHAR(3)PRIMARY KEY NOT NULL,
name VARCHAR(100)NOT NULL
);


CREATE TABLE用户(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VARCHAR(100)NOT NULL,
country_code VARCHAR(3),
FOREIGN KEY(country_code)参考国家(代码)
);

这是我的Java实体。国家POJO:

pre code $ @ $实际
@Table(name =country)
public class Country {

@Id
@Column(name =code)
private String code;

@Column(name =name)
私有字符串名称;

和用户POJO:

  @Entity 
@Table(name =user)
public class User实现Serializable {

@Id
@GeneratedValue = GenerationType.AUTO)
private Integer id;

@Column(name =name)
私有字符串名称;

@Column(name =country_code)
private String countryCode;

问题是如何加入 Contry.code User.countryCode 在Hibernate中使用注释?当我使用Hibernate创建User对象时,我需要自动将这两个字段(code和countryCode)绑定在一起。

国家用户实体和相应的<$ c的映射c $ c> @OneToMany $ c> @ManyToOne 从用户国家的映射:

  @Entity 
@Table(name =country)
public class Country {

@ Id
@Column(name =code)
private String code;

@Column(name =name)
私有字符串名称;

@OneToMany(mappedBy =country)
private Set< User>用户;

$ b $实体
@Table(name =user)
public class User实现Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@Column(name =name)
私有字符串名称;

@ManyToOne
@JoinColumn(name =country_code)
私人国家国家;
}


I have two tables and related Java mapping.

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 )
);

Here is my Java entities. Country POJO:

@Entity
@Table(name = "country")
public class Country {

    @Id
    @Column (name = "code")
    private String code;

    @Column (name = "name")
    private String name;

And User 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;

Question is how can I join Contry.code to User.countryCode in Hibernate using annotation? When I create User object with Hibernate I need to bind these two fields (code and countryCode) automatically.

解决方案

You need @OneToMany mapping from Country to User entity and corresponding @ManyToOne mapping from User to Country:

@Entity
@Table(name = "country")
public class Country {

    @Id
    @Column (name = "code")
    private String code;

    @Column (name = "name")
    private String name;

    @OneToMany(mappedBy = "country")
    private Set<User> users;
}

@Entity
@Table(name = "user")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(name = "name")
    private String name;

    @ManyToOne
    @JoinColumn(name = "country_code")
    private Country country;
}

这篇关于如何使用Hibernate连接两个表的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆