Java??ò???????????
???????????? ???????[ 2013/4/27 9:49:20 ] ????????
????Java JPA?????????????
????1??JPQL
????2??Criteria API
????3??SQL
?????????????
???????????SQL???
????SQL???????????????????????????????????????????????????SQL?????
// ???????????
EntityManager em = ...
// ????SQL???
String getByFirstName = "SELECT * FROM contacts c WHERE c.first_name = ?1";
// ??????????
Query query = em.createNativeQuery(getByFirstName?? Contact.class);
// ???ò??????
query.setParameter(1?? "John");
// ??????
List contacts = query.getResultList();
????????????????????3???£?
????1????JPA????????????????μ????????
????2?????????????????????????????????????????
????3???????г?????????????????д????????д???
????????SQL???????????????????????????????????????????????
????????JPQL???
????JPQL???????????????????????????SQL???????JPQL????????????????SQL????????????????
// ???????????
EntityManager em = ...
// ????JPQL???
String getByFirstName = "SELECT c FROM Contact c WHERE c.firstName = :firstName";
// ??????????
TypedQuery<Contact> query = em.createQuery(getByFirstName?? Contact.class);
// ???ò??????
query.setParameter("firstName"?? "John");
// ??????
List<Contact> contacts = query.getResultList();
????????????????????3???£?
????1???????????????????????????????????????
????2??JPQL????????????????????????
????3??????????????????????????????
????JPQL???????????????e??????仰?????????????????????????????JPQL?????????????JPQL??????????ú???????
????????Criteria API
????Criteria API??????????????ORM??????JPQL????????????????????????????????????????е?SQL?????????????????????????
// ???????????
EntityManager em = ...
// ???Criteria??????
CriteriaBuilder cb = em.getCriteriaBuilder();
// ????Criteria???
CriteriaQuery<Contact> query = cb.greateQuery(Contact.class);
// ???????Root
Root<Contact> root = query.from(Contact.class);
// ????firstName????????????t??????
Predicate firstNameIs = cb.equal(root.get(Contact_.firstName?? "John"));
// ????????where????
query.where(firstNameIs);
// ???????????????
TypedQuery<Contact> q = em.createQuery(query);
List<Contact> contacts = q.getResultList();
????????????????????3???£?
????1????????????????????????????????????
????2????????SQL??JPQL????????
????3???????????Java API?????Java???????????????????????
????Criteria API???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????ú?????????????????
????????????http://blog.csdn.net/chszs/article/details/8852437
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11