当前位置: 首页 > 知识库问答 >
问题:

放一个注释@Lob与否有什么区别?

邵俊才
2023-03-14

我的数据库中有一个请求表。这里是它的结构

36086c79ac2.png" width="100%" height="100%" />

通常我用手创建实体,但这次我决定从数据库生成它。这是我得到的

@Entity
@Table(name = "request")
public class Request
{
    @Id
    @Column(name = "id", nullable = false)
    private Integer id;

    @Lob
    @Column(name = "body")
    private String body;

    @Column(name = "description", length = 10000)
    private String description;

    @Lob
    @Column(name = "headers")
    private String headers;

    @Column(name = "http_method", nullable = false, length = 20)
    private String httpMethod;

    @Column(name = "name", nullable = false, length = 100)
    private String name;

    @Lob
    @Column(name = "path_variables")
    private String pathVariables;

    @Column(name = "port")
    private Integer port;

    @Lob
    @Column(name = "query_params")
    private String queryParams;

    @Lob
    @Column(name = "url", nullable = false)
    private String url;

我注意到出现了一个奇怪的注释@Lob。我以前从未见过她。我决定用谷歌搜索它的意思以及为什么要放它。我找到了很多答案,他们都说需要这个注释,以便Hibernate理解这里可能有一个大对象。

但在我看来,这个注释没有任何作用。毕竟,不管是否值得,一切都是一样的?

它是干什么用的?

共有1个答案

华永新
2023-03-14
LOB or Large OBject refers to a variable length datatype for storing large objects.

This datatype has two variants as follows :
CLOB – Character Large Object will store large text data
BLOB – Binary Large Object is for storing binary data like image, audio, or video.

When we have the string field in a entity class, The hibernate framework will create the table with string fields as varchar datatype with maximum value of 255 (ie:  varchar(255)) by default

but , If we add the @LOB annotation to string field it will be changed as longtext from the varchar datatype in table, then we can able to store the large amount of character to specific column  .

For Example , to  Create a  materialized mappings , You just need an attribute of type String or byte[] and annotate it with  @Lob annotation.

@Entity
public class Book {
 
    @Id
    @GeneratedValue
    private Long id;
 
    private String title;
     
    @Lob
    private String content;
     
    @Lob
    private byte[] cover;
 
    ...
}

结论:@Lob注释用于将大值的字段/属性映射到相应的数据库支持的大对象类型。

Lob可以是二进制类型或字符类型。

 类似资料:
  • 问题内容: 方法和注释之间有什么区别?他们是一样的吗? 例如,这是: 与: 问题答案: 它们都达到相同的结果。通常不会使用注解(),因为您不会用看起来都一样的样板分配来填充代码。 请注意,为了使用注释,您的测试类应在其方法中进行注释或包含对的调用。

  • 问题内容: @Inject和@Resource以及@Autowired注释有什么区别? 我们什么时候应该使用它们? 问题答案: 和注释@Inject和有什么区别?@Resource@Autowired 我们什么时候应该使用它们?@Inject与@Autowire与@Resource之间的区别? @Autowired:spring专有注释(与@Inject和@Resource相反),按类型(即,通过

  • 问题内容: 我大致了解这种构造的作用:它创建了SomeType EJB,并将对象注入到另一个EJB中。 现在,我有一个以这样的方式开始的类:(尽管我认为只有的相关,我会给出所有类级别的注释) 什么的就做吗?他们可能会从JNDI获取或创建“ name1” …对象,但是将结果放在哪里?我看不到附近有任何电话,但是代码库很大,所以我对此不太确定。 额外的问题:我想这两个注释只是重复默认值? 更新:目前有

  • 问题内容: 嵌入式注释如何影响数据库? SQL查询将如何改变? 使用注释的典型用例是什么? 问题答案: 嵌入式注释如何影响数据库? 它根本不影响它。在ORM提供程序层上,来自嵌入式实体的所有字段都 将 与父实体 合并 ,并像对待它们始终在其中声明一样。换句话说,它的工作方式就好像您将所有字段,获取器和设置器直接复制到包含嵌入式对象的实体中一样。 SQL查询将如何改变? 他们不会。您无需更改任何内容