java注解@Transient的作用
@Transient标注的属性,不会被ORM框架映射到数据库中。
用于数据库表字段和java实体属性不一致的时候,标注在属性上使用。
例如时间段的查询 查询
Receipt
的createDate
那么可以在
Receipt
实体中加两个字段:
//起始时间
@Transient
private Date startDate;
//结束时间
@Transient
private Date endDate;
这两个字段并不会在数据库中出现。
但是前台传递过来的时间段,就可以使用这两个字段做接收,然后在JPA查询条件中,
在实体中加上下面这个方法:
package com.pisen.cloud.luna.ms.security.code.base.domain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;
import javax.persistence.*;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 入库单
*/
@Entity
@Table(
uniqueConstraints = {
@UniqueConstraint(columnNames = "uid"),
@UniqueConstraint(columnNames = {"receiptCode","tenementId"})
},
indexes = {
@Index(columnList = "receiptCode")
}
)
public class Receipt {
public static final Integer SOURCE_PAGE_ADD = 1; //入库单来源-页面新增
public static final Integer SOURCE_CODE_STORAGE= 2;//入库单来源-生码入库
public static final Integer SOURCE_PACKING_STORAGE = 3;//入库单来源-装箱入库
public static final Integer SOURCE_SCAN_CODE_STORAGE = 4;//入库单来源-扫码入库
public static final Integer STATUS_RECEIPT_CREATE_ING = 1;//入库状态-清单生成中
public static final Integer STATUS_RECEIPT_FAIL = 2;//入库状态-清单生成失败
public static final Integer STATUS_RECEIPT_COMPELETE = 3;//入库状态-已入库
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; //主键
private String uid;//业务主键
private String createUid;// 创建人id
private String createName;//入库单创建人
private Date createDate;// 入库单创建时间 入库日期
private String updateId;// 修改人id
private Date updateDate;// 入库单修改时间
private String remark;//备注
private String receiptCode;// 入库单号 租户内唯一
private Integer receiptSource;// 入库单来源
private Integer receiptStatus;// 入库状态
private String tenementId;//租户ID
private String depotId;//仓库ID
private String receiptDepot;// 入库方仓库 仓库名称
private String depotAddress;//仓库地址
private String outerCode;//外部单号 暂时隐藏
//第几页
@Transient
private Integer pageNum = 0;
//每页多少条
@Transient
private Integer pageSize = 10;
//起始时间
@Transient
private Date startDate;
//结束时间
@Transient
private Date endDate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getCreateUid() {
return createUid;
}
public void setCreateUid(String createUid) {
this.createUid = createUid;
}
public String getCreateName() {
return createName;
}
public void setCreateName(String createName) {
this.createName = createName;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public String getUpdateId() {
return updateId;
}
public void setUpdateId(String updateId) {
this.updateId = updateId;
}
public Date getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getReceiptCode() {
return receiptCode;
}
public void setReceiptCode(String receiptCode) {
this.receiptCode = receiptCode;
}
public Integer getReceiptSource() {
return receiptSource;
}
public void setReceiptSource(Integer receiptSource) {
this.receiptSource = receiptSource;
}
public Integer getReceiptStatus() {
return receiptStatus;
}
public void setReceiptStatus(Integer receiptStatus) {
this.receiptStatus = receiptStatus;
}
public String getDepotAddress() {
return depotAddress;
}
public void setDepotAddress(String depotAddress) {
this.depotAddress = depotAddress;
}
public String getTenementId() {
return tenementId;
}
public void setTenementId(String tenementId) {
this.tenementId = tenementId;
}
public String getDepotId() {
return depotId;
}
public void setDepotId(String depotId) {
this.depotId = depotId;
}
public String getReceiptDepot() {
return receiptDepot;
}
public void setReceiptDepot(String receiptDepot) {
this.receiptDepot = receiptDepot;
}
public String getOuterCode() {
return outerCode;
}
public void setOuterCode(String outerCode) {
this.outerCode = outerCode;
}
public Integer getPageNum() {
return pageNum;
}
public void setPageNum(Integer pageNum) {
this.pageNum = pageNum;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
//高级查询
public static Specification<Receipt> where(final Receipt receipt){
return new Specification<Receipt>() {
@Override
public Predicate toPredicate(Root<Receipt> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<Predicate>();
//动态条件
String uid = receipt.getUid();
if(StringUtils.isNotBlank(uid)){
predicates.add(cb.equal(root.<String>get("uid"), uid));
}
//入库单号
String receiptCode = receipt.getReceiptCode();
if (StringUtils.isNotBlank(receiptCode)) {
predicates.add(cb.equal(root.<String>get("receiptCode"), receiptCode));
}
//入库单来源
Integer receiptSource = receipt.getReceiptSource();
if (receiptSource != null) {
predicates.add(cb.equal(root.<String>get("receiptSource"), receiptSource));
}
//入库日期
Date startDate = receipt.getStartDate();
Date endDate = receipt.getEndDate();
if (startDate != null && endDate != null){
predicates.add(cb.between(root.get("createDate"),startDate,endDate));
}
//入库状态
Integer receiptStatus = receipt.getReceiptStatus();
if (receiptStatus != null) {
predicates.add(cb.equal(root.<String>get("receiptStatus"), receiptStatus));
}
//租户id
String tenementId = receipt.getTenementId();
if(StringUtils.isNotBlank(tenementId)){
predicates.add(cb.equal(root.<String>get("tenementId"), tenementId));
}
return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
}
};
}
}
package com.pisen.cloud.luna.ms.security.code.base.domain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;
import javax.persistence.*;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 入库单
*/
@Entity
@Table(
uniqueConstraints = {
@UniqueConstraint(columnNames = "uid"),
@UniqueConstraint(columnNames = {"receiptCode","tenementId"})
},
indexes = {
@Index(columnList = "receiptCode")
}
)
public class Receipt {
public static final Integer SOURCE_PAGE_ADD = 1; //入库单来源-页面新增
public static final Integer SOURCE_CODE_STORAGE= 2;//入库单来源-生码入库
public static final Integer SOURCE_PACKING_STORAGE = 3;//入库单来源-装箱入库
public static final Integer SOURCE_SCAN_CODE_STORAGE = 4;//入库单来源-扫码入库
public static final Integer STATUS_RECEIPT_CREATE_ING = 1;//入库状态-清单生成中
public static final Integer STATUS_RECEIPT_FAIL = 2;//入库状态-清单生成失败
public static final Integer STATUS_RECEIPT_COMPELETE = 3;//入库状态-已入库
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; //主键
private String uid;//业务主键
private String createUid;// 创建人id
private String createName;//入库单创建人
private Date createDate;// 入库单创建时间 入库日期
private String updateId;// 修改人id
private Date updateDate;// 入库单修改时间
private String remark;//备注
private String receiptCode;// 入库单号 租户内唯一
private Integer receiptSource;// 入库单来源
private Integer receiptStatus;// 入库状态
private String tenementId;//租户ID
private String depotId;//仓库ID
private String receiptDepot;// 入库方仓库 仓库名称
private String depotAddress;//仓库地址
private String outerCode;//外部单号 暂时隐藏
//第几页
@Transient
private Integer pageNum = 0;
//每页多少条
@Transient
private Integer pageSize = 10;
//起始时间
@Transient
private Date startDate;
//结束时间
@Transient
private Date endDate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getCreateUid() {
return createUid;
}
public void setCreateUid(String createUid) {
this.createUid = createUid;
}
public String getCreateName() {
return createName;
}
public void setCreateName(String createName) {
this.createName = createName;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public String getUpdateId() {
return updateId;
}
public void setUpdateId(String updateId) {
this.updateId = updateId;
}
public Date getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getReceiptCode() {
return receiptCode;
}
public void setReceiptCode(String receiptCode) {
this.receiptCode = receiptCode;
}
public Integer getReceiptSource() {
return receiptSource;
}
public void setReceiptSource(Integer receiptSource) {
this.receiptSource = receiptSource;
}
public Integer getReceiptStatus() {
return receiptStatus;
}
public void setReceiptStatus(Integer receiptStatus) {
this.receiptStatus = receiptStatus;
}
public String getDepotAddress() {
return depotAddress;
}
public void setDepotAddress(String depotAddress) {
this.depotAddress = depotAddress;
}
public String getTenementId() {
return tenementId;
}
public void setTenementId(String tenementId) {
this.tenementId = tenementId;
}
public String getDepotId() {
return depotId;
}
public void setDepotId(String depotId) {
this.depotId = depotId;
}
public String getReceiptDepot() {
return receiptDepot;
}
public void setReceiptDepot(String receiptDepot) {
this.receiptDepot = receiptDepot;
}
public String getOuterCode() {
return outerCode;
}
public void setOuterCode(String outerCode) {
this.outerCode = outerCode;
}
public Integer getPageNum() {
return pageNum;
}
public void setPageNum(Integer pageNum) {
this.pageNum = pageNum;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
//高级查询
public static Specification<Receipt> where(final Receipt receipt){
return new Specification<Receipt>() {
@Override
public Predicate toPredicate(Root<Receipt> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<Predicate>();
//动态条件
String uid = receipt.getUid();
if(StringUtils.isNotBlank(uid)){
predicates.add(cb.equal(root.<String>get("uid"), uid));
}
//入库单号
String receiptCode = receipt.getReceiptCode();
if (StringUtils.isNotBlank(receiptCode)) {
predicates.add(cb.equal(root.<String>get("receiptCode"), receiptCode));
}
//入库单来源
Integer receiptSource = receipt.getReceiptSource();
if (receiptSource != null) {
predicates.add(cb.equal(root.<String>get("receiptSource"), receiptSource));
}
//入库日期
Date startDate = receipt.getStartDate();
Date endDate = receipt.getEndDate();
if (startDate != null && endDate != null){
predicates.add(cb.between(root.get("createDate"),startDate,endDate));
}
//入库状态
Integer receiptStatus = receipt.getReceiptStatus();
if (receiptStatus != null) {
predicates.add(cb.equal(root.<String>get("receiptStatus"), receiptStatus));
}
//租户id
String tenementId = receipt.getTenementId();
if(StringUtils.isNotBlank(tenementId)){
predicates.add(cb.equal(root.<String>get("tenementId"), tenementId));
}
return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
}
};
}
}