Java JPA2.1和新的日期APi:YearMonth-to-Date转换器,MySQL异常

Java JPA2.1和新的日期APi:YearMonth-to-Date转换器,MySQL异常,java,mysql,jpa,jpql,Java,Mysql,Jpa,Jpql,我是stack,我需要在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.Dat

我是stack,我需要在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();

桌子是空的。这有什么问题吗?????我甚至不理解异常的解决方案:更改两列的名称:SOURCE,YEAR\u MONTH。因为它们是MySQL的保留字。没有Java问题,只是colums命名

我可能错了(我现在不怎么使用mySQL),但它看起来好像被解释为一个字符串?JPA中对java.time的支持是什么样的?我不知道我是否正确回答了这个问题,但是,没有支持,这就是为什么需要实现一个转换器来持久化java.time类!这可能是一个基本问题,但您正在扫描包含@Converter的包以获取注释?JPA可以做到这一点,在客户端应用程序(SE)中,您必须像persistence.xml中的任何实体一样列出转换器,这就是为什么JPA会自动转换YearMonth-to-Date类型。只是想让你知道,我甚至将YearMonth转换为字符串,但仍然是相同的例外!!告诉我,如果在dbms中手动执行生成的查询,会发生什么情况?从app.INCOME中选择ID、金额、期间、来源、年/月,其中(年/月='2015-05-01 00:00:00')
@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());
}
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;