java 在Spring数据JPA中将String转换为CLOB

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41470901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:54:30  来源:igfitidea点击:

Convert String to CLOB in Spring data JPA

javaspringspring-bootspring-data-jpa

提问by Samantha

I have large text which is in String format. I would like to know how to convert that String into CLOB. I'm using Spring data JPA, Spring boot.

我有字符串格式的大文本。我想知道如何将该字符串转换为 CLOB。我正在使用 Spring 数据 JPA,Spring 引导。

I have tried using

我试过使用

clob.setString(position, string)

回答by Avinash

Without dragging the question further I want to answer it simply.

没有进一步拖延问题,我想简单地回答一下。

In Spring Data JPA there should be an entity which is String and needed to be saved as CLOB in DB. So, the CLOB column of entity should look like this.

在 Spring Data JPA 中应该有一个实体是 String 并且需要在 DB 中保存为 CLOB。因此,实体的 CLOB 列应如下所示。

@Entity
public class SampleData {
    // other columns 

    @Column(name="SAMPLE", columnDefinition="CLOB NOT NULL") 
    @Lob 
    private String sample;

    // setters and getters
}

Then you should have a Repository like below

那么你应该有一个像下面这样的存储库

public interface SampleDataRepo extends PagingAndSortingRepository<SampleData, Integer> {

}

Now in Service method you could do something like below

现在在 Service 方法中,您可以执行以下操作

@Service
public class SampleDataService {

    @Autowire 
    SampleDataRepo repo;

    public SampleData saveSampleData() {
        SampleData sd = new SampleData();
        sd.setSample("longtest");

        repo.save(sd);
    }
}

This is how the String data is saved as CLOB in DB.

这就是字符串数据在 DB 中保存为 CLOB 的方式。