使用 EclipseLink 和 UCanAccess 的持久性错误

Error with persistence using EclipseLink and UCanAccess

出于锻炼原因,我正在尝试开发一个应用程序。我使用 MSAccess 2010 作为数据库,UCanAccess (3.06) 作为驱动程序,EclipseLink 2.1 作为 entity framework。

我无法向数据库添加新记录。这里的错误代码:

Internal Exception: net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.6 user lacks privilege or object not found: IDENTITY_VAL_LOCAL
Error Code: -5501
Call: SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1
Query: ValueReadQuery(name="SEQ_GEN_IDENTITY" sql="SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1")

在我看来,id 的自动生成失败了。实体 class 是通过 Netbeans 生成的,如下所示:

@Transient
private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "ID")
private Integer id;

默认情况下,EclipseLink 会尝试自动检测底层数据库并使用适当的 SQL 方言生成 SQL 语句。这显然对您不起作用,因为 UCanAccess 无法识别 SQL 检索最后创建的标识值的语句。

您可以尝试将 target-database 指令添加到指定 SQLServer 的 EclipseLink 配置中,以尝试获得有效的 SQL 语句 (SELECT @@IDENTITY) 以检索最后一个创建的 ID 值。但是,请记住 T-SQL 和 Access SQL 之间存在显着差异,因此您可能会继续遇到 EclipseLink 和 UCanAccess 之间的其他兼容性问题。

在知道上面的答案之前,我也面临着在访问数据库中插入新记录的同样问题, 感谢 先生。 Gord Thompson 为我提供了一个很好的解决方案, 而且它也在工作。

我刚刚在我的 persistence.xml 文件中添加了一行..

属性 名称="eclipselink.target-database" 值="HSQL"

 <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
      <persistence-unit name="OnePU" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>design_frames.One</class>
        <properties>
          <property name="javax.persistence.jdbc.url" value="jdbc:ucanaccess://C:\One\One.accdb"/>
          <property name="javax.persistence.jdbc.user" value=""/>
          <property name="javax.persistence.jdbc.driver" value="net.ucanaccess.jdbc.UcanaccessDriver"/>
          <property name="javax.persistence.jdbc.password" value=""/>
          <property name="eclipselink.target-database" value="HSQL"/>
        </properties>
      </persistence-unit>
    </persistence>