作為EJB3.0的一部分,JPA是一個好東西。
@Entity
public class NewsDir ...{
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
PRivate Long id;// 主鍵
@Column(unique = true, nullable = false, length = 16)
private String sn;// 目錄編號
private String title; // 目錄名稱
@OneToMany(mappedBy = "parent", cascade = javax.persistence.CascadeType.REMOVE)
private List<NewsDir> children = new java.util.ArrayList<NewsDir>();// 下級目錄
@ManyToOne
private NewsDir parent;// 父級目錄
…
}
當然,跟任何其它優秀的技術一樣,JPA也不是完美的,在使用的過程中難免都會出這樣那樣的問題,這就需要我們程序員具有格物致知的本領,在應用中靈活應付這些問題。
這里例舉一個緩遲加載的問題,以上面的新聞目錄Entity為例。對于parnet與children這個一對多的雙向關聯,為了提高系統效率,children默認使用的是緩遲加載的方式。在一些輕量級的構架中,由于脫離了J2EE容器及事務支持,經常會出現Entity脫離了Persitence Context,變成了detach或EntityManager關閉,導致一些我們預想中的一些功能無法正常運行。
最常見的就是在使用MVC框架的時候,在表示層無法加載需要緩遲加載的數據。比如,在一個基于EasyJWeb的mvc應用中,action中的方法如下:
public Page doList(WebForm form, Module module) ...{
NewsDirQueryObject ndqo = new NewsDirQueryObject();
form.toPo(ndqo);
ndqo.setDel(true);
ipageList pageList = service.queryDirsByConditions(ndqo);
CommUtilForTeaec.saveIPageList2WebForm(pageList, form);
form.addResult("dirPath", this.getDirPath(form));
return module.findPage("list");
}
在模板文件中有如下內容:
#foreach($info in ${dir.children})
目錄名稱:${info.title}
#end
關于業務邏輯層Bean的配置:
<aop:config>
<aop:pointcut id="CmsManage"
eXPression="execution(* com.easyjf.cms.service.*.*(..))" />
<aop:advisor advice-ref="cmsManageAdvice"
pointcut-ref="CmsManage" />
<tx:advice id="cmsManageAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="SUPPORTS"
read-only="true" />
<tx:method name="query*" propagation="SUPPORTS"
read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<bean id="cmsManageService"
class="com.easyjf.cms.service.impl.CmsManageServiceImpl">
<property name="newsDirDao" ref="newsDirDao" />
</bean>
在這里,當mvc層執行到$!info.getChildren()方法的時候,將會用到緩遲加載,由于Spring的事務是配置在service層的,因此在執行service.queryDirsByConditions方法完成后就關閉了事務。因此運行程序就會出現類似下面的錯誤信息:
新聞熱點
疑難解答