Hibernate - 数据截断:列 - 对象字段的数据太长

时间:2015-01-03 14:58:23

标签: java spring hibernate

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

数据截断:第1行的列'movie'的数据太长 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>

2 个答案:

答案 0 :(得分:2)

您应该将movie字段与@ManyToOne@OneToOne注释对应。此字段是另一个@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

答案 1 :(得分:2)

如果未定义下面的映射注释JPA会假定将ClassDetails对象(及其所有嵌套图形)保存到DB,因此它会创建一个LOB类型

OneToOne
ManyToOne
OneToMany
ManyToMany

此外,如果未使用@JoinColumn,则JPA会假定将ClassDetails对象(及其所有嵌套图形)保存到DB,因此会创建LOB 1}}输入

所以声明如下,并由@przemek

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