簡體   English   中英

JPA-為什么我的一對多關系沒有出現在數據庫中

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

我正在學習JPA,我有2個實體(Customer和CheckingAccount),其中第一個實體與第二個實體具有一對多關系。 CheckingAccount子類是BankAccount類。 我正在使用MySQL。

我在一個測試程序中創建2個支票帳戶,並將它們都分配給我創建的一位客戶。

堅持一切並檢查數據庫后,我希望在CheckingAccount表中看到2行,在Customer表中看到2行,表明該客戶有2個支票帳戶,但我只看到一行,並且Accounts列的值為“ blob”,我希望看到一個帳號。

我是否應該在“客戶”表中看到2行以表明該客戶有2個支票帳戶?

這是我的客戶實體...

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

}

這是我的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();
    }

}

這是我的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;
    }

}

當您將“一對多”用於2個支票帳戶和一個客戶時,數據庫中將具有以下結構(僅是一個任意示例-我使用json格式只是為了更好地解釋,但將其屬性想象為列):

客戶= [{id:1,姓名:安德森先生}]

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

客戶表中不需要兩行。 當您具有一對多關系時,您在多關系方表中具有外鍵。

知道了..有意義嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM