我可以一次将一个列映射到两个表吗? [英] Can I map a single column to two tables at one time?

查看:101
本文介绍了我可以一次将一个列映射到两个表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以通过一个输入将列值添加到两个不同的表中.如果是这样,我该如何去做.

I want to know if it is possible to add a column value to two different tables with one input. If so, how do I go about doing this.

我尝试为@JoinColumn批注添加mapdby语句,但随后它为我的整个项目返回404.我是JPA,Hibernate和Spring的新手,所以为自己的无知表示歉意.

I've tried adding the mappedby statement for the @JoinColumn annotation but it then returns a 404 for my entire project. I'm very new to JPA, Hibernate, and Spring so I apologize for my ignorance.

我已经看到了有关secondarytable注释的一些内容,并且还在表的列注释中添加了表,但是这些对我没有帮助,因为我希望该信息在两个表中都存在.

I've seen somethings about a secondarytable annotation and also adding table to the column annotation but those don't help me because I want that information to be in both tables.

表一用户


@Entity
@Table(name="User", schema="db")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="User_Id")
    private Integer userId;

    @Column(name="Username", unique=true)
    private String username;

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

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

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

    @Column(name="MAC_Address", unique=true)
    private String macAddress;


    @OneToMany(mappedBy = "guestId", fetch = FetchType.EAGER)
    private Set<Reservation> guestReservations = new HashSet<>();

    @OneToMany(mappedBy= "hostId", fetch = FetchType.EAGER)
    private Set<Reservation> hostReservations = new HashSet<>();
...

表二RDKAccess

Table Two RDKAccess

@Entity
@Table(name="RDKAccess", schema="db")
public class RDKAccess implements Serializable {

private static final long serialVersionUID = 1L;


    @Id
    @JoinColumn(name="MAC_Address")
    @OneToOne
    private User macaddress;

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

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

我希望它如何工作:

使用用户名,密码,名字,姓氏和macAddress创建用户帐户后,所有这些字段都将添加到User Table中,并且在一个条目上将相同的macAddress添加到RDKAccess表中.

When user account is created with username, password, firstname, lastname, and macAddress all of those fields are added to User Table also adding the same macAddress to the RDKAccess table on one entry.

推荐答案

将此添加到用户表中 @OneToOne(mappedBy =用户",级联= CascadeType.ALL) 私有RDKAccess rdkAccess;

Add this into your User Table @OneToOne(mappedBy = "user", cascade = CascadeType.ALL) private RDKAccess rdkAccess ;

Add this into your RDK acess Table and get tha data from user not add to in rdk access table

@OneToOne
@MapsId
private User user;

Hope this will help you,

这篇关于我可以一次将一个列映射到两个表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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