简体   繁体   English

JPA-从数据库表中检索数据wrt外键-

[英]JPA - Retrieving data from database table wrt Foreign key -

I am trying to retrieve data from a table which has a foreign key, "reg_no". 我正在尝试从具有外键“ reg_no”的表中检索数据。 And the foreign key is not unique, it can be duplicated. 而且外键不是唯一的,可以重复。

Now I want to retrieve the data from this table using this foreign key. 现在,我想使用此外键从此表中检索数据。 I will provide a "reg_no" and the Java Persistence API will retrieve a list of the result set from the table wrt to "reg_no" provided. 我将提供一个“ reg_no”,Java Persistence API将从表wrt到提供的“ reg_no”中检索结果集的列表。

Please enlighten me how can I solve this problem? 请赐教我如何解决这个问题?

You can do something like this, using JPQL: 您可以使用JPQL执行以下操作:

String queryString = "SELECT t FROM YourTable t " +
                     "WHERE reg_no = :regNo";
Query query = getEntityManager().createQuery(queryString); 
query.setParameter("regNo", regNoValue);
return query.getResultList();
EntityManager entityManager = entityManagerFactory.createEntityManager();
List results = em.createQuery("SELECT c FROM Vehicle v WHERE reg_no = :reg_no").setParameter("reg_no", new String("0000")).getResultList;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JPA从数据库检索数据 - Retrieving data from database using JPA 如何使用JPA通过外键搜索mySql数据库表? - How to search mySql database table by foreign key using JPA? 使用外键从 spring 引导 jpa 中的引用表中查找所有数据 - Find all data using foreign key from referenced table in spring boot jpa 使用 spring 数据从关系数据库添加和检索数据 jpa - Adding and retrieving the data from relational database using spring data jpa 使用 JPA 使用外键连接来自另一个表的列 - Join column from another table with Foreign Key using JPA 从Java中的mysql数据库表中检索数据 - retrieving data from mysql database table in Java 如何使用JPA / JPQL查询和外键从数据库读取数据 - How to read data from database with JPA/JPQL Query and Foreign Keys 从具有Spring和JPA批注的两个表之间具有外键关系的html表单(使用Thymeleaf)保存数据的问题 - Problem with saving data from a html form (using Thymeleaf) that has a foreign key relation between two table with Spring and JPA annotations 数据库中的JPA外键,项目中的返回对象 - JPA Foreign key in database, return object in project Spring data jpa 在空数据库中为具有包含外键的复合主键的实体创建错误的字段 - Spring data jpa creates wrong fields in empty database for entity with composite primary key that contains foreign key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM