S
S
Stepan2021-09-23 14:23:48
Spring
Stepan, 2021-09-23 14:23:48

Spring: Data truncation: Data too long for column 'page_content' at row 1, why?

I process a text file — "book". Each line of this file will later be a book page (BookPage).
What I do: I create a stream from the file and, filtering out empty lines, convert the stream into a List:

...
    stringList = fileLinesStream.filter(l -> !l.isEmpty()).collect(Collectors.toList());
...

Then I go over the elements of the list and on each repetition I create a new BookPage object, to the pageContent field of which I assign the contents of the List element:
for (int i = 0; i < stringList.size(); i++) {
    BookPage bookPage = new BookPage(book, stringList.get(i));
    bookPageService.CreatePage(bookPage); // в этом методе сервиса вызывается bookPageRepository.save(bookPage);
    }

The object is created, but at the next step ( bookPageService.CreatePage(bookPage)) Spring crashes with the error:
Data truncation: Data too long for column 'page_content' at row 1

There are only four lines in the test file, the longest of which is 430 characters.

book page

@Entity
@Table(name = "pages")
public class BookPage {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "book_id")
    private Book book;

    @Column(columnDefinition = "LONGBLOB")
    private String pageContent;

    public BookPage(Book book, String pageContent) {
        this.book = book;
        this.pageContent = pageContent;
    }

    public BookPage() {
    }
}

In the annotation to pageContent, I tried different columnDefinition values: both LONGBLOB and TEXT, but nothing changes.

What could be the reason? In data type? Any other restrictions?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stepan, 2021-09-23
@steff

It looks like the problem was that my changes in the code were not reflected in the database.
Everything was solved simply: I deleted the table, and the next time the application was launched, it was recreated.
Everything works with:

@Column(columnDefinition = "TEXT",
            name = "page_content")
    private String pageContent;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question