【问题标题】:How to map multiple columns of one table to single parent column of another table using Spring JPA using Hibernate`如何使用 Spring JPA 使用 Hibernate 将一个表的多个列映射到另一个表的单个父列
【发布时间】:2016-07-04 08:46:34
【问题描述】:

我想将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

【问题讨论】:

  • 你想要地址和配置之间的关联吗????
  • 我已经更新了我想要的,请看。
  • 您想将您的地址类中的州城市和国家成员映射到配置中的相应行..我是rt吗??
  • 是的。这就是我想要的。我也在寻找诸如加入子类或每个类的表等替代方案。
  • 请在我的回答中查看下面的更新

标签: java mysql spring hibernate jpa


【解决方案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;

}

在上述配置中,您将 Configuration 实体与 state 、 city 和 country 的地址一一对应。

【讨论】:

  • 谢谢,它成功了。表自动创建。让我看看它也适用于前端。
猜你喜欢
  • 2017-08-13
  • 2012-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-03
  • 1970-01-01
相关资源
最近更新 更多