jav*_*per 7 java orm spring hibernate jpa
我正在使用Spring rest和Hibernate开发一个应用程序,我想从数据库中获取嵌套记录,就像我正在获取Profession一个User,现在我想获取Users与Profession我之前获取的相关联.
这是我的Dao课程
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<Profession> getProfessionById(long id) throws Exception {
session = sessionFactory.openSession();
Criteria cr = session.createCriteria(Profession.class);
cr.add(Restrictions.eq("uid", id));
List results = cr.list();
tx = session.getTransaction();
session.beginTransaction();
tx.commit();
return results;
}
Run Code Online (Sandbox Code Playgroud)
获取策略
\n\n有四种抓取策略
\n\n详细解释可以查看Hibernate文档。
\n\nFetchType.LAZY按需提供
FetchType.EAGER是立即的
@SuppressWarnings({ "unchecked", "rawtypes" })\npublic List<User> getProfessionById(long id) throws Exception {\n session = sessionFactory.openSession();\n Criteria cr = session.createCriteria(Profession.class, "pro")\n .setFetchMode("user", FetchMode.JOIN);\n cr.add( Restrictions.eq("uid", id));\n Profession pro = cr.uniqueResult();\n tx = session.getTransaction();\n session.beginTransaction();\n tx.commit();\n return pro.getUsers();\n} \nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
3923 次 |
| 最近记录: |