springMVC中的實體映射以及兩表關聯時的映射

例1:(多對一)

@Column(name = "VISIT_ID")   (這是本表的一個字段)

private Long visitId;


@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "VISIT_ID", updatable = false, insertable = false)  //默認和VisitApply的主鍵相關聯
private VisitApply visitApply;


Dao層的使用:

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


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



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());
if (visitorInfoVo.getDate() != null) {
query.addLt("va.createDate", PssDateWrapper.instance(visitorInfoVo.getDate()).toNextDay().toOneDayBegin().toDate());
query.addGe("va.createDate", PssDateWrapper.instance(visitorInfoVo.getDate()).toOneDayBegin().toDate());
}
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;
}




例2:(一對多)

@OneToMany(targetEntity = Classes.class, fetch = FetchType.LAZY)
@JoinColumn(name = "TRAINNING_ID", updatable = false, insertable = false) (這是指classes表TRAINNING_ID
List<Classes> sessionList = Lists.newArrayList();



Dao層的使用:

public List<String> getCourseCatalogByApplyIds(List<Long> applyIds, Long userId) {

if (CollectionUtils.isEmpty(applyIds)) {
return null;
}
DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(CourseCatalog.class, "cc");
DetachedCriteriaBuilder subBuilder = DetachedCriteriaBuilder.instance(TrainingApply.class, "ta");
subBuilder.addIn("ta.id", applyIds);
subBuilder.addEq("ta.userId", userId);
subBuilder.setProjection(Projections.property("ta.courseCatalogId"));
builder.addIn("cc.id", this.selectE(subBuilder));
builder.setProjection(Projections.property("cc.title"));
return this.selectE(builder);
}



例3:(一般字段的映射)

@Column(name = "publish_on")
@Temporal(TemporalType.TIMESTAMP)
private Date publishOn;


@Column(name = "is_active")
@Type(type = "org.hibernate.type.NumericBooleanType")
private Boolean active = Boolean.TRUE;



枚舉類型:


public static enum ClassStatus {
Creating, Open, Full, Completed, Cancelled
}


public static enum ClassroomType {
Dummy(0), Classroom(1), Lab(2);
Integer status;


ClassroomType(Integer status){
this.status = status;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}


@Transient  (來自其他的表中的字段)
private String currentSystem;




例4 多對一(與非主鍵相關聯)

@Column(name="COUNTRY")
private String countryId;

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="COUNTRY",updatable=false,insertable=false,referencedColumnName="C_KEY")
private CountryView country;





麻煩大家給點兒評論咯

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