可以在hbm.xml中映射的属性是暂时的吗? [英] Can properties mapped in hbm.xml be transient?

查看:105
本文介绍了可以在hbm.xml中映射的属性是暂时的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个 User 这样的实体:

  class用户{
私人字符串登录;
暂时私有字符串hashedPassword;
}

我不希望传输 hashedPassword 给客户端,所以我把它定义为临时的。



这个类由Hibernate映射,两个字段映射在hbm.xml中。



这个实现是否安全正确? Hibernate会在数据库中正确存储 hashedPassword ,将它从数据库中加载到对象中,并将它保存在复制的二级缓存和本地会话缓存等中?



换句话说,Hibernate或二级缓存是否以任何方式尊重 transient 或完全忽略它?



编辑:我已经得到了两个答案,似乎没有包含方程的一个特定方面。我根本不使用注释,只使用 hbm.xml 中的XML映射。并且这个Java瞬态字段在 hbm.xml 中进行了或映射。

解决方案

未映射/瞬态属性不会被hibernate保存

Hibernate理解标准java瞬态修饰符的重要性 - 但也允许您使用@Transient注释将属性注释为瞬态,如果您愿意选择...或者只是离开该字段完全不在您的映射文件中。

在你的情况中,你可能不需要做任何特殊的事情,hibernate应该简单地做正确的事情,忽略未映射的字段。
$ b

所以:这里学到的教训 -



如果只使用hbm.xml



1)未映射的属性不会通过休眠保存 - 它们实际上是瞬态的。 2)Hibernate将忽略保存@Transient,如果使用POJO


$ b 注释变量:

  @Transient 
int ignored = 0;



<3> Hibernate也会忽略使用标准的transient修饰符保存变量:

  private transient int ignored = 0; 

请参阅 http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/ 对此进行了很好的说明。

Suppose I have a User entity like this:

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

I don't want to ever transfer hashedPassword to clients, so I make it transient.

This class is mapped by Hibernate, with both fields mapped in 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?

In order words, does Hibernate or 2nd level cache respect transient in any way or completely ignore it?

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. And this Java-transient field is OR-mapped in hbm.xml.

解决方案

Unmapped/Transient properties are not saved by 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.

In your case, you probably will NOT need to do anything special, hibernate should simply "do the right thing", by ignoring unmapped fields.

So : the lesson learned here -

If only using hbm.xml

1) Unmapped properties are not saved by hibernate - they are effectively transient.

If using POJOs

2) Hibernate will ignore saving "@Transient" annotated variables :

@Transient
int ignored=0;

3) Hibernate will also ignore saving variables with standard "transient" modifiers :

private transient int ignored =0;

See http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/ for an excellent explanation of this.

这篇关于可以在hbm.xml中映射的属性是暂时的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