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

时间:2015-09-15 23:23:05

标签: java jpa eclipselink

我是JPA的新手,我正在尝试与 EclipseLink 2.6.1-RC1 sqlite 3.8.10.1 结合使用。

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

我在使用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标准,什么不是!

2 个答案:

答案 0 :(得分:1)

从您的问题中不清楚您如何使用@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;
}
}

答案 1 :(得分:0)

请勿在{{1​​}}上使用@Transient。

在方法(getters)上设置JPA注释具有与字段相同的意义,它只是不同的方言&#34;。 Eclipselink(在我看来)需要以一种方式结果,Hibernate更宽容(可以混合字段/方法注释)

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