当前位置: 代码迷 >> JavaScript >> Donuts-spring2+hibernate3+jsf开发日记(3)
  详细解决方案

Donuts-spring2+hibernate3+jsf开发日记(3)

热度:690   发布时间:2012-11-17 11:14:14.0
Donuts-spring2+hibernate3+jsf开发日志(3)

?1-49)碰到 用dao.load(class,id)出错,提示延迟加载proxy已经关闭,而用dao.get(class,id)没错,难道spring这个getHibernateTemplate().load方法还需要OpenSessionInViewFilter来支持?后来发现并非如此,是因为spring事务拦截中需要配置load*方法的只读属性,不然spring事务中不会让没有配置的方法读取数据的。

?1-50)JSF Core Library中大部分Tag都有rendered这样的属性,这个属性用来控制该组件是否输出,我们只要把原先
??写在<c:if>中的判断表达式放到这里面就可以达到和<c:if>控制输出一样的效果了。
??<h:dataTable id="myTable" value="#{backingBean.myList}" var="item"
??rendered="#{not empty backingBean.myList}">
??……
??</h:dataTable>
?1-51)EL 表达式? #{not empty myBean.List}

?1-52)这样的标签也支持:<f:verbatim><input type="checkbox" name="chk" value="</f:verbatim><h:outputText value="#{example_messages['date_comp_text3']}"/><f:verbatim> "></f:verbatim>

??????? 1-53)trinidad1.2.1需要设置faces-config.xml的application的renderkit才能正常显示。

?1-54)看来spring接口式编程变得非常重要,只有老老实实按照规范实现接口
??(dao interface,service interface),spring事务处理才大有作为。
??不然事务管理中会导至:OpenSessionInViewFilter(延迟加载就是通过这个代理实现
??的),aop配置等运行运行出错。。

?1-55)myeclipse的refractor和source代码修改工具真是太好用了,有了他,spring编程才变得有乐趣。。。。:)
??1)其中的从class里抽出interface,
??2)把自己原生方法变成继承方法;
??3)把自己原生方法下放给继承自己的类而自己变成抽象方法;
??4)把变量生成getter/setter;
??5)或者把成员的方法代理出来变成自己的方法;
??6)更名,移动目录等功能简直是太好用了。。伟大的工具啊,原来版本jbuilder没带来这么多好处。

?1-56)myeclipse创建bean时,有个Enclosing type 选项,表示创建成为Enclosing type bean的子类

?1-57)> sun未来改进<f:selectItems value="#{someBean.branches}" var="branch" itemValue="#{branch.branchCd}" itemLabel="#{branch.branchLabel}"/>

?1-58)代码收藏:
??<c:forEach var="role" items="#{userForm.userRoles}" varStatus="status">
?????????????? ${role}<c:if test="${!status.last}">,</c:if>
?????????????? <input type="hidden" name="userForm:userRoles" value="${role}" />
?????????? ?</c:forEach>
?1-59)<h:outputText?? value="#{backend.tableUsers.rowIndex?? +?? 1}"?? />?? 这个方法能自动生成行号.

?1-60)selectManyListBox? binding示例:
?My bean code is as below:
?public UIComponentBase getUserGroupsList(){
?EntityManager em = getEntityManager();
?listUserGroup = (List <UserGroup>) em.createQuery("select o from UserGroup as o").getResultList();
?list=new UISelectMany();
?System.out.println("sizeof list:"+listUserGroup.size());

?SelectItem item = new SelectItem();
?for(UserGroup x : listUserGroup) {
?item = new SelectItem(x);

?UISelectItem uiItem = new UISelectItem();
?uiItem.setValue( item );

?list.getChildren().add( uiItem );
?}
?return list;
?}

?The web page code :
?<h:outputText value="User Group Assigned:"/>
?<h:selectManyListbox binding="#{user.userGroupsList}">
------------------------
I understand that that is typically a solution...but it does not get rid of
the "Validation Error: Value is not valid" problem I'm having. My equals
method (below) is similar to the one you provided. However, what appears to
be happening is that on submit of a UISelectMany or UISelectOne component
(such as selectManyListbox), is that every submitted object is compared to
the original list of selectable objects; obviously, the equals method will
only return true one for each of the submitted objects, and false for as
many other selectable objects as are in the original selectable list.

As long as the submitted values are part of the original list, why am I
getting this "Validation Error: Value is not valid" message? Is there
something else I should be doing, or questioning?

??1-61)jsf页面也能直接调用springbean对象,
??1)jsf标签的中比如#{springbean.property},太好了,不用每次都去backingbean一次。这个调用没问题
??2)jstl标签中 <c:out value="${teamService}"></c:out>?? ${springbean.property} ,jstl不能操作jsf和spring环境,调用有问题。

?1-62)多对多关系中,不用设置inverse=true,不然导至一方无法控制级联表。???!!!

