如何使用Hibernate使用Spring JPA将一个表的多个列映射到另一张表的单父列 [英] How to map multiple columns of one table to single parent column of another table using Spring JPA using Hibernate`

查看:219
本文介绍了如何使用Hibernate使用Spring JPA将一个表的多个列映射到另一张表的单父列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to map 3 columns of address (country, state,city) to the configuration table id column.

我具有以下表格结构:

地址模型/表

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

在这里,我无法映射配置"和地址"之间的关系以及配置"本身.

Here, i am unable to map the relationship between Configuration and Address and Configuration with itself.

请朋友们在这方面帮助我.

Please friends help me in this regards.

已更新

**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

推荐答案

当您要将三个字段从地址映射到配置时.您基本上需要两件事.

When you want to map the three field from address to Configuration. There are basically two things that you want .

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

When you try to save a new configuration you want the id of the configuration to be set automatically by using the associated address of the configuration. For this you would have to create a Custom identifier generations strategy . To write your Custom Identifier generator see this http://www.onlinetutorialspoint.com/hibernate/custom-generator-class-in-hibernate.html

当您希望自己设置ID而不使用自动标识符生成时.为此,只需从Configuration类的id成员中删除@GeneratedValue批注,现在您必须使用Address的三个属性自己设置标识符.您可以在配置"和地址"之间创建关联.

When you would want to set the id yourself without using automatic identifier generation . For this just remove the @GeneratedValue annotation from your id member in Configuration class.Now you would have to set the identifier yourself using the three property from Address. You can create an association between Configuration and Address.

我看不出您想将一个实体的三个属性映射到另一实体的id属性的其他原因.

I don't see any other reason why you would want map the three property of one entity to id property of other entity.

更新

如下更改地址实体

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

}

在上述配置中,您将州,城市和国家/地区的地址与地址之间以一对一的关系映射到配置实体.

IN the above configuration you map the Configuration entity in one to one relationship with address once for state , city and country.

这篇关于如何使用Hibernate使用Spring JPA将一个表的多个列映射到另一张表的单父列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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