【问题标题】:EntityManager.find method leading to MySQLIntegrityConstraintViolationExceptionEntityManager.find 方法导致 MySQLIntegrityConstraintViolationException
【发布时间】:2010-12-17 23:55:16
【问题描述】:

当我执行EntityManager.find() 时会抛出以下异常:

引起:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: 字段列表中的“日期”列不明确

我要查找的类(配置文件)与另一个实体类(图库)具有双向的一对一关系。我很感激帮助理解why 引发此异常并how 解决它。下面是两个实体类:

public class Profile {
 ...
 private java.sql.Date date;
 private my.webapp.Gallery gallery;
 ...

 @OneToOne
 @JoinColumn(name="gallery_id")
 public my.webapp.Gallery getGallery() {
   return gallery
 }
 ...
}

public class Gallery {
 ...
 private my.webapp.Profile profile;
 ...

 @OneToOne(mappedBy="gallery")
 public my.webapp.Profile getProfile() {
   return profile
 }
 ...
} 

【问题讨论】:

  • 事实:date 是 MySQL 中的保留关键字。看起来 Hibernate/JPA 没有逃脱它(我想知道为什么不)。开启 SQL 日志,验证生成的 SQL 是否正确。

标签: java mysql hibernate jpa ambiguous


【解决方案1】:

不是因为“日期”是保留关键字吗?尝试将此添加到“日期”属性(或吸气剂)。它将引用属性名称。

@Column(name="`date`")

【讨论】:

  • 不,DATE 不是保留字(至少现在不是)。
猜你喜欢
  • 2015-05-13
  • 1970-01-01
  • 1970-01-01
  • 2018-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-26
  • 1970-01-01
相关资源
最近更新 更多