Search code examples
hibernatejpamongodb-atlasjava-ee-8hibernate-ogm

how connect mogodb atlas to java EE 8 Framework with jpa through hibernate ogm?


so i want to connect a mongodb atlas cluster to a jpa project

i could connect to mongodb local with this repo : https://github.com/i0712326/java-ee-hibernate-mongodb/tree/main/java-ee-09-hibernate-ogm

but how much i changed its persistanse.xml file to able to connect to atlas it didnt work... i used almost all of property in this page : https://github.com/WiPeK/JavaEE-Bank/blob/5446e1c0fc87b7d4ef28e89bd3c5542de62ecaa7/shared/src/main/resources/META-INF/mongo-persistence.xml

i think problem is from host property so i used this property:

  <property name="hibernate.ogm.datastore.host" value="cluster0-shard-00-00.dd0ui.mongodb.net:27017,cluster0-shard-00-01.dd0ui.mongodb.net:27017,cluster0-shard-00-02.dd0ui.mongodb.net:27017 "/>

then this error comes up :

           Caused by: org.hibernate.HibernateException: OGM001203: Unable to find or initialize a connection to the MongoDB server
at deployment.java-ee-09-hibernate-ogm.war//org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider.createMongoClient(MongoDBDatastoreProvider.java:133)
at deployment.java-ee-09-hibernate-ogm.war//org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider.start(MongoDBDatastoreProvider.java:108)
... 43 more
          Caused by: java.lang.NumberFormatException: For input string: "27017,cluster0-shard-00-01.dd0ui.mongodb.net:27017,cluster0-shard-00-02.dd0ui.mongodb.net:27017"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at deployment.java-ee-09-hibernate-ogm.war//com.mongodb.ServerAddress.<init>(ServerAddress.java:85)
at deployment.java-ee-09-hibernate-ogm.war//org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider.createMongoClient(MongoDBDatastoreProvider.java:124)
... 44 more
      

i used this property:

       <property name="hibernate.ogm.datastore.host" value="cluster0-shard-00-00.dd0ui.mongodb.net"/> 

then this error show:

            Caused by: org.hibernate.HibernateException: OGM001214: Unable to connect to MongoDB instance cluster0-shard-00-00.dd0ui.mongodb.net:27017
at deployment.java-ee-09-hibernate-ogm.war//org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider.extractDatabase(MongoDBDatastoreProvider.java:182)
at deployment.java-ee-09-hibernate-ogm.war//org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider.start(MongoDBDatastoreProvider.java:110)
... 43 more
                 Caused by: com.mongodb.MongoTimeoutException: Timed out after 5000 ms while waiting to connect. Client view of cluster state is {type=Unknown, servers=[{address=cluster0-shard-00-00.dd0ui.mongodb.net:27017, type=Unknown, state=Connecting}]
at deployment.java-ee-09-hibernate-ogm.war//com.mongodb.BaseCluster.getDescription(BaseCluster.java:128)
at deployment.java-ee-09-hibernate-ogm.war//com.mongodb.DBTCPConnector.getClusterDescription(DBTCPConnector.java:396)
at deployment.java-ee-09-hibernate-ogm.war//com.mongodb.DBTCPConnector.getMaxBsonObjectSize(DBTCPConnector.java:641)
at deployment.java-ee-09-hibernate-ogm.war//com.mongodb.Mongo.getMaxBsonObjectSize(Mongo.java:641)
at deployment.java-ee-09-hibernate-ogm.war//com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:81)
at deployment.java-ee-09-hibernate-ogm.war//com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:66)
at deployment.java-ee-09-hibernate-ogm.war//com.mongodb.DB.getCollectionNames(DB.java:512)
at deployment.java-ee-09-hibernate-ogm.war//com.mongodb.DB.collectionExists(DB.java:555)
at deployment.java-ee-09-hibernate-ogm.war//org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider.extractDatabase(MongoDBDatastoreProvider.java:173)
... 44 more

hibernate-core-4.3.8.final

hibernate-ogm-mongodb-4.1.2.final

mongo-java-driver-2.12.4


Solution

  • The dependencies you are using are pretty old. I'm not sure they support MongoDB Atlas, they don't support setting multiple hosts in the configuration property. That's why you are having the NumberFormatException.

    The latest Hibernate OGM version available is 5.4.1.Final. Unless you have strict requirements about the dependency to use, I would try to use that one: https://hibernate.org/ogm/releases/5.4/