[英]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.