PersistenceException:行大小太大

时间:2013-12-23 11:34:33

标签: java jpa openjpa

这是我收到的异常消息:

[java] Exception in thread "main" javax.ejb.EJBException: The bean encountered a non-application exception; nested exception is: 
     [java]     javax.ejb.EJBTransactionRolledbackException: The transaction has been marked rollback only because the bean encountered a non-application exception :org.apache.openjpa.persistence.PersistenceException : Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs {stmnt 23775157 CREATE TABLE comment (comment_id INTEGER NOT NULL AUTO_INCREMENT, comment_content VARCHAR(65535) NOT NULL, comment_date DATETIME NOT NULL, comment_title VARCHAR(65535) NOT NULL, user_id INTEGER NOT NULL, post_id INTEGER NOT NULL, PRIMARY KEY (comment_id), UNIQUE U_COMMENT_COMMENT_ID (comment_id)) ENGINE = innodb} [code=1118, state=42000]

这是Comment类的一部分:

@Entity
@Table(name = "comment")
public class Comment implements Serializable {

    private static final long serialVersionUID = 1L;

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

    @Column(name = "comment_title", length=65535, unique = false, nullable = false)
    private String title;

    @Column(name = "comment_date", unique = false, nullable = false)
    private Date date;

    @Column(name = "comment_content", length=65535, unique = false, nullable = false)
    private String content;

    @ManyToOne
    @JoinColumn (name = "user_id", referencedColumnName="user_id", nullable = false)
    private User user;

    @ManyToOne
    @JoinColumn (name = "post_id", referencedColumnName="post_id", nullable = false)
    private Post post;
        // ... getters and setters
}

我有这个代码使用上面的注释,我在数据库中生成表。说实话,可能是,我搞砸了将罐子导入项目(不确定)。我没有更改代码,这是肯定的,但我不知道如何解决这个问题。感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

错误告诉您行的大小太大--MySQL的最大行大小为65,535字节(请参阅http://dev.mysql.com/doc/refman/5.0/en/column-count-limit.html)。

您应该更改comment_titlecomment_text列的长度属性,以使总行数达到65,535字节以下。您当前使用的值看起来像是使用了一些默认值而没有真正考虑适当的限制。你真的想让评论标题那么长吗?!

JPA为定义表而创建的SQL将列设置为VARCHAR(65535),这可能会让您相信它应该没问题,因为两个列的长度都是可变的,但事实并非如此。总行大小计算假定表中的所有列都已完全使用 - 即您的VARCHAR(65535)列中存储了65,535个字节。我链接到的页面对其进行了解释,并提供了一个示例,其中包含一个表格页面的一半,其中两个VARCHAR列突破了限制。