zoukankan      html  css  js  c++  java
  • 使用注解@Transient使表中没有此字段

    注意,实体类中要使用org.springframework.data.annotation.Transient

    在写实体类时发现有加@Transient注解的

    加在属性声明上,但网上有加到get方法上的;

    1 serialization会忽略掉

    Java的serialization提供了一种持久化对象实例的机制。当持久化对象时,可能有一个特殊的对象数据成员,我们不想用serialization机制来保存它。

    为了在一个特定对象的一个域上关闭serialization,可以在这个域前加上关键字transient

    2 不跟数据库表做映射 就是表中没有这个字段

    @Transient表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性.

    在项目查了下,mongodb中确实没有此字段,因此也适用于mongodb

    个人认为,可以用于一些计算值,或缓存值,如

    http://blog.sina.com.cn/s/blog_4e64ae7a0106grty.html

    spring源码

    package org.springframework.data.annotation;
     
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
     
    /**
     * Marks a field to be transient for the mapping framework. Thus the property will not be persisted and not further
     * inspected by the mapping framework.
     * 
     * @author Oliver Gierke
     * @author Jon Brisbin
     */
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface Transient {
    }
  • 相关阅读:
    docker入门
    spring aop切面入门
    java 拉姆达 lamdba get
    Spring 3
    Spring 进阶二
    腾讯云 视频 点播 视频上传接口
    js 实时获取后台数据 Spring
    Spring 进阶一
    hibernate 第四天 重点查询的方式
    hibernate 第三天
  • 原文地址:https://www.cnblogs.com/stit/p/3954411.html
Copyright © 2011-2022 走看看