Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JPA检索多个对象_Java_Mysql_Jpa_Jdbc - Fatal编程技术网

Java 使用JPA检索多个对象

Java 使用JPA检索多个对象,java,mysql,jpa,jdbc,Java,Mysql,Jpa,Jdbc,在使用普通JDBC几个月后,我最近切换到JPA(EclipseLink),我正在使用API重建我的一个应用程序 一个简单的背景:我在MySQL数据库中有两个表。CandidateBean表示的表在SearchCriteria表的“Ref”列中将其主键用作外键 我希望以这样的方式检索对象:如果检索CandidateBean,它会自动检索它链接到的SearchCriteria对象。我理解对单个实体和JPQL查询的检索,但我非常确定在JPA中有一种简单的方法,它不会像JDBC那样涉及多个查询/连接 我

在使用普通JDBC几个月后,我最近切换到JPA(EclipseLink),我正在使用API重建我的一个应用程序

一个简单的背景:我在MySQL数据库中有两个表。CandidateBean表示的表在SearchCriteria表的“Ref”列中将其主键用作外键

我希望以这样的方式检索对象:如果检索CandidateBean,它会自动检索它链接到的SearchCriteria对象。我理解对单个实体和JPQL查询的检索,但我非常确定在JPA中有一种简单的方法,它不会像JDBC那样涉及多个查询/连接

我有以下代码:

候选项:

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

如果我需要澄清什么,请告诉我。谢谢你的帮助

就像您在SearchCriteria中添加了@manytone一样,在CandidateBean中添加了一个reverese@OneToMany,它将创建一个搜索标准列表/集。现在,当您获取CandidateBean的记录时,您将自动获得链接的SearchCriteria。您可以根据自己的需要,以懒散或急切的方式获取链接的记录


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

正如Dhruv Rai Puri所述,JPA允许您使用OneToMany映射映射与候选bean关联的搜索条件集合,如下所述:

对于您的模型,它只是:

  @ManyToOne(mappedBy = "user")
  List<SearchCriteria> searchCriterias;
@ManyToOne(mappedBy=“user”)
列出搜索标准;
请注意,mappedBy引用了SearchCriteria中用户属性上的映射,因此将使用其中指定的联接列。它使关系具有双向性,并由“用户”控制;对SearchCriteria中“用户”的更改将更改数据库中的字段,而不管对CandidateBean中集合的更改如何,但为了缓存的目的,这两个字段应保持同步。上面的映射默认为延迟抓取,这意味着如果关联实例尚未缓存在对象中,则在访问时将触发对关联实例的查询

从那里,您可以控制通过各种JPA和本机EclipseLink设置检索此集合的方式和时间-您可以设置总是以某种方式获取关系,或者决定逐个查询地对其进行更改。看见

这澄清了一切,我非常感谢您的详细回复。干杯