了解JPA中的单表继承

时间:2015-01-22 14:51:08

标签: java jpa single-table-inheritance

我是JPA的新手并做了一些小样本来了解它。 但我在下面遇到一个问题,请帮帮我,请解释原因:

我有类Customer.java,它在db:

中映射到表customer
@Entity
public class Customer implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "id_customer")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    // accountNumber field maps with accountNumber column in Account table
    @Column(name = "loginId", unique = true)
    private String loginId;

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

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

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

    @Column(name = "address")
    private String address;

    @Column(name = "email")
    private String email;

    @Column(name = "phone")
    private String phone;

    @OneToMany(mappedBy="customer")
    private List<Account> accountList;

    @OneToMany(mappedBy="customer")
    private List<Card> cardList;
// getters and setters goes here
}

上面的类有两个列表,accountList和cardList,它们的泛型类(Card和Account)使用单表继承扩展BaseInfo。
这是我的BaseInfo.java:

@Entity
@Table(name = "account")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING)
public class BaseInfo implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "number")
    private String number;

    @Column(name = "availableNumber")
    private Long availableNumber;
//getter and setter here
}

Class Card.java:

@Entity
@Table(name = "account")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue(value = "C")
public class Card extends BaseInfo implements Serializable {

    private static final long serialVersionUID = 1L;

    @Column(name = "cardType")
    private String cardType;

    @ManyToOne
    @JoinColumn(name = "id_customer")
    private Customer customer;

//getter and setter
}

并且类Account.java:

@Entity
@Table(name = "account")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue(value = "A")
public class Account extends BaseInfo implements Serializable {
    private static final long serialVersionUID = 1L;

    @Column(name = "accountName")
    private String accountName;

    @Column(name = "accountType")
    private String accountType;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "dt_created")
    private Date createdDate;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "dt_lst_updt")
    private Date lastUpdatedDate;

    @ManyToOne
    @JoinColumn(name = "id_customer")
    private Customer customer;

//getter, setter
}

然后,我执行查询,使用loginid和密码从数据库查询客户,如下所示:

entityTransaction.begin();

TypedQuery<Customer> query = entityManager.createQuery(
                    "SELECT c FROM " + Customer.class.getName()
                            + " c Where c.loginId= :loginId", Customer.class);
query.setParameter("loginId", loginId);

res = query.getSingleResult();

entityTransaction.commit();

代码运行没有错误,但结果对我来说很奇怪:当我调试(或将结果打印到jsp)时,accountList或cardList包含该客户的所有帐户,就像他们不关心“鉴别者”专栏。

我有两个问题:

  1. 如何归档listCard仅包含Card(歧视= c)且listAccount仅包含Account(discriminator = a)的目标?

  2. 是否有另一种方法可以查询listCard或listAccount而无需先查询客户(就像我使用的那样)??

  3. 提前感谢! :d

1 个答案:

答案 0 :(得分:0)

我不确定它是JPA限制还是特定于Hibernate的限制,但您可能不会使用相同的列来映射两个不同的关联。

您应该使用类似car_customer_id的内容来映射客户和卡之间的关联,并使用account_customer_id来映射客户和帐户之间的关联。