简体   繁体   中英

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. I'm using Spring data JPA, Spring boot.

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. So, the CLOB column of entity should look like this.

@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
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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