简体   繁体   English

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

[英]Convert String to CLOB in Spring data JPA

I have large text which is in String format.我有字符串格式的大文本。 I would like to know how to convert that String into CLOB.我想知道如何将该字符串转换为 CLOB。 I'm using Spring data JPA, Spring boot.我正在使用 Spring 数据 JPA,Spring 引导。

I have tried using我试过使用

clob.setString(position, string)

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.在 Spring Data JPA 中应该有一个实体,它是 String 并且需要在 DB 中保存为 CLOB。 So, the CLOB column of entity should look like this.因此,实体的 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 的方式。

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

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