Java Persistence / JPA:@Column vs @Basic [英] Java Persistence / JPA: @Column vs @Basic

查看:76
本文介绍了Java Persistence / JPA:@Column vs @Basic的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JPA中的 @Column @Basic 注释有什么区别?它们可以一起使用吗? 应该一起使用吗?或者其中一个是否足够?

What is the difference between @Column and @Basic annotations in JPA? Can they be used together? Should they be used together? Or does one of them suffice?

推荐答案


  • @Basic 表示要保留属性并使用标准映射。它有一些参数,允许您指定是否要延迟加载属性以及它是否可以为空。

    • @Basic signifies that an attribute is to be persisted and a standard mapping is to be used. It has parameters which allow you to specify whether the attribute is to be lazily loaded and whether it's nullable.

      @Column 允许您指定要将属性保留到的数据库中的列的名称。

      @Column allows you to specify the name of the column in the database to which the attribute is to be persisted.

      如果您指定一个没有另一个,那么您将获得合理的默认行为,因此通常人们只使用一个特殊情况除外。

      If you specify one without the other then you get default behaviour which is sensible, so commonly folks use only one with the exception of special cases.

      因此,如果我们想要延迟加载属性并指定列名,我们可以说

      So if we wanted a lazy loading of an attribute and to specify a column name we can say

       @Basic(fetch=FetchType.LAZY)
       @Column(name="WIBBLE")
      

      如果我们需要默认的非惰性行为,那么只需 @Column 即可。

      If we neeed the default, non-lazy behaviour then just the @Column would have been sufficient.

      这篇关于Java Persistence / JPA:@Column vs @Basic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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