使用JPA

时间:2015-09-25 00:55:35

标签: java mysql jpa jdbc

我使用普通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

SearchCriteria:

//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

如果我需要澄清一些事情,请告诉我。谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

如Dhruv Rai Puri所述,JPA允许您使用OneToMany映射映射与CandidateBean关联的SearchCriteria集合,如下所述: https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Relationship_Mappings/Collection_Mappings/OneToMany

对于您的模型,它只是:

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

请注意,mappedBy引用SearchCriteria中用户属性的映射,因此将使用此处指定的连接列。它使关系成为双向的,并由“用户”控制;无论CandidateBean中的集合发生什么变化,对SearchCriteria中“user”的更改都将更改数据库中的字段,但两者都应保持同步以用于缓存目的。上面的映射默认为延迟提取,这意味着如果它们尚未缓存在对象中,它将在访问时触发相关实例的查询。

从那里,您可以控制通过各种JPA和本机EclipseLink设置检索此集合的方式和时间 - 您可以设置始终以某种方式获取关系,或者决定在查询的基础上更改它。看到 http://vard-lokkur.blogspot.com/2011/05/eclipselink-jpa-queries-optimization.html

答案 1 :(得分:0)

就像你在SearchCriteria中放置一个@ManyToOne一样,在CandidateBean中添加了一个尊敬的@OneToMany,它将是一个List / Set的SearchCriterias。现在,当您获取CandidateBean的记录时,您将自动获得链接的SearchCriterias。您可以根据需要以LAZY或EAGER方式获取链接记录。

此外,不需要单独的JOIN等,@ OneToMany映射就足够了