【发布时间】:2017-08-02 17:19:51
【问题描述】:
目前我正在学习 Spring 框架,主要关注它的安全模块。我看过一些与注册和登录有关的指南。我在 User 类的密码字段中看到了 transient 关键字或 @Transient 注释的常见用法。
我的虚拟应用正在使用 Spring Boot + Spring MVC + Spring Security + MySQL。
我知道
Java 的 transient 关键字用于表示字段不被序列化。
JPA 的 @Transient 注解...
...指定属性或字段不是持久的。它用于注释实体类、映射超类或可嵌入类的属性或字段。
还有 org.springframework.data.annotation 的 @Transient 注释...
将字段标记为映射框架的瞬态。因此,属性不会被持久化,也不会被映射框架进一步检查。
在我的 MySQL 数据库中,我的 spring_demo 模式有 3 个表:
+-----------------------+
| Tables_in_spring_demo |
+-----------------------+
| role |
| user |
| user_role |
+-----------------------+
当我在 User 类的密码字段上使用 transient 关键字时,它不会存储在 MySQL 数据库中。 (例如:test01)
mysql> select * from user;
+----+--------+------------------+----------+
| id | active | email | username |
+----+--------+------------------+----------+
| 1 | 1 | test01@gmail.com | test01 |
+----+--------+------------------+----------+
1 row in set (0,00 sec)
当我在 User 类的密码字段上使用 javax.persistence @Transient 注释时,它也不会存储在 MySQL 数据库中。 (例如:test02)
但是...当我在 User 类的密码字段上使用 org.springframework.data.annotation @Transient 注释时,它确实存储在 MySQL 数据库中。 (例如:test03)这是为什么?
mysql> select * from user;
+----+--------+------------------+----------+--------------------------------------------------------------+
| id | active | email | username | password |
+----+--------+------------------+----------+--------------------------------------------------------------+
| 1 | 1 | test02@gmail.com | test02 | |
| 2 | 1 | test03@gmail.com | test03 | $2a$10$UbvmdhfcKxSNr/I4CjOLtOkKGX/j4/xQfFrv3FizxwEVk6D9sAoO |
+----+--------+------------------+----------+--------------------------------------------------------------+
2 rows in set (0,00 sec)
我的主要问题是,当我使用基于 spring.data 的 @Transient 注释时,密码字段一直存在。为什么?我为什么要在密码字段上使用任何 @Transient 注释?
提前感谢您的指导和帮助!
【问题讨论】:
标签: jpa spring-security spring-data spring-annotations transient