java - hibernate -数据截断: Data too long for column - object field

标签 java spring hibernate

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

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

我不知道为什么会收到错误,因为我没有定义诸如电影对象的“长度”之类的内容。对象是否有最大尺寸或类似的东西?

我不会展示每个类的完整代码,因为它相当庞大,只展示最重要的部分(类有构造函数、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 表的外键

一些有用的链接:

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

关于java - hibernate -数据截断: Data too long for column - object field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27756137/

相关文章:

java - 无法启动 RESTful Web 服务 IntelliJ

java - 如何修复 .flex 文件的语法问题?

Java - 循环遍历 JSONArray

java - 设备上没有剩余空间 - Android 模拟器

java - 从 Java 中的给定树中检索所有节点

spring - YARN 客户端身份验证失败,且未启用 SIMPLE 身份验证。可用 :[TOKEN]

java - "[http-bio-8080-exec-494] [ERROR]"中 exec 之后的数字是什么意思?

java - 使用 AbstractTransactionalJUnit4SpringContextTests 中途提交

java - Spring Rest 存储库 -> 如何动态省略响应中的字段

java - 集成 Pentaho/Talend/等使用 OR 映射器