简体   繁体   English

可以在hbm.xml中映射的属性是暂时的吗?

[英]Can properties mapped in hbm.xml be transient?

Suppose I have a User entity like this: 假设我有一个像这样的User实体:

class User {
    private String login;
    transient private String hashedPassword;
}

I don't want to ever transfer hashedPassword to clients, so I make it transient. 我不想将hashedPassword转移到客户端,因此我将其设置为瞬态。

This class is mapped by Hibernate, with both fields mapped in hbm.xml. 该类由Hibernate映射,两个字段都映射在hbm.xml中。

Is this implementation safe and correct? 这种实施安全和正确吗? Will Hibernate correctly store hashedPassword in database, load it into objects from database, keep it in replicated 2nd level cache and local session cache etc? Hibernate会在数据库中正确存储hashedPassword ,将其从数据库加载到对象中,将其保存在复制的二级缓存和本地会话缓存等中吗?

In order words, does Hibernate or 2nd level cache respect transient in any way or completely ignore it? 顺便说一句,Hibernate或二级缓存是否以任何方式对transient或完全忽略它?

EDIT : I already got two answers that didn't seem to include one specific aspect of the equation. 编辑 :我已经有两个答案似乎没有包括等式的一个特定方面。 I am not using annotations at all, only XML mappings in hbm.xml . 我根本没有使用注释,只使用hbm.xml XML映射。 And this Java-transient field is OR-mapped in hbm.xml . 这个Java瞬态字段在hbm.xml OR映射。

Unmapped/Transient properties are not saved by hibernate . hibernate不保存未映射/瞬态属性

Hibernate understands the significance of standard java transient modifiers - but also allows you to annotate properties as transient using the @Transient annotation, if you so choose... Or just leave the field out of your mapping file altogether. Hibernate理解标准java瞬态修饰符的重要性 - 但是如果您选择的话,还允许您使用@Transient注释将属性注释为瞬态...或者只是将字段完全从映射文件中删除。

In your case, you probably will NOT need to do anything special, hibernate should simply "do the right thing", by ignoring unmapped fields. 在你的情况下,你可能不需要做任何特别的事情,hibernate应该简单地“做正确的事”,忽略未映射的字段。

So : the lesson learned here - 所以:这里吸取的教训 -

If only using hbm.xml 如果只使用hbm.xml

1) Unmapped properties are not saved by hibernate - they are effectively transient. 1)休眠不保存未映射的属性 - 它们实际上是瞬态的。

If using POJOs 如果使用POJO

2) Hibernate will ignore saving "@Transient" annotated variables : 2)Hibernate将忽略保存“@Transient”注释变量:

@Transient
int ignored=0;

3) Hibernate will also ignore saving variables with standard "transient" modifiers : 3)Hibernate也会忽略使用标准“瞬态”修饰符保存变量:

private transient int ignored =0;

See http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/ for an excellent explanation of this. 请参阅http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/以获得对此的出色解释。

It looks like Hibernate will not persist a field with the transient keyword, regardless of what other annotations you have. 看起来Hibernate不会持有带有transient关键字的字段,无论你有什么其他注释。

The separate @Transient annotation will allow you to direct Hibernate to ignore a non-transient field for persistence, but I don't think it's possible to do the opposite of having Hibernate persist a transient field. 单独的@Transient注释将允许您指示Hibernate忽略非瞬态字段的持久性,但我不认为可能与Hibernate持续存在transient字段相反。

Similar discussion here: 这里类似的讨论:

JPA - using annotations in a model JPA - 在模型中使用注释

Annotation @Basic to transient variables 注释@Basic到瞬态变量

The most relevant quote via above, from JPA 2.0 spec: "Mapping annotations must not be applied to fields or properties that are transient or @Transient ." 通过上面最相关的引用,来自JPA 2.0规范:“映射注释不得应用于transient@Transient字段或属性。”

Here is what I think - Hibernate is just a mapping technology. 这就是我的想法 - Hibernate只是一种映射技术。 When you mark a field as TRANSIENT it will not be persisted by java. 当您将字段标记为TRANSIENT时,它将不会被java持久化。 And since its state is not persisted why should hibernate maintain it in the L2 cache, etc ? 并且由于它的状态没有持久化,为什么hibernate应该在L2缓存中维护它等等? so hibernate should have no problem even if you map the transient field in hbm file. 所以即使你在hbm文件中映射瞬态字段,hibernate应该没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM