I am getting following exception:
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')
Here are the configurations:
persistence.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 to create instance of 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();
}
}
}
Following is the function in another class, for fetching the data from a collection in 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;
}
}
Following is the Data Entity(here Details is another Entity):
@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;
}
When I try to save data in db, I am able to without any issues. But I am unable to fetch it back.
2
Answers
I am dumb. I didn't read the documentation carefully. They have mentioned in the documentation that:
I have used FetchType.EAGER, which is making a complex query. I changed it to FetchType.LAZY and it is working now.
how to set username and password ? did you figure it out ?