当EJB3.0加hibernate和标记语言后,哇哦,用起来太爽了夜,和在学校学的EJB2.1完全不一样了哦,不说看看代码就知道了
先写一个接口LOOK什么都不用继承的
public interface QueryDAO {
public String ExecuteQuery(int index);
public void initdate();
}
然后看看实体类的写法一个字酷
@SuppressWarnings("serial")
@Entity
@Table(name="DictionaryCatalog")
public class DictionaryCatalogEntity implements Serializable {
private Integer catalogId;
private String catalogName;
private String cataMemo;
private DictionaryTypeEntity dte;
private Set<DirectoryData> dce;
@ManyToOne(cascade=CascadeType.ALL ,fetch=FetchType.LAZY,optional=false)
@JoinColumn(name="id")
public DictionaryTypeEntity getDte() {
return dte;
}
public void setDte(DictionaryTypeEntity dte) {
this.dte = dte;
}
@OneToMany(cascade=CascadeType.ALL ,fetch=FetchType.LAZY,mappedBy="dataId")
@OrderBy(value="dataId desc")
public Set<DirectoryData> getDce() {
return dce;
}
public void setDce(Set<DirectoryData> dce) {
this.dce = dce;
}
@Id
@GeneratedValue
public Integer getCatalogId() {
return catalogId;
}
public void setCatalogId(Integer catalogId) {
this.catalogId = catalogId;
}
@Column(name="catalogName" , nullable=true ,length=50)
public String getCatalogName() {
return catalogName;
}
public void setCatalogName(String catalogName) {
this.catalogName = catalogName;
}
@Column(name="cataMemo" ,nullable=true ,length=50)
public String getCataMemo() {
return cataMemo;
}
public void setCataMemo(String cataMemo) {
this.cataMemo = cataMemo;
}
}
呵呵连数据库都自己生成了,你门说酷不
完了是会话BEAN
@Stateless
@Remote ({QueryDAO.class})
public class QueryDAOBean implements QueryDAO {
@PersistenceContext
protected EntityManager em;
public void initdate() {
Query query = em.createQuery("select count(p) from Person p");
Object result = query.getSingleResult();
if (result == null || Integer.parseInt(result.toString()) == {
}
查询基本是上是写完了,看是不是很类似HIBERNATE的查询语言呵呵,但是EJB又封装了哈,现在EJB终于可以和SPRING想抗衡了哦,呵呵)
(此例子3个类没有任何关系,所以大家看看如何写就好咯,呵呵.后面我会写个例子让大家参考的)
posted on 2007-07-13 13:35
大博的BLOG 阅读(323)
评论(0) 编辑 收藏 所属分类:
EJB3.0