jpa 2.1和新的日期APi:YearMonth到date转换器,MySQL例外

时间:2015-05-18 14:50:25

标签: java mysql jpa jpql

我是堆栈,我需要在Java SE 8客户端应用程序中使用JPA 2.1获取表(MySQL)中的条目,但是我得到了这个例外:

[EL Warning]: 2015-05-18 13:57:15.882--UnitOfWork(974306870)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'YEAR_MONTH FROM app.INCOME WHERE (YEAR_MONTH = '2015-05-01 00:00:00')' at line 1
Error Code: 1064
Call: SELECT ID, AMOUNT, PERIOD, SOURCE, YEAR_MONTH FROM app.INCOME WHERE (YEAR_MONTH = ?)
bind => [1 parameter bound]
Query: ReadAllQuery(referenceClass=Income sql="SELECT ID, AMOUNT, PERIOD, SOURCE, YEAR_MONTH FROM app.INCOME WHERE (YEAR_MONTH = ?)")
Exception in thread "JavaFX Application Thread" javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse     Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'YEAR_MONTH FROM app.INCOME WHERE (YEAR_MONTH = '2015-05-01 00:00:00')' at line 1
Error Code: 1064
Call: SELECT ID, AMOUNT, PERIOD, SOURCE, YEAR_MONTH FROM app.INCOME WHERE (YEAR_MONTH = ?)
bind => [1 parameter bound]
Query: ReadAllQuery(referenceClass=Income sql="SELECT ID, AMOUNT, PERIOD, SOURCE, YEAR_MONTH FROM app.INCOME WHERE (YEAR_MONTH = ?)")
at org.eclipse.persistence.internal.jpa.QueryImpl.getDetailedException(QueryImpl.java:382)
at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:260)

这是代码

@Entity
@Table(schema="app")
public class Income implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    //partial: 2015-02      AUTO
    @Column(name="YEAR_MONTH")
    private YearMonth ymonth;

    private String source;

    private int amount;

使用此JPQL查询:

private static final String MONTH_INCOMES = "SELECT i FROM Income i WHERE i.ymonth = :month";
List<Income> sources= em.createQuery(MONTH_INCOMES, Income.class).setParameter("month", YearMonth.now()).getResultList();

我实现了这个转换器:YearMonth&lt; - &gt;日期

@Converter(autoApply = true)
public class YearMonthDateJPAConverter implements AttributeConverter<YearMonth, Date>{

@Override
public Date convertToDatabaseColumn(YearMonth entityValue) {
    return Date.from(entityValue.atDay(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
}

@Override
public YearMonth convertToEntityAttribute(Date dbValue) {
    return YearMonth.from(dbValue.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
}

DB是MySQL 5.6,表定义是:

CREATE TABLE `income` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `YEAR_MONTH` date NOT NULL,
  `SOURCE` varchar(100) NOT NULL,
  `AMOUNT` int(11) NOT NULL,
  `PERIOD` enum('once','daily','weekly','twoWeeks','twiceAmonth','fourweeks','monthly','twoMonths','ev3month','ev4month','twiceAyear','yearly','twoYear') NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

,表格为空。这有什么问题?????我甚至不理解异常

1 个答案:

答案 0 :(得分:0)

解决方案:更改两列的名称:SOURCE,YEAR_MONTH。因为它们是MySQL的保留字。没有Java问题,只有colums命名