如何在实体类entity添加表中没有对应的字段

比如现在有实体类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实体是多表联查查询到的数据封装实体.

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值