当我们使用spring data jpa开发的时候,会将实体类中的成员变量与表中的字段一一对应,当我们在实体类中加上一个数据库表没有对应字段的成员变量的时候,此时我们只要在这个成员变量上加上注解@Transient
@Transient表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性. 如果一个属性并非数据库表的字段映射,务必将其标示为@Transient,否则,ORM框架默认其注解为@Basic
比如现在有实体类Message,但是我想在使用它时想要多用一个字段来接收未读消息的数量 unReadCount,此时只要在字段前加上@Transient注解就可以了。当数据库表字段与result不一致时用@Column注解
依赖包
import javax.persistence.Transient;
代码
package com.xy.base.domain;import lombok.Data;import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Date;
import java.util.List;@Entity
@Table(name = "T_SS_LLJLB")
@Data
@Accessors(chain = true)
public class TSsLljlb extends BaseEntity {public static final String TABLE = "T_SS_LLJLB";@Id@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(name = "id", nullable = false)private Integer id;@ApiModelProperty(value = "操作人")@Column(name = "user_id", length = 50)private String userId;@ApiModelProperty(value = "摄像机ID")@Column(name = "camera_id", length = 50)private String cameraId;@ApiModelProperty(value = "摄像机名称")@Column(name = "camera_name", length = 50)private String cameraName;@ApiModelProperty(value = "设备ID")@Column(name = "device_id", length = 50)private String deviceId;@ApiModelProperty(value = "通道ID")@Column(name = "channel_id", length = 50)private String channelId;@ApiModelProperty(value = "线路编码")@Column(name = "line_code", length = 50)private String lineCode;@ApiModelProperty(value = "线路名称")@Column(name = "line_name", length = 50)private String lineName;@ApiModelProperty(value = "操作类型,1-拉流 0-关流")@Column(name = "operation")private Integer operation;@ApiModelProperty(value = "拉流时间(页面请求视频流的时间)")@Column(name = "pull_time")@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")private Date pullTime;@ApiModelProperty(value = "推流时间(页面获取视频URL的时间)")@Column(name = "push_time")@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")private Date pushTime;@ApiModelProperty(value = "页面关流时间(页面请求关闭视频流的时间)")@Column(name = "page_close_time")@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")private Date pageCloseTime;@ApiModelProperty(value = "服务关流时间(分转发服务实际关闭视频流的时间)")@Column(name = "service_close_time")@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")private Date serviceCloseTime;@ApiModelProperty(value = "取流状态(取流成功-200,取流失败-报错编码)")@Column(name = "status")private Integer status;@ApiModelProperty(value = "创建记录时间")@Column(name = "create_time")@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")private Date createTime;@ApiModelProperty(value = "更新记录时间")@Column(name = "update_time")@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")private Date updateTime;@ApiModelProperty(value = "临时记录摄像头拉流次数")@Transientprivate Integer num;public TSsLljlb initTSsLljlb(TSsLljlb tSsLljlb) {return this.setCreateTime(new Date());}
}