簡體   English   中英

使用JPA檢索多個對象

[英]Retrieve multiple objects using JPA

最近幾個月后,我使用純JDBC切換到JPA(EclipseLink),並且正在使用API​​重建我的應用程序之一。

快速背景:我在MySQL數據庫中有兩個表。 CandidateBean代表的表在SearchCriteria表的“ Ref”列中將其主鍵用作外鍵。

我想以這樣一種方式來檢索對象:如果我檢索CandidateBean,它將自動檢索它鏈接到的SearchCriteria對象。 我了解單個實體和JPQL查詢的檢索,但是我很確定在JPA中有一種直接的方法,它不像JDBC中那樣涉及多個查詢/聯接。

我有以下代碼:

CandidateBean:

// Tells JPA that this class defines an entity to be stored in a database
@Entity
// Overrides default table name
@Table(name = "CandidateUsers")
// Maps CandidateProfile table to this entity
public class CandidateBean implements Serializable {

private static final long serialVersionUID = 1L;

// Overriding column names to match database tables
@Column(name = "FName")
private String fName;
@Column(name = "LName")
private String lName;
@Column(name = "Pass")
private String password;
@Column(name = "Email")
private String email;
// Tells JPA that the following field is the primary key for this entity
@Id
// Tells JPA to use auto-incremented values of the MySQL table as the userID
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "UserID")
private int userID = 0;
etc etc constructors/getters/setters

搜索條件:

//Tells JPA that this class defines an entity to be stored in a database
@Entity
//Overrides default table name
@Table(name = "SearchCriteria")
public class SearchCriteria implements Serializable {

private static final long serialVersionUID = 1L;

// Tells JPA that the following field is the primary key for this entity
@Id
// Tells JPA to use auto-incremented values of the MySQL table as the userID
@GeneratedValue(strategy = GenerationType.IDENTITY)
// Overriding column names to match database tables
@Column(name = "SearchID")
private int searchID;
@Column(name = "State")
private String state;
@Column(name = "Field")
private String field;
@Column(name = "Cert")
private String certification;
@Column(name = "CompDate")
private String date;
@Column(name = "School")
private String school;

@ManyToOne(optional = false)
@JoinColumn(name="Ref", referencedColumnName = "UserID")
private CandidateBean user;
etc etc constructors/getters/setters

請讓我知道是否需要澄清。 感謝您的幫助!

如Dhruv Rai Puri所述,JPA允許您使用OneToMany映射來映射與CandidateBean關聯的SearchCriteria的集合,如下所述: https : //wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Relationship_Mappings/ Collection_Mappings /一對多

對於您的模型,它就是:

  @ManyToOne(mappedBy = "user")
  List<SearchCriteria> searchCriterias;

請注意,mappedBy引用SearchCriteria中user屬性上的映射,因此將使用在那里指定的連接列。 它使關系是雙向的,並由“用戶”控制; 無論對CandidateBean中集合的更改如何,對SearchCriteria中的“用戶”所做的更改都會更改數據庫中的字段,但是出於緩存目的,兩者應保持同步。 上面的映射默認為延遲獲取,這意味着如果關聯的實例尚未緩存在對象中,則會在訪問時觸發查詢。

從那里,您可以控制通過各種JPA和本機EclipseLink設置檢索此集合的方式和時間-您可以設置始終以某種方式獲取該關系,或者決定根據查詢的不同來更改該關系。 看到http://vard-lokkur.blogspot.com/2011/05/eclipselink-jpa-queries-optimization.html

就像您在SearchCriteria中放置了@ManyToOne一樣,在CandidateBean中放置了reverese @OneToMany,它將顯示SearchCriterias的列表/集合。 現在,當您獲取CandidateBean的記錄時,將自動獲得鏈接的SearchCriterias。 您可以根據需要以LAZY或EAGER方式獲取鏈接記錄。

另外也不需要單獨的JOIN等,@ OneToMany映射就足夠了

暫無
暫無

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

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