不能使 EclipseLink 使用方法而不是字段

Cannot bring EclipseLink to use methods instead of fields

我是 JPA 的新手,我正在尝试结合 EclipseLink 2.6.1-RC1sqlite 3.8.10.1 .

我的实际挑战是让 Persistence Provider 使用 get-/set-methods 而不是直接字段访问。

我在使用 EclipseLink 时遇到了很多麻烦,目前我正在将我的简单测试项目与 Hibernate 进行比较,据我所知,Hibernate 没有问题。

Do I use JPA in an incorrect way or are my errors EclipseLink bugs?

这是我的例子:

persistence.xml:

<persistence-unit name="inmemorydb" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="eclipselink.target-database" value="Database" />
        <property name="javax.persistence.jdbc.url" value="jdbc:sqlite::memory:"/>
        <property name="javax.persistence.jdbc.driver" value="org.sqlite.JDBC"/>
    </properties>
</persistence-unit>

SimpleEntity.java:

@Entity
public class SimpleEntity implements Serializable {
private static final long serialVersionUID = 1L;

@Id
private long id;

@Transient
private String otherName;

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

@Basic
@Column(name="name")
public String getOtherName() {
    System.out.println("getOtherName: " + otherName);
    return otherName;
}

public void setOtherName(String otherName) {
    System.out.println("setOtherName: " + otherName);
    this.otherName = otherName;
}
}

JpaTest.java:

public class JpaTest {

public static final String PERSISTENCE_UNIT = "inmemorydb";

public static final String SQL_CREATE_TABLE = "CREATE TABLE simpleentity (id INTEGER PRIMARY KEY, name TEXT);";

private static EntityManagerFactory dbConnectionPool;

@BeforeClass
public static void initialiseDatabase() {
    System.out.println("initialiseDatabase");
    dbConnectionPool = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
    EntityManager dbConnection = dbConnectionPool.createEntityManager();
    dbConnection.getTransaction().begin();

    Query createTableQuery = dbConnection.createNativeQuery(SQL_CREATE_TABLE);
    createTableQuery.executeUpdate();

    dbConnection.getTransaction().commit();
    dbConnection.close();
}

@Test
public void createTable_InsertEntity() {
    System.out.println("createTable_InsertEntity");
    EntityManager dbConnection = dbConnectionPool.createEntityManager();
    dbConnection.getTransaction().begin();

    SimpleEntity testEntity = new SimpleEntity();
    testEntity.setId(1);
    testEntity.setOtherName("my name");
    dbConnection.persist(testEntity);

    dbConnection.getTransaction().commit();
    dbConnection.close();
}

@Test
public void createTable_InsertEntity_ReadEntity() {
    System.out.println("createTable_InsertEntity_ReadEntity");
    EntityManager dbConnection = dbConnectionPool.createEntityManager();
    dbConnection.getTransaction().begin();

    SimpleEntity testEntity = new SimpleEntity();
    testEntity.setId(2);
    testEntity.setOtherName("my other name");
    dbConnection.persist(testEntity);

    dbConnection.getTransaction().commit();
    dbConnection.getTransaction().begin();

    TypedQuery<SimpleEntity> findEntityQuery = dbConnection.createQuery("SELECT e FROM SimpleEntity e WHERE e.id = 2", SimpleEntity.class);
    SimpleEntity foundEntity = findEntityQuery.getSingleResult();
    dbConnection.close();

    assertEquals(2, foundEntity.getId());
    assertEquals("my other name", foundEntity.getOtherName());
}
}

如您所想,我希望看到 EclipseLink 调用我的 getOtherName()setOtherName(String)(对此我不确定)。但是简化后的结果是:

initialiseDatabase
[EL Info]: server: 2015-09-16 01:07:07.877--Detected server platform: org.eclipse.persistence.platform.server.NoServerPlatform.
[EL Info]: server: 2015-09-16 01:07:07.969--ServerSession(1845066581)--Detected server platform: org.eclipse.persistence.platform.server.NoServerPlatform.
 [EL Info]: server: 2015-09-16 01:07:08.184--Detected server platform: org.eclipse.persistence.platform.server.NoServerPlatform.
[EL Info]: server: 2015-09-16 01:07:08.184--ServerSession(1845066581)--Detected server platform: org.eclipse.persistence.platform.server.NoServerPlatform.
[EL Info]: 2015-09-16 01:07:08.187--ServerSession(1845066581)--EclipseLink, version: Eclipse Persistence Services - 2.6.1.v20150605-31e8258
[EL Info]: connection: 2015-09-16 01:07:09.017--ServerSession(1845066581)--/file:/C:/eclipse_ee/workspaces/jpaproperty/target/classes/_inmemorydb login successful
createTable_InsertEntity_ReadEntity
setOtherName: my other name
getOtherName: my other name
createTable_InsertEntity
setOtherName: my name

set 和 get 方法的打印调用是我在测试中的调用 class - 所以没有单独调用 EclipseLink?我真的尝试了很多 @Transient@Basic@Column 的不同设置,但没有成功。

当我尝试使用所谓的 @Access(AccessType.PROPERTY) 标准时,我什至得到一个非常奇怪的错误:

Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: sun.misc.Launcher$AppClassLoader@29453f44
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [inmemorydb] failed.
Internal Exception: java.lang.ClassCastException: org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataField    cannot be cast to org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataMethod

Isn't this JPA standard and isn't EclipseLink conforming to this?

顺便说一句,当我使用 Hibernate 时,输出如下:

createTable_InsertEntity
setOtherName: my name
getOtherName: my name
getOtherName: my name
getOtherName: my name
createTable_InsertEntity_ReadEntity
setOtherName: my other name
getOtherName: my other name
getOtherName: my other name
getOtherName: my other name
getOtherName: my other name
getOtherName: my other name
getOtherName: my other name

Can you help me - please try not to convince me about Hibernate - my first target is to understand what is JPA standard and what is not!

从你的问题中不清楚你是如何使用 @Access 注释的;但您需要在两个地方指定:一次是在 class 上指定实体属性的默认值,一次是在不使用默认访问权限的任何属性上。所以:你的例子应该是这样的:

@Entity
@Access(AccessType.FIELD)
public class SimpleEntity implements Serializable {
private static final long serialVersionUID = 1L;

@Id
private long id;

@Transient
private String otherName;

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

@Basic
@Column(name="name")
@Access(AccessType.PROPERTY)
public String getOtherName() {
    System.out.println("getOtherName: " + otherName);
    return otherName;
}

public void setOtherName(String otherName) {
    System.out.println("setOtherName: " + otherName);
    this.otherName = otherName;
}
}

不要在 otherName 上使用 @Transient。

在方法 (getter) 上设置 JPA 注释与在字段上设置意义相同,唯一不同的是 "dialect"。 Eclipselink(在我看来)需要以一种方式得到结果,Hibernate 更宽容(可以混合字段/方法注释)

在我个人的工作中,从不对成员使用 @Access(AccessType.PROPERTY) - 如果满足上述规则,自动配置就可以了。