简体   繁体   English

如何使 @Lob 注释与 oracle 和 postgresql 兼容

[英]how to make @Lob annotation compatible for both oracle and postgresql

My code is designed to support both oracle and postgresql, but the @Lob annotation behaved different from oracle to pg, in oracle @Lob with Java String Object type works well, but in pg, I should add annotation @Type(type ="org.hibernate.type.TextType") to make String mapping to pg text format, which makes it not compatible with oracle. My code is designed to support both oracle and postgresql, but the @Lob annotation behaved different from oracle to pg, in oracle @Lob with Java String Object type works well, but in pg, I should add annotation @Type(type ="org .hibernate.type.TextType") 将String映射为pg文本格式,这使得它与oracle不兼容。 How to make it possible to support oracle and pg in just one JPA entity class?如何在一个 JPA 实体 class 中支持 oracle 和 pg?

Here is my entity class.这是我的实体 class。

package hello.world.demo3;

import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Type;

import javax.persistence.*;

@Entity
@Table(name = "my_task")
@Getter
@Setter
public class Task {
    
    @Id
    @GeneratedValue
    @Column(name = "i_id", nullable = false)
    private Long id;
    
    @Lob
    @Column(name = "c_lob")
    private String lob;

    @Lob
    @Type(type ="org.hibernate.type.TextType")
    @Column(name = "c_text")
    private String text;

}

I suggest using the columnDefinition in the @Column annotation to specify that the field should be TEXT type in the underlying SQL table.我建议使用@Column注解中的columnDefinition来指定该字段应该是底层SQL表中的TEXT类型。

@Column(name = "c_text", columnDefinition = "TEXT")
private String text;

Note that specifying @Lob will probably not give you the behavior you want in Postgres.请注意,指定@Lob可能不会给您在 Postgres 中想要的行为。 In Postgres, using @Lob will instruct the database to create an auxiliary table with the purpose of storing large binary content.在 Postgres 中,使用@Lob将指示数据库创建一个辅助表,用于存储大量二进制内容。

Do not use LOMBOK for multipurpose database, its not helping you.不要将 LOMBOK 用于多用途数据库,它对您没有帮助。

Put annotations on methods forces JPA to access properties via methods.在方法上添加注释会强制 JPA 通过方法访问属性。 It makes sense when internal state of your object differs from the database schema:当您的 object 的内部 state 与数据库模式不同时,这是有道理的:

private String text;

@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "text")
public String getText(text ) {
    return text;
}
public void setText(String text) {
    text = text;
}

read this: source阅读: 来源

I use this method for local Postgres development and Oracle Server both.我将此方法用于本地 Postgres 开发和 Oracle Server 两者。

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

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