hibernate4 DetachedCriteriaBuilder的基本案例

實體的映射

@Entity

@Table(name = "VISITOR_INFO")
@DynamicInsert
@DynamicUpdate
public class VisitorInfo implements Serializable{
/**

*/
private static final long serialVersionUID = 1L;      //是這個實體保持唯一性


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name="NAME")
private String name;

@Column(name="POSITION")
private String position;


@Column(name="COUNTRY")

private String countryId;

//關聯時要注意joinColumn的name屬性要和上面的一致,referencedColumnName表示關聯的哪一個屬性
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="COUNTRY",updatable=false,insertable=false,referencedColumnName="C_KEY")
private CountryView country;

@Column(name = "CITY")
private String city;

@Column(name="CONTACT")
private String contact;

@Column(name="LINKMAN")
private String linkMan;

@Column(name="DESCRIPTION")
private String description;

@Column(name="ADDINFO")
private String company;

@Column(name = "VISIT_ID")
private Long visitId;




@Column(name = "CRT_TIME")
@Temporal(TemporalType.TIMESTAMP)
private Date createDate;


@Column(name = "CRT_USER_ID")
private Long createBy;


@Column(name = "UPD_TIME")
@Temporal(TemporalType.TIMESTAMP)
private Date updateDate = new Date();


@Column(name = "UPD_USER_ID")
private Long updateBy;


@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "VISIT_ID", updatable = false, insertable = false)
private VisitApply visitApply;

@Column(name="DESC_MORE")
private String desc;

//表中沒有的字段,但是需要在頁面中顯示出來,可以用@Transient表示
@Transient
private String code;

@Transient
private Date visitDate;

@Transient
private Integer count;

@Transient
private String attitude;

@Transient
private String currentSystem;

@Transient
private Date dateFrom;


@Transient
private Date dateTo;



@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}


@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Classroom other = (Classroom) obj;
if (id == null) {
if (other.getId() != null)
return false;
} else if (!id.equals(other.getId()))
return false;
return true;
}

}



dao層

@Repository
public class VisitorInfoDao extends BaseDAO<VisitorInfo>{
   
public PagingVO findByFilter(PagingVO page, VisitorInfoVo visitorInfoVo){
DetachedCriteriaBuilder query=initQuery(visitorInfoVo);
DetachedCriteriaBuilder count=initQuery(visitorInfoVo);
return this.selectPagingVO(query, page,count);
}


private DetachedCriteriaBuilder initQuery(VisitorInfoVo visitorInfoVo) {
DetachedCriteriaBuilder query=DetachedCriteriaBuilder.instance(VisitorInfo.class,"vi");

query.leftJoin("visitApply", "va");
query.leftJoin("country", "country");
   

                //select語句


query.setProjection(Projections.projectionList().
              add(Projections.property("vi.name"),"name").    
              add(Projections.property("vi.company"),"company").
              add(Projections.property("vi.position"),"position").
              add(Projections.property("vi.contact"),"contact").
              add(Projections.property("country.value"),"countryId"). 
              add(Projections.property("city"),"city").
              add(Projections.property("va.code"),"code").
              add(Projections.property("va.dateFrom"),"dateFrom").
              add(Projections.property("va.dateTo"),"dateTo").
              add(Projections.property("va.visitorCount"),"count").
              add(Projections.property("va.currentSystem"),"currentSystem").
              add(Projections.property("va.attitude"),"attitude"));
query.addEq("country.locale", visitorInfoVo.getLocale().getLanguage());   //是date變爲string類型


             //因爲業務的需要根據時間條件查詢時,必須精確到時分秒,但是在頁面上不顯示時分秒
if(visitorInfoVo.getDate()!=null){
Calendar c=Calendar.getInstance();
c.setTime(visitorInfoVo.getDate());
//<=
c.set(Calendar.HOUR_OF_DAY, 23);
query.addLe("va.createDate", c.getTime());

c.set(Calendar.HOUR_OF_DAY, 0);
//>=
query.addGe("va.createDate", c.getTime());



query.addLikeAny("vi.name", visitorInfoVo.getName());
query.addLikeAny("vi.contact", visitorInfoVo.getTel());
query.addLikeAny("vi.company", visitorInfoVo.getCompany());
query.getDetachedCriteria().setResultTransformer(new AliasToBeanResultTransformer(VisitorInfo.class));
return query;
}
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章