【问题标题】:com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'DATE' at row 1com.mysql.jdbc.MysqlDataTruncation:数据截断:第 1 行的“日期”列的数据太长
【发布时间】:2015-11-28 07:39:34
【问题描述】:

我的实体类:

@Entity
@Table(catalog = "emp", name = "person")
@XmlAccessorType(XmlAccessType.FIELD)
public class Person implements Serializable {
private static final long serialVersionUID = 1L;

/**
 */

@Column(name = "ID", nullable = false,length = 50)
@Basic(fetch = FetchType.EAGER)
@Id
@XmlElement
String id;
/**
 */

@Column(name = "ADDRESS", length = 50)
@Basic(fetch = FetchType.EAGER)
@XmlElement
String address;
/**
 */

@Column(name = "EMAIL", length = 50)
@Basic(fetch = FetchType.EAGER)
@XmlElement
String email;



@Column(name = "DATE")
@Basic(fetch = FetchType.EAGER)
@XmlElement
DateTime date;
/**
 */

/*getters and setters */

}

我在约会时使用 joda 时间。当我坚持时,它会抛出这个异常:

原因:com.mysql.jdbc.MysqlDataTruncation:数据截断:第 1 行的“DATE”列的数据太长

我的日期字段包含此值:2015-09-02T16:24:05.226+04:00

下面是它在db中持久化的代码:

    public Person save(Person person) {

    Person currentPerson = entityManager.find(Person.class,
            person.getId());
    if (currentPerson != null) {
        entityManager.detach(currentPerson);
        person =entityManager.merge(person);
    } else {
        entityManager.persist(person);
    }
    return person;
}

我正在使用 hibernate/jpa 和 mysql。我的数据库是基于我的实体类创建的。我的日期在我的实体类中是 datetime 类型,但在数据库中是 tinyblob 类型。

【问题讨论】:

  • 您是否应用了Joda-Time-Hibernate 映射?
  • 什么意思?无论我在上面发布什么,这就是我所做的一切
  • Hibernate 不理解 Joda,因此它可能只是退回到将数据作为字符串发送,而 MySQL 不理解/支持字符串值。
  • 那么我应该怎么做这个映射呢?
  • @Andreas 哪个链接?!

标签: java mysql spring hibernate jpa


【解决方案1】:

您似乎不能在 Datetime 中使用毫秒: mysql

【讨论】:

  • 那你有什么建议呢?我需要存储日期和时间(小时 n 分钟)我对毫秒不感兴趣
  • 你为什么在课堂上使用 datetime 而在数据库中使用 tinyblob。您是否尝试用日期时间替换 tinyblob?
  • 不是我把 tinyblob 放到 db 里的。我的数据库是由休眠基于我的实体类创建的。所以是hibernate把datetime解释为db中的tinyblob
【解决方案2】:

在创建新表时,在 SQL 代码中使用 Datetime 而不是 Date

【讨论】:

    猜你喜欢
    • 2017-01-30
    • 2014-09-28
    • 2012-11-14
    • 1970-01-01
    • 2014-02-26
    • 2011-10-02
    • 2022-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多