如何通过 hibernate ogm 将 mongodb atlas 连接到 java EE 8 Framework with jpa?

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

所以我想将一个 mongodb atlas 集群连接到一个 jpa 项目

我可以使用此 repo 连接到 mongodb 本地: https://github.com/i0712326/java-ee-hibernate-mongodb/tree/main/java-ee-09-hibernate-ogm

但是我改变了它的 persistanse.xml 文件多少才能连接到 atlas 但它没有用...... 我几乎使用了此页面中的所有 属性 : https://github.com/WiPeK/JavaEE-Bank/blob/5446e1c0fc87b7d4ef28e89bd3c5542de62ecaa7/shared/src/main/resources/META-INF/mongo-persistence.xml

我认为问题出在主机 属性 所以 我用这个 属性:

  <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 "/>

然后出现这个错误:

           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
      

我用过这个 属性:

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

然后这个错误显示:

            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

您使用的依赖项很旧。我不确定他们是否支持 MongoDB Atlas,他们不支持在配置 属性 中设置多个主机。 这就是为什么你有 NumberFormatException.

最新的 Hibernate OGM 版本是 5.4。1.Final。除非你对使用的依赖有严格的要求,否则我会尝试使用那个:https://hibernate.org/ogm/releases/5.4/