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



我对JPA相当陌生,我正在尝试与 EclipseLink 2.6.1-RC1 sqlite 3.8.10.1 相结合。

我的实际挑战是让持久性提供程序使用get-/set-方法,而不是直接访问字段。

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

我是否以不正确的方式使用JPA,或者我的错误是EclipseLink错误?

下面是我的例子:

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方法的打印调用是我在测试类中的调用-所以没有单独调用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

这不是JPA标准,不是EclipseLink符合这个吗?

顺便说一下,当我使用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

你能帮我吗-请尽量不要说服我关于Hibernate -我的第一个目标是了解什么是JPA标准,什么不是!

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

@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注释与在字段上设置注释具有相同的意义,其唯一不同的"方言"。Eclipselink(在我看来)需要在一个方面是后续的,Hibernate更具容忍度(可以混合字段/方法注释)

在我的个人工作中从不使用@Access(AccessType.PROPERTY)超过成员-如果满足上述规则,自动配置是OK的

相关内容

最新更新