Hibernate - 数据截断:数据对于列 - 对象字段来说太长

Hibernate - Data truncation: Data too long for column - object field

我遇到了一个问题,我在网上找不到任何解决方案。 所以...我在 Spring 应用程序中使用 Hibernate。我正在将 Timetable 对象插入到我的数据库中。它有一个 Movie 对象作为其字段之一。错误说:

数据截断:第 1 行第 'movie' 列的数据太长 org.hibernate.exception.DataException:无法插入:[com.model.Timetable]

我不知道为什么我会收到错误,因为我没有定义电影对象的 "length" 之类的东西。是否有对象的最大尺寸或类似的东西?

我不会显示每个 class 的完整代码,因为它相当庞大,只有最重要的部分(classes 有构造函数、getter 和 setter):

@Entity
@Table(name = "timetable")
public class Timetable implements Serializable {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private int id;

    @ElementCollection
    @JoinColumn(name = "timetable_id")
    private List<Date> timetable;

    @Column(name = "movie")
    private Movie movie;

    @Column(name = "dubbing")
    private boolean dubbing;

}

@Entity
@Table(name = "movies")
public class Movie implements Serializable {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private int id;

    @Column(name = "filmwebID", unique = true, nullable = true, length = 10)
    private int filmwebID = 0;

    @Column(name = "imdbId", unique = true, nullable = true, length = 10)
    private String IMDBID = null;

    @Column(name = "title", unique = false, nullable = true, length = 100)
    private String title = null;

    @Column(name = "polishTitle", unique = false, nullable = true, length = 100)
    private String polishTitle = null;

    @Column(name = "year", unique = false, nullable = true)
    private Integer year = null;

    @Column(name = "coverUrl", unique = false, nullable = true, length = 120)
    private URL coverUrl = null;

    @Column(name = "filmwebUrl", unique = false, nullable = true, length = 120)
    private String filmwebUrl = null;

    @Column(name = "imdbUrl", unique = false, nullable = true, length = 120)
    private String IMDBUrl = null;

@Column(name = "englishDescription", unique = false, nullable = true, length = 500)
    private String englishDescription = null;

}

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="hibernate.connection.driver_class">
            com.mysql.jdbc.Driver
        </property>

        <!-- Assume test is the database name -->
        <property name="hibernate.connection.url">
            jdbc:mysql://localhost:3036/test
        </property>
        <property name="hibernate.connection.username">
            root
        </property>

        <property name="hbm2ddl.auto">create</property>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <mapping class="info.talacha.filmweb.models.Movie" />
        <mapping class="info.talacha.filmweb.models.Person" />
        <mapping class="com.model.Cinema" />
        <mapping class="com.model.Timetable" />
    </session-factory>
</hibernate-configuration>

您应该使用 @ManyToOne@OneToOne 注释映射 movie 字段。此字段是另一个 @Entity - 不是简单类型。

例如:

@ManyToOne
@JoinColumn(name = "movie")
private Movie movie;

这将创建 movie 列作为 外键 movies table.

一些有用的链接:

https://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/ https://howtoprogramwithjava.com/hibernate-manytoone-unidirectional-tutorial/ http://www.dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-many-to-one-using-annotations-1.html

如果没有定义下面的 映射注释JPA 假定 ClassDetails 对象将(连同其所有嵌套图)保存到数据库因此它创建了一个 LOB 类型

OneToOne
ManyToOne
OneToMany
ManyToMany

此外,如果未使用 @JoinColumn,再次 JPA 假设 ClassDetails 对象将(连同其所有嵌套图)保存到数据库,因此它创建了一个LOB类型

所以如下声明和@przemek 的评论

@ManyToOne
@JoinColumn(name = "movie")
private Movie movie;