我遇到以下异常:
java.lang.ClassCastException: class org.eclipse.persistence.sessions.DatabaseRecord cannot be cast to class org.eclipse.persistence.oxm.record.XMLRecord (org.eclipse.persistence.sessions.DatabaseRecord and org.eclipse.persistence.oxm.record.XMLRecord are in unnamed module of loader 'app')
Cause: class org.eclipse.persistence.sessions.DatabaseRecord cannot be cast to class org.eclipse.persistence.oxm.record.XMLRecord (org.eclipse.persistence.sessions.DatabaseRecord and org.eclipse.persistence.oxm.record.XMLRecord are in unnamed module of loader 'app')
以下是配置: 持久性.xml:
<?xml version="1.0"?>
<persistence xmlns="https://jakarta.ee/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0">
<persistence-unit name="eclipselink-persistence-unit" transaction-type="RESOURCE_LOCAL">
<description> This is a EclipseLink based Persistence Unit</description>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.target-database" value="org.eclipse.persistence.nosql.adapters.mongo.MongoPlatform"/>
<property name="eclipselink.nosql.connection-spec" value="org.eclipse.persistence.nosql.adapters.mongo.MongoConnectionSpec"/>
<property name="eclipselink.nosql.property.mongo.port" value="27017"/>
<property name="eclipselink.nosql.property.mongo.host" value="localhost"/>
<property name="eclipselink.nosql.property.mongo.db" value="my_db"/>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
</persistence>
JPAUtil 创建 EntityManagerFactory 实例:
public class JPAUtil {
public static final String persistenceUnitName = "eclipselink-persistence-unit";
private static EntityManagerFactory factory;
private static final Logger LOGGER = LoggerFactory.getLogger(JPAUtil.class);
public static EntityManagerFactory getEntityManagerFactory(){
if(factory != null && factory.isOpen()){
return factory;
}
LOGGER.info("Creating EntityManager Factory........");
factory = Persistence.createEntityManagerFactory(persistenceUnitName);
return factory;
}
public static void closeEntityManagerFactory(){
LOGGER.info("Closing EntityManager Factory........");
if(factory != null && factory.isOpen()){
factory.close();
}
}
}
以下是另一个类中的函数,用于从 MongoDB 中的集合中获取数据:
public List<Data> getAll() {
EntityManager entityManager = JPAUtil.getEntityManagerFactory().createEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
boolean operationCompleted = false;
List<Data> listOfData = null;
try{
transaction.begin();
List<Data> data = entityManager.createQuery("SELECT v FROM Data v")
.getResultList();
LOGGER.info("Completed the Search Operation");
operationCompleted = true;
listOfData = data;
}
catch( Exception e) {
if(e.getCause()!=null) {
LOGGER.error("Exception(Fetch All): {}\n Cause: {}", e.getMessage(), e.getCause().getMessage());
}
else{
if(e.getMessage() == null){
LOGGER.error("Exception(Fetch All): {}, No Message",String.valueOf(e));
}
else {
LOGGER.error("Exception(Fetch All): {}, With Message", e.getMessage());
}
}
}
finally {
if(operationCompleted){
if(transaction.isActive())transaction.commit();
}
else{
if(transaction.isActive())transaction.rollback();
}
entityManager.close();
}
return listOfData;
}
}
以下是数据实体(这里的Details是另一个实体):
@Entity
@Getter
@Setter
@NoSql(dataType = "DATA", dataFormat = DataFormatType.MAPPED)
public class Data {
@Id
@Field(name = "_id")
private String name;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinField(name = "details", referencedFieldName = "_id")
private List<Details> internalDetails;
}
当我尝试将数据保存在数据库中时,我可以没有任何问题。但我无法取回它。
我很笨。我没有仔细阅读文档。他们在文档中提到:
MongoDB – 支持 JPQL 和 Criteria 查询,但有一些限制:不支持联接、子选择、分组依据和某些数据库函数。
我使用了FetchType.EAGER,它正在进行复杂的查询。 我将其更改为 FetchType.LAZY 现在可以使用了。