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 {
    }
  • 相关阅读:
    目前加尼福尼亚自动驾驶公司测试公司————20150529
    DDR3
    Linux mysql 5.7: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
    macOS 10.12,解决如何打开隐私中的任何来源方法
    git查看某个文件的提交历史
    ios-deploy命令
    sed简用
    啊,栈溢出了
    二叉树题目总结(一)
    线段树(二)
  • 原文地址:https://www.cnblogs.com/stit/p/3954411.html
Copyright © 2011-2022 走看看