我实际上是在寻找一个“@Ignore”类型的注释,我可以用它来停止某个特定字段的持久化。如何实现这一目标?
当前回答
None of the above answers worked for me using Hibernate 5.2.10, Jersey 2.25.1 and Jackson 2.8.9. I finally found the answer (sort of, they reference hibernate4module but it works for 5 too) here. None of the Json annotations worked at all with @Transient. Apparently Jackson2 is 'smart' enough to kindly ignore stuff marked with @Transient unless you explicitly tell it not to. The key was to add the hibernate5 module (which I was using to deal with other Hibernate annotations) and disable the USE_TRANSIENT_ANNOTATION feature in my Jersey Application:
ObjectMapper jacksonObjectMapper = new ObjectMapper();
Hibernate5Module jacksonHibernateModule = new Hibernate5Module();
jacksonHibernateModule.disable(Hibernate5Module.Feature.USE_TRANSIENT_ANNOTATION);
jacksonObjectMapper.registerModule(jacksonHibernateModule);
下面是Hibernate5Module的依赖:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate5</artifactId>
<version>2.8.9</version>
</dependency>
其他回答
为了完成上面的回答,我使用了一个XML映射文件,其中@Transient和transient都不工作… 我必须把临时信息放在xml文件中:
<attributes>
(...)
<transient name="field" />
</attributes>
这个回答来的有点晚,但它完成了响应。
为了避免来自实体的字段被持久化到DB中,可以使用以下两种机制之一:
@Transient—标记字段不可持久的JPA注释
java中的Transient关键字。注意-使用此关键字将阻止该字段与java中的任何序列化机制一起使用。因此,如果字段必须序列化,最好只使用@Transient注释。
None of the above answers worked for me using Hibernate 5.2.10, Jersey 2.25.1 and Jackson 2.8.9. I finally found the answer (sort of, they reference hibernate4module but it works for 5 too) here. None of the Json annotations worked at all with @Transient. Apparently Jackson2 is 'smart' enough to kindly ignore stuff marked with @Transient unless you explicitly tell it not to. The key was to add the hibernate5 module (which I was using to deal with other Hibernate annotations) and disable the USE_TRANSIENT_ANNOTATION feature in my Jersey Application:
ObjectMapper jacksonObjectMapper = new ObjectMapper();
Hibernate5Module jacksonHibernateModule = new Hibernate5Module();
jacksonHibernateModule.disable(Hibernate5Module.Feature.USE_TRANSIENT_ANNOTATION);
jacksonObjectMapper.registerModule(jacksonHibernateModule);
下面是Hibernate5Module的依赖:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate5</artifactId>
<version>2.8.9</version>
</dependency>
使用@Transient使JPA忽略字段。
但是!Jackson也不会序列化该字段。要解决这个问题,只需添加@JsonProperty
一个例子
@Transient
@JsonProperty
private boolean locked;
要忽略一个字段,请使用@Transient注释它,这样它就不会被hibernate映射。
但是jackson在转换为JSON时不会序列化字段。
如果你需要混合JPA和JSON(JPA省略,但仍然包含在Jackson中)使用@JsonInclude:
@JsonInclude()
@Transient
private String token;
TIP:
你也可以使用JsonInclude.Include。当token == null时,在反序列化期间在JSON中隐藏NON_NULL字段:
@JsonInclude(JsonInclude.Include.NON_NULL)
@Transient
private String token;
推荐文章
- 比较两个SQL Server数据库(模式和数据)的最佳工具是什么?
- 如何在java中格式化持续时间?(如格式H:MM:SS)
- urlencoder .encode(字符串)已弃用,我应该使用什么代替?
- javax.transaction.Transactional vs . org.springframework.transaction.annotation.Transactional
- Java 8接口方法中不允许“同步”的原因是什么?
- 如何找到Java堆大小和内存使用(Linux)?
- Spring引导——不是托管类型
- 使用Enum实现单例(Java)
- 如何检查一个表是否存在于给定的模式中
- RabbitMQ与通道和连接之间的关系
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何在SQL Server Management Studio中查看查询历史
- 如何在Java中按两个字段排序?
- 文件之间的差异。路径中的分隔符和斜杠