比如现在有实体类Message,但是我想在使用它时想要多用一个字段来接收未读消息的数量 unReadCount,此时只要在字段前加上@Transient注解就可以了。记住要引入 import javax.persistence.Transient;
@Entity
@Table(name = "message")
@DynamicUpdate
@DynamicInsert
@Data
public class Message extends BaseEntity {
/*接收人id*/
private Long receiveCustomerId;
/*发送人id*/
private Long sendCustomerId;
/*标题*/
private String title;
/*内容*/
private String content;
/*通知信息*/
private Integer msgType;
/*0未读 1已读*/
private Integer status;
@Transient
private Long unReadCount;
2020-5-20
@Transient注解标识的字段,在springJpa中使用的时候.进行多表联查,对查询结果进行接收的时候该字段接收到的值为null;
因此查找其他方法
@RequestMapping(value = "/test",method = RequestMethod.POST)
public String test(@RequestParam("classifyId")String classifyId){
return classifyService.test(classifyId);
}
public String test(String classifyId) {
try {
List<Object[]> select = classifyInformationDao.findByAdminId(classifyId);
List<Test> testViews = castEntity(select, Test.class);
for (Test testView : testViews) {
System.out.println(testView.toString());
}
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//转换实体类
public static <T> List<T> castEntity(List<Object[]> list, Class<T> clazz) throws Exception {
List<T> returnList = new ArrayList<T>();
if (CollectionUtils.isEmpty(list)) {
return returnList;
}
Object[] co = list.get(0);
Class[] c2 = new Class[co.length];
//确定构造方法
for (int i = 0; i < co.length; i++) {
if (co[i] != null) {
c2[i] = co[i].getClass();
} else {
c2[i] = String.class;
}
}
for (Object[] o : list) {
Constructor<T> constructor = clazz.getConstructor(c2);
returnList.add(constructor.newInstance(o));
}
return returnList;
}
Test实体是多表联查查询到的数据封装实体.