\u4e0b\u8f7d<\/a><\/p>\n\u5148\u770b\u770b\u6d4b\u8bd5\uff1a<\/p>\n
public class UserDaoTest extends TestCaseBase {\n\t@Resource\n\tprivate UserDao userDao;\n\n\tpublic void testPost() {\n\t\tUser user = new User();\n\t\tuser.setUsername(\"ir\");\n\t\tuser.setPassword(\"ir\");\n\t\tList roles = new ArrayList();\n\t\troles.add(User.ROLE_STUDENT);\n\t\troles.add(User.ROLE_TEACHER);\n\t\tuser.setRoles(roles);\n\t\tuserDao.post(user);\n\t}\n\n\t@Test\n\tpublic void testGet() throws Exception {\n\t\ttestPost();\/\/ \u5b58\u50a8\u4e00\u4e2a\u540d\u53eb\"ir\"\u7684\u7528\u6237\n\t\tUser user = userDao.get(\"ir\");\/\/ \u53d6\u51fa\u4e00\u4e2a\u540d\u53eb\"ir\"\u7684\u7528\u6237\n\t\tlog.debug(BeanUtils.describe(user));\n\t\t\/**\n\t\t * \u7ed3\u679c\u4e3a\uff1a{id=1, username=ir, roles=ROLE_STUDENT, class=class\n\t\t * org.jerrymouse.User, password=ir}\n\t\t *\/\n\t\tlog.debug(user.getRoles());\/\/ \u7ed3\u679c\u4e3a\uff1a[ROLE_STUDENT, ROLE_TEACHER]\n\t}\n}<\/pre>\n\u91cc\u9762\u53ea\u6709\u4e00\u4e2a\u7528\u4f8btestGet<\/strong>\uff0c\u4e00\u5b58\u4e00\u53d6\uff0c\u4e00\u770b\u5c31\u660e\u767d\u3002\u90a3\u4e2aUser\u548cUserDao\u662f\u600e\u4e48\u56de\u4e8b\u5462\uff1f<\/p>\n@Entity\npublic class User implements CopyAble {\n\tpublic static String ROLE_STUDENT = \"ROLE_STUDENT\";\n\tpublic static String ROLE_TEACHER = \"ROLE_TEACHER\";\n\n\t@Id\n\t@Column(nullable = false)\n\t@GeneratedValue(strategy = GenerationType.AUTO)\n\tprivate Integer id;\/\/\u4e3b\u952e\n\n\tprivate String password;\n\n\t@ElementCollection(fetch = FetchType.EAGER)\n\tprivate List roles;\/\/\u4e00\u5bf9\u591a\u96c6\u5408\n\n\t@Column(unique = true)\n\tprivate String username;\n\n\/\/get and set\n}<\/pre>\n\u5982\u679c\u662fJPA1.0\u5fc5\u987b\u518d\u5199\u4e00\u4e2aRole\u7c7b\u3002<\/p>\n
@Component\npublic class UserDao extends DatabaseSupport {\n\tprivate static Log log = LogFactory.getLog(UserDao.class);\n\n\tpublic User get(String username) {\n\t\tUser user = null;\n\t\tEntityManager em = null;\n\t\ttry {\n\t\t\tem = getEntityManager();\n\t\t\t\/**\n\t\t\t * \u6784\u9020\u4e00\u4e2aQuery\n\t\t\t *\/\n\t\t\tCriteriaBuilder cb = em.getCriteriaBuilder();\n\t\t\tCriteriaQuery query = cb.createQuery(User.class);\n\t\t\tRoot emp = query.from(User.class);\n\t\t\tquery = query.select(emp).where(\n\t\t\t\t\tcb.equal(emp.get(\"username\"), username));\n\t\t\t\/**\n\t\t\t * \u8fd0\u884c\u4e4b\n\t\t\t *\/\n\t\t\tuser = em.createQuery(query).getSingleResult();\n\t\t} catch (NoResultException e) {\n\t\t\treturn null;\n\t\t} finally {\n\t\t\tem.close();\n\t\t}\n\t\treturn user;\n\t}\n\n\t@Override\n\tpublic Class getType() {\n\t\treturn User.class;\n\t}<\/pre>\n\u7ec8\u4e8e\u53ef\u4ee5\u4e0d\u5199\u4e00\u884cSQL\u5c31\u80fd\u5b9e\u73b0\u6570\u636e\u5e93\u67e5\u8be2\u4e86\uff0c\u8fd9\u4e00\u70b9\u770b\u4f3c\u5bfb\u5e38\u5176\u5b9e\u5f88\u91cd\u8981\uff0c\u5b83\u53ef\u4ee5\u5728\u91cd\u6784\u65f6\u5927\u5927\u51cf\u5c11\u9519\u8bef\u7684\u6570\u91cf\u3002
\nJPA2.0\u7684Criteria\u529f\u80fd\u8fdc\u8fdc\u4e0d\u9650\u4e8e\u6b64\uff0c\u6587\u6863\u4e2d\u6709\u4ecb\u7ecd\u3002<\/p>\n
\u5bf9\u4e86\uff0c\u63a8\u8350\u4e00\u672c\u975e\u5e38\u597d\u7684JPA\u8bfb\u7269<\/p>\n
<\/a>Pro JPA 2: Mastering the Java Persistence API<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"
JPA\u662fJava Persistence API\u7684\u7b80\u79f0\u3002JPA\u901a\u8fc7\u6ce8\u89e3\u63cf\u8ff0\u5bf9\u8c61\uff0d\u5173\u7cfb\u8868\u7684\u6620\u5c04\u5173\u7cfb\uff0c\u5e76\u5c06\u8fd0\u884c\u671f\u7684 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"yoast_head":"\n
JPA2\u521d\u8bd5 - \u6211\u81ea\u7136<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n