随笔 - 11  文章 - 11  trackbacks - 0

留言簿(2)

随笔分类(13)

随笔档案(11)

搜索

  •  

最新评论

阅读排行榜

评论排行榜

在Struts1.1-1.2中如果不想没有登录的用户访问一些action,可以扩展RequestProcessor,并重载processProprocess方法,在其中进行验证

public class CustomRequestProcessor
        extends RequestProcessor {
    protected boolean processPreprocess (
            HttpServletRequest request,
            HttpServletResponse response) {
        HttpSession session = request.getSession(false);
        //If user is trying to access login page
        // then don't check
        if( request.getServletPath().equals("/loginInput.do")
            || request.getServletPath().equals("/login.do") )
            return true;
        //Check if userName attribute is there is session.
        //If so, it means user has allready logged in
        if( session != null &&
        session.getAttribute("userName") != null)
            return true;
        else{
            try{
                //If no redirect user to login Page
                request.getRequestDispatcher 
                    ("/Login.jsp").forward(request,response);
            }catch(Exception ex){
            }
        }
        return false;
    }
}

相应的,在struts-config.xml中加入controller元素

<controller>
 <set-property  property="processorClass"
 value="com.sample.util.CustomRequestProcessor"/>
</controller>

但刚才看到在Struts1.3中,已经不再使用RequestProcessor,而是用ComposableRequestProcessor 来实现自定义的request响应。






posted @ 2007-04-09 18:52 hijeff 阅读(436) | 评论 (0)编辑 收藏

三个@Entity类Tc, Tracer, Track是三向关联的的表, @Embeddable类TcTracer则存储着三个表的ForeignKey

@Entity
@Table(name= "tc", catalog="first_hiber")
public class Tc {
      @org.hibernate.annotations.CollectionOfElements
      @JoinTable(name="tc_tracer", joinColumns=@JoinColumn(name="tc_id"))
      private Set<TcTracer> tcTracers = new HashSet<TcTracer>();

      @Id @GeneratedValue
      @Column(name="tc_id", nullable= false)
      private Long id;
}
  @Embeddable
  @Table(name="tc_tracer")
  public class TcTracer {
      @org.hibernate.annotations.Parent
      private Tc tc;
  
@ManyToOne(cascade=CascadeType.ALL) @JoinColumn(name="tracer_id", nullable=false, updatable=false) private Tracer tracer; @ManyToOne(cascade=CascadeType.ALL) @JoinColumn(name="track_id", nullable=false, updatable=false) private Track track; }

如上,将TcTracer作为Tc的子元素,从而实现Tc的三相关联。

用HQL可以通过特定的Tc,Tracer得到对应的所有Track:

  select tctracers.track
  from Tc tc
      join tc.tcTracers tctracers
  where tc.name="tc name"
      and tctracers.tracer.name="tracer name"
 List<Track> tracks = session.createQuery(HQL).list();
posted @ 2007-04-02 17:45 hijeff 阅读(374) | 评论 (0)编辑 收藏

今天调试程序的时候,发现插入数据时老是抛出异常:"a different object with the same identifier value was already associated with the session"

起初以为是override hashCode()的方法不对。后来google查了查多数问题都出现在detached对象之后,再链接session由于对象的改变而出现的异常。

最后发现原来问题很简单....是在用annotation重写mapping的时候,忘记给@Id加上@GeneratedValue,导致第一个row insert后,再次insert时没有生成新的id!

看来annotation虽然方便了mapping,但由于和代码集中在一块了,很容易出现疏忽大意的错误,而且一般都不会注意到-.-

想起了python的SQLObject, SQLAlchemy,虽然功能没有Hibernate丰富,但是简单实用!

posted @ 2007-03-29 20:28 hijeff 阅读(400) | 评论 (0)编辑 收藏