简体   繁体   English

JPA-为什么我的一对多关系没有出现在数据库中

[英]JPA - why my one to many relationship does not appear in database

I am learning JPA and I have 2 entities (Customer and CheckingAccount) where the first entity has a one to many relationship with the 2nd entity. 我正在学习JPA,我有2个实体(Customer和CheckingAccount),其中第一个实体与第二个实体具有一对多关系。 CheckingAccount subclasses a BankAccount class. CheckingAccount子类是BankAccount类。 I am using MySQL. 我正在使用MySQL。

I create 2 checking accounts in a test program and assign them both to one customer that I have created. 我在一个测试程序中创建2个支票帐户,并将它们都分配给我创建的一位客户。

After I persist everything and check my database I expected to see 2 rows in the CheckingAccount table which I do and 2 rows in the Customer table indicating this customer has 2 checking accounts but I only see one row and the value of the Accounts column is "blob" where I was expecting to see an account number. 坚持一切并检查数据库后,我希望在CheckingAccount表中看到2行,在Customer表中看到2行,表明该客户有2个支票帐户,但我只看到一行,并且Accounts列的值为“ blob”,我希望看到一个帐号。

Shouldn't I see 2 rows in my Customer table to indicate that the customer has 2 checking accounts? 我是否应该在“客户”表中看到2行以表明该客户有2个支票帐户?

Here is my Customer entity... 这是我的客户实体...

package com.gmail.gmjord;

import java.io.Serializable;
import java.lang.String;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;

/**
 * Entity implementation class for Entity: Customer
 * 
 */
@Entity
public class Customer implements Serializable {

    @Id
    @GeneratedValue
    private String id;
    @Column(nullable = false)
    private String name;
    @Column(nullable = false)
    private List<BankAccount> accounts;
    @Column(nullable = false)
    private String infoId;
    @Column(nullable = false)
    private String pin;
    private static final long serialVersionUID = 1L;

    public Customer() {
        super();
        accounts = new ArrayList<BankAccount>();
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAccountsLength() {
        return this.accounts.size();
    }

    public boolean isAccountsNull() {
        if (accounts == null) {
            return true;
        } else {
            return false;
        }
    }

    public List<BankAccount> getAccounts() {
        return accounts;
    }

    public void setAccounts(List<BankAccount> accounts) {
        this.accounts = accounts;
    }

    public String getInfoId() {
        return this.infoId;
    }

    public void setInfoId(String infoId) {
        this.infoId = infoId;
    }

    public String getPin() {
        return this.pin;
    }

    public void setPin(String pin) {
        this.pin = pin;
    }

}

Here is my CheckingAccount entity... 这是我的CheckingAccount实体...

package com.gmail.gmjord;

import com.gmail.gmjord.BankAccount;
import java.io.Serializable;
import javax.persistence.*;

/**
 * Entity implementation class for Entity: CheckingAccount
 *
 */
@Entity

public class CheckingAccount extends BankAccount implements Serializable {


    private static final long serialVersionUID = 1L;

    public CheckingAccount() {
        super();
    }

}

Here is my BankAccount superclass of CheckingAccount... 这是我的CheckingAccount的BankAccount超类...

package com.gmail.gmjord;

import java.io.Serializable;
import java.lang.String;
import javax.persistence.*;

/**
 * Entity implementation class for Entity: BankAccount
 *
 */
@MappedSuperclass

public abstract class BankAccount implements Serializable {


    @Id
    @Column(nullable=false)
    private String accountNum;
    @Column(nullable=false)
    private String balance;
    @Column(nullable=false)
    private String accountType;
    private static final long serialVersionUID = 1L;

    public BankAccount() {
        super();
    }   
    public String getAccountNum() {
        return this.accountNum;
    }

    public void setAccountNum(String accountNum) {
        this.accountNum = accountNum;
    }   
    public String getBalance() {
        return this.balance;
    }

    public void setBalance(String balance) {
        this.balance = balance;
    }   
    public String getAccountType() {
        return this.accountType;
    }

    public void setAccountType(String accountType) {
        this.accountType = accountType;
    }

}

When you use "one to many" with 2 checking accounts and one customer, you have the following structure in your database (only an arbitrary example - I am using json format just to explain better but imagine its attributes as columns): 当您将“一对多”用于2个支票帐户和一个客户时,数据库中将具有以下结构(仅是一个任意示例-我使用json格式只是为了更好地解释,但将其属性想象为列):

Customer = [{id: 1, name: Mr. Anderson}] 客户= [{id:1,姓名:安德森先生}]

CheckingAccount = [{id:1, value: $100, id_customer: 1}, {id: 2, value: $250, id_customer: 1}] CheckingAccount = [{{id:1,值:$ 100,id_customer:1},{id:2,值:$ 250,id_customer:1}]

You don't need to have two rows in customer table. 客户表中不需要两行。 When you have one-to-many relationship, you have the foreign key in the many-relationship-side table. 当您具有一对多关系时,您在多关系方表中具有外键。

Got it.. does it make sense? 知道了..有意义吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM