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 {
    }
  • 相关阅读:
    组合数
    2019牛客暑期多校训练营第一场 E ABBA
    2019牛客暑期多校训练营(第一场) H XOR
    POJ-1742Coins
    POJ 1384 Piggy-Bank
    operator.itemgetter函数
    常用内置模块之itertools
    解决Django REST framework中的跨域问题
    关于模板渲染冲突的问题
    什么是跨域?以及如何解决跨域问题?
  • 原文地址:https://www.cnblogs.com/stit/p/3954411.html
Copyright © 2011-2022 走看看