如何使用Spring JPA使用Hibernate`将一个表的多个列映射到另一个表的单个父列

时间:2016-07-04 08:46:35

标签: java mysql spring hibernate jpa

我想将3列地址(国家,州,市)映射到配置表id列。

我有以下表结构:

地址型号/表格

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "addresses")
public class Address {

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

    private Configuration configuration;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "id")
    private int country;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "id")
    private int state;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "id")
    private int city;

    private int zipCode;

    @Column(name = "created_at")
    private Date createdAt;
    @Column(name = "created_by")
    private String createdBy;

    @Column(name = "updated_at")
    private Date updatedAt;

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

    private String status;
    private String deleteFlag;
}

配置模型/表

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

@Entity
@Table(name = "configurations")
public class Configuration {

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

     @NotNull
     private String configurationName;

     @NotNull
     private String configurationType;

     @NotNull
     private String ConfigurationDescription;

     @Column(name = "parent_id")
     private long parentId;
}

在这里,我无法映射配置和地址与配置之间的关系。

请各位朋友帮助我。

更新

**Configurations Table record**

id configname  configtype  configdescription parentid created_at  updated_at Status deleteflag
1  India       Country     This is a country  0       12-05-2016  Null Active Null
2  Delhi       State       This is a State    1       12-05-2016  Null Active Null
3  New Delhi   City        This is a city     2       12-05-2016  Null Active Null
----------

**Address Table record**

id Country State City zipcode created_at updated_at status deleteflag
1      1     2     3  110034  12-05-2016  Null       Active  Null

1 个答案:

答案 0 :(得分:1)

如果要将三个字段从地址映射到配置。你想要的基本上有两件事。

当您尝试保存新配置时,您希望使用配置的关联地址自动设置配置的ID。为此,您必须创建自定义标识符生成策略。 要编写自定义标识符生成器,​​请参阅此http://www.onlinetutorialspoint.com/hibernate/custom-generator-class-in-hibernate.html

如果您想在不使用自动标识符生成的情况下自行设置ID。为此,只需从Configuration类中的id成员中删除@GeneratedValue注释。现在您必须使用Address中的三个属性自行设置标识符。您可以在配置和地址之间创建关联。

我没有看到为什么要将一个实体的三个属性映射到另一个实体的id属性的任何其他原因。

<强>更新

更改您的地址实体,如下所示

@Entity
@Table(name = "addresses")
public class Address {

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

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "country")
    private Configuration country;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "state")
    private Configuration state;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "city")
    private Configuration city;

    @Column(name="zipcode") 
    private int zipCode;

    @Column(name = "created_at")
    private Date createdAt;
    @Column(name = "created_by")
    private String createdBy;

    @Column(name = "updated_at")
    private Date updatedAt;

    @Column(name = "updated_by")
    private String updatedBy;
     @Column(name = "status")
    private String status;
    @Column(name = "deleteFlag")
    private String deleteFlag;

}

在上述配置中,您将配置实体映射为一对一关系,地址为州,城市和国家/地区。