在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 阅读(373) |
评论 (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 阅读(399) |
评论 (0) |
编辑 收藏
通常为了实现entity class的双向association,每添加一组实例在java中需要两行代码:
persion.getAddresses().add(address);
address.getPersons().add(person);
但对Hibernate而已,上面的代码会被转化为两条insert语句,
为了避免这样的情况出现需要定义inverse属性,告诉Hibernate persion和address之间的关系:
// !file: Person.class
public class Person {
// ...
@ManyToMany(
targetEntity= Address.class,
cascade = CascadeType.ALL,
)
@JoinTable(
name="persion_address",
joinColumns= {@JoinColumn(name="person_id")},
inverseJoinColumns= {@JoinColumn(name="address_id")}
)
private Set<Address> adressses = new HashSet<Address> (0);
// ...
}
// !file: Address.class
public class Address {
// ...
@ManyToMany(
targetEntity= Persion.class,
cascade = CascadeType.ALL,
mappedBy = "addresses"
)
private Set<Person> persons = new HashSet<Person>();
// ...
}
上面的两个annotation告诉Hibernate,Person是主,对Address中persons属性的修改将不会产生SQL语句。
Hibernate里,如果是Many-to-many的关系,可以任意设置主class。
通常,还可以将添加删除关系的代码包装起来:
// !file: Person.class
public class Person {
// ...
public void addAddress(Address address) {
this.getAddresses().add(address);
address.getPersons().add(this);
}
public void removeAddress(Address address) {
this.getAddresses().remove(address);
address.getPersons().remove(this);
}
//...
}
posted @
2007-03-27 21:18 hijeff 阅读(351) |
评论 (0) |
编辑 收藏