【问题标题】:Hibernate - Data truncation: Data too long for column - object fieldHibernate - 数据截断:列的数据太长 - 对象字段
【发布时间】:2015-01-03 14:58:23
【问题描述】:

我遇到了一个问题,我在网上找不到任何解决方案。 所以...我在我的 Spring 应用程序中使用 Hibernate。我将 Timetable 对象插入到我的数据库中。它有一个 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>

【问题讨论】:

    标签: java spring hibernate


    【解决方案1】:

    您应该使用@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

    【讨论】:

      【解决方案2】:

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

      OneToOne
      ManyToOne
      OneToMany
      ManyToMany
      

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

      因此声明如下并由@przemek 提及

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-14
        • 2015-10-15
        • 1970-01-01
        • 2017-01-03
        • 1970-01-01
        • 2018-07-06
        • 1970-01-01
        相关资源
        最近更新 更多