?1-63)好文收藏:
??在工作中,我们常常会碰到具有递归性质记录的数据,最常见的是某个机构部门节点的数据,某个节点一定会有个父节点属性,同时它也许会有若干子节点。所有的节点数据都会存在数据库中一张表中。这种现象在设计模式上叫Composite模式。下面我就给出一个用hibernate操作这种表的例子,关于hibernate配置和建立表结构的部分请参考我的前一篇文章《利用weblogic的数据源作为hibernate的数据源的例子》

??1.持久化类Node.java
??package com.jagie.business.organization;

??/**
?? * <p>Title: </p>
?? * <p>Description: 部门节点</p>
?? * <p>Copyright: Copyright (c) 2003</p>
?? * <p>Company: www.jagie.com</p>
?? * @author Jagie
?? * @version 1.0
?? */

??public class Node {
??? private String ID;//pk
??? private String name;//名称
??? private String description;//描述
??? private Node parent;//上级部门
??? private java.util.Set children;//下级部门
??? public static void main(String[] args) {
??? }
??? public String getID() {
???return ID;
??? }
??? public void setID(String ID) {
???this.ID = ID;
??? }
??? public String getName() {
???return name;
??? }
??? public void setName(String name) {
???this.name = name;
??? }
??? public Node getParent() {
???return parent;
??? }
??? public void setParent(Node parent) {
???this.parent = parent;
??? }
??? public String getDescription() {
???return description;
??? }
??? public void setDescription(String description) {
???this.description = description;
??? }
??? public String toString(){
???return name;
??? }
??? public java.util.Set getChildren() {
???return children;
??? }
??? public void setChildren(java.util.Set children) {
???this.children = children;
??? }
??}


??2.映射文件Node.hbm.xml
??<?xml version='1.0' encoding='UTF-8'?>
??<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
??<hibernate-mapping>
??? <class name="com.jagie.business.organization.Node" table="SYS_Node">
???<id name="ID">
???? <generator class="uuid.hex" />
???</id>
???<property name="name" />
???<property name="description" />
???<set cascade="save-update" inverse="true" lazy="true" name="children">
???? <key column="parent" />
???? <one-to-many class="com.jagie.business.organization.Node" />
???</set>
???<many-to-one cascade="none" class="com.jagie.business.organization.Node"
???? column="parent" name="parent" />
??? </class>
??</hibernate-mapping>
??3.用SchemaExport创建数据库(略)

??4.测试的代码片断

?? //获取net.sf.hibernate.Session和net.sf.hibernate.Transaction;
?? Session s = JDOUtils.getInstance().getSessionFactory().openSession();
?? Transaction t = s.beginTransaction();
?? //建立父节点
?? Node org = new Node();
?? org.setName("北京总公司");
?? org.setDescription("这是北京总公司");

?? //建立子节点
?? Node org2 = new Node();
?? org2.setName("海淀分公司");
?? org2.setDescription("这是海淀分公司");
?? //建立子节点对父节点的引用
?? org2.setParent(org);
???
?? //持久化
?? s.save(org);
?? s.save(org2);

?? t.commit();
?? s.close();

??代码运行后,我们观察数据库,发现已经生成表SYS_Node,其结构为
??id??? varchar2(255)???????????
??name??? varchar2(255)??? y???????
??description??? varchar2(255)??? y???????
??parent??? varchar2(255)??? y???????

??其中y的意思为nullable(可空),同时把我们的节点数据正确的插入了。
??你可以自己再用session.find()方法来找出某父节点下面的所有子节点了。
??最后,希望这篇短文对你有用。

?1-64) 树形遍历不用递归,用堆栈.
???? /**?
???? * 树形遍历?
???? * 不用递归,用堆栈.?
???? * 这里只是做为例子,本人不建议把业务逻辑封装在Entity层.?
??????????????? */?
??? public List getVisitResults() {??
??????? List l = new ArrayList();??
??????? Stack s = new Stack();??
??????? s.push(this);??
??????? while (s.empty() == false) {
?????????? Cat c = (Cat) s.pop();
?????????? l.add(c);??
?????????? List children = c.getChildren();??
????????????? if (children != null) {??
???????????????? for (int i = 0; i <? hildren.size(); i++)?? {??
??????????? Cat cat = (Cat) children.get(i);??
??????????? s.push(cat);??
???????????????? }//end for??
???????????? }//end if??
??????? }//end while??
??????? return l;??
??? }?

?1-65)批量插入的高效方法
?我用 Connection 来试了试,很快就完成了 20w 数据的输入,哎~ Hibernate
?Session session = HibernateUtil.getSessionFactory().getCurrentSession();
?Transaction tran = session.beginTransaction();
?session.setCacheMode(CacheMode.IGNORE);
?PreparedStatement stmt;
?try {
?stmt = session.connection().prepareStatement("INSERT INTO EVENTS(EVENT_DATE, title) VALUES(?,?)");
?for (int i = 0; i < 200000; i++) {
?stmt.setTimestamp(1, new Timestamp(new Date().getTime()));
?stmt.setString(2, "Title["+i+"]");
?stmt.addBatch();
?}
?stmt.executeBatch();
?} catch (SQLException e) {
?e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
?tran.rollback();
?}
?tran.commit();
?session.close();
?HibernateUtil.getSessionFactory().close();

?

?
  相关解决方案