gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:56:34 25 4
gpt4 key购买 nike

我对 JPA 相当陌生,我正在尝试与 EclipseLink 2.6.1-RC1sqlite 3.8.10.1 结合使用.

我的实际挑战是让持久性提供者使用 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 方法的打印调用是我在测试类中的调用 - 所以没有对 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注释;但您需要在两个地方指定:一次在类上指定实体属性的默认值,一次在不使用默认访问权限的任何属性上。所以:你的例子应该是这样的:

@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;
}
}

关于java - 无法使 EclipseLink 使用方法而不是字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32597250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com