SpringBoot项目中 想在一个实体类中添加一个属性,不关联实体类对应的表的任何字段,只在逻辑中查询传值使用. 但是在属性上使用@Transient注解报错,提示在表中找不到该属性对应的字段。
实体类:
package com.compere.flowable.main.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.compere.flowable.web.domain.BaseEntity;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
/**
* @author xyz
* @date 2020年3月23日
*/
@Data
@ToString
@Accessors(chain = true)
@TableName("T_FLOWABLE_FORM")
public class FlowableForm extends BaseEntity<FlowableForm> {
private static final long serialVersionUID = 1L;
@TableId
@NotNull
private String formKey;
@NotNull
private String formName;
private String formJson;
/**
* 修改标识 修改:1 修改表单:2
**/
@Transient
private String flag;
}
网上的教程解决方案有以下几种:
1.在实体类的属性上加@Transient注解
2.在get方法上加
3.导错包(应是javax.persistence)
经过尝试 以上方法均不起作用.
猜想:
可能是因为将实体类与数据库中的表进行映射时,@Data注解,和其他方法将实体类与数据库中的表进行映射原理不同。
解决1: transient 关键字
/**
* 修改标识 修改:1 修改表单:2
**/
private transient String flag;
transient 关键字介绍,参考https://blog.csdn.net/u012723673/article/details/80699029
解决2: @TableField(exist = false)
/**
* 修改标识 修改:1 修改表单:2
**/
@TableField(exist = false)
private String flag;
网友评论