如何连接JPA实体类? (一到许多/多到一个)

时间:2017-12-13 08:32:32

标签: java jpa entities

如果有这两个实体:

@Entity
@Table(name = "CUSTOMER")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "FIRST_NAME") //Attributes in the entitiy
    private String firstName;

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

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

@Entity
@Table(name = "ADDRESS")
public class Address {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "STREET_NAME") //Attributes in the entitiy
    private String streetName;

    @Column(name = "HOUSE_NUMBER")
    private int houseNumber;

我现在如何连接这两个实体并定义Customer有多个Addresses

2 个答案:

答案 0 :(得分:0)

您可以通过Internet轻松找到大量此类示例。我强烈建议您查看ObjectDB website

对于此特定方案,您可以按如下方式定义它们:

@Entity
@Table(name = "CUSTOMER")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "FIRST_NAME") //Attributes in the entity
    private String firstName;

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

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

    @OneToMany(mappedBy = "customer")
    private List<Address> addresses;
}

并在Address类中引用您的客户:

@Entity
@Table(name = "ADDRESS")
public class Address {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "STREET_NAME") //Attributes in the entitiy
    private String streetName;

    @Column(name = "HOUSE_NUMBER")
    private int houseNumber;

    @ManyToOne(fetch = FetchType.LAZY)
    private Customer customer;
}

编辑:根据您的请求获得多对多关系的示例,以下是您为客户和地址实体实施此方法的方法:

首先,您应该有一个具有两个外键的中间表,一个引用客户表,另一个引用地址。假设您将此中间表命名为customer_address,那么它的数据将是这样的:

select * from customer_address;

customer_id | address_id
------------------------
    12      |     15
    12      |     14
    12      |     13
    2       |     15
    2       |     13
    19      |     11

现在您应该更新注释并告诉JPA使用此表来推断您的实体之间的多对多关系。

客户类:

@Entity
@Table(name = "CUSTOMER")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "FIRST_NAME") //Attributes in the entity
    private String firstName;

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

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

    @ManyToMany(mappedBy = "customer")
    @JoinTable(name = "customer_address", 
        joinColumns = @JoinColumn(name = "customer_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "address_id", referencedColumnName = "id"))
    private List<Address> addresses;
}

@Entity
@Table(name = "ADDRESS")
public class Address {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "STREET_NAME") //Attributes in the entitiy
    private String streetName;

    @Column(name = "HOUSE_NUMBER")
    private int houseNumber;

    @ManyToMany(mappedBy = "customer")
    @JoinTable(name = "customer_address", 
        joinColumns = @JoinColumn(name = "address_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "customer_id", referencedColumnName = "id"))
    private List<Customer> customer;
}

在此代码中,注释用于向JPA提供以下信息:

  • @ManyToMany:表示关系的类型
  • @JoinTable:用于指定包含实际关系数据的表,并具有以下属性:
    • joinColumns:定义中间表中引用包含@ManyToMany注释的实体类的列。在Customer类中,它定义作为customer表的外键的列,在Address类中,它定义作为address表的外键的列。
    • inverseJoinColumns:它与joinColumn属性完全相反。定义中间表中引用@ManyToMany注释另一侧的实体类的列。在Customer类中,它定义作为address表的外键的列,在Address类中,它定义作为customer表的外键的列。

我知道我的描述并不像应有的那样可读/可理解,但您知道描述@JoinTable注释并不容易。读取示例代码本身并匹配列/属性名称以查找每个名称的含义将是一种更简单的方法。您也可以在ObjectWeb网站上查看(也包括样本)。

答案 1 :(得分:-1)

你应该有一个xml文件来定义这个实体引用的表,然后你可以在那里添加它们的字段和关系。

示例:

<entity class="class" access="FIELD">
<table name="class_table" />

 <attributes>
    <id name="classId">
        <column name="class_id" />
    </id>

    <basic name="className">
        <column name="case_name" />
    </basic>

<!-- Foreign key many-to-one -->
    <many-to-one name="classType" fetch="LAZY">
        <join-column name="class_type_id" referenced-column-name="class_type_id" />
    </many-to-one>

<!-- Foreign key one-to-many -->
    <one-to-many name="class_list"  mapped-by="class" fetch="LAZY" />
 </attributes>
 </entity>