【问题标题】:JPA - why my one to many relationship does not appear in databaseJPA - 为什么我的一对多关系没有出现在数据库中
【发布时间】:2014-11-18 17:37:22
【问题描述】:

我正在学习 JPA,我有 2 个实体(客户和 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;
    }

}

【问题讨论】:

  • 两张支票和一位顾客看起来很一对多。
  • 声明 OneToMany 的注解是 ... OneToMany。不是列。如果你想要一个 CheckingAccount 列表,你应该有一个 List 类型的字段,而不是 List 类型的字段。

标签: java mysql jpa orm


【解决方案1】:

当您对 2 个支票账户和一个客户使用“一对多”时,您的数据库中有以下结构(只是一个任意示例 - 我使用 json 格式只是为了更好地解释,但将其属性想象为列):

客户 = [{id: 1, name: Mr. Anderson}]

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

您不需要在客户表中有两行。当你有一对多关系时,你在多关系边表中有外键。

明白了..有意义吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 2020-03-19
    相关资源
    最近更新 更多