Using Kundera with MongoDB: Configuring JPA in the persistence.xml Configuration File

In this section we shall create a META-INF/persistence.xml configuration file in the src/main/resources folder in the Maven project. We shall configure the object/relational mapping in the persistence.xml configuration file. Kundera supports some properties that are specified in persistence.xml using the <property/> tag, common to all NoSQL datastores it supports. These common properties are discussed in Table 9-1.

  1. In the persistence.xml for the Maven project specify persistence-unit name as kundera.
  2. Add a <provider/> element set to com.impetus.kundera.KunderaPersistence.
  3. Specify the JPA entity class to kundera.Catalog in the <class/> element.
  4. Add <property/> tags grouped as subelements of the <properties/> tag. Add the properties discussed in Table 9-2.

The persistence.xml configuration file is listed below.

<?xml version=”1.0″ encoding=”UTF-8″?>

<persistence version=”2.1″

xmlns=”” xmlns:xsi=” XMLSchema-instance”


<persistence-unit name=”kundera”>




<property name=”kundera.nodes” value=”″ />

<property name=”kundera.port” value=”27017″ />

<property name=”kundera.keyspace” value=”local” />

<property name=”kundera.dialect” value=”mongodb” />

<property name=”” value=”create” />

<property name=”kundera.client.lookup.class”

value=”com.impetus.client.mongodb.MongoDBClientFactory” />

<property name=”kundera.annotations.scan.package” value=”kundera” />




Some NoSQL database-specific properties may also be specified in a separate XML configuration file. The following (Table 9-3) MongoDB-specific properties are supported.

  1. For example, to configure MongoDB-specific properties add the following property for the MongoDB-specific configuration file in persistence.xml.

<property name=”” value=”kundera-mongo.xml” />

The name of the MongoDB-specific configuration file, kundera-mongo.xml. A sample MongoDB- specific configuration file is listed.

<?xml version=”1.0″ encoding=”UTF-8″?>







<property name=”read.preference” value=”secondary”></property>

<property name=”socket.timeout” value=”50000″></property>
















We have not used any MongoDB-specific configuration file.

14. Next, create the persistence.xml configuration file in the src/main/resources/ META-INF/ folder. The META-INF folder is not created when a new Maven project is created. To add the META-INF folder right-click on the resources folder and select New ➤ Folder as shown in Figure 9-9.

15. In New Folder wizard select the src/main/resources folder and specify Folder name as META-INF and click on Finish as shown in Figure 9-10.

Figure 9-10. Creating the META-INF Folder

The META-INF folder gets created. Next, add a persistence.xml file to the META-INF folder.

  1. To create the persistence.xml file select File ➤ New ➤ Other.
  2. In the New window, select XML ➤ XML File and click on Next as shown in Figure 9-11.

3. In the New XML File wizard select the META-INF folder and specify File name as persistence.xml as shown in Figure 9-12. Click on Finish.

Figure 9-12. Creating the persistence.xml

The persistence.xml file gets added to the META-INF folder.

  1. Copy the persistence.xml file listed earlier to the persistence.xml file in the Maven project as shown in Figure 9-13.

Source: Vohra Deepak (2015), Pro MongoDB™ Development, Apress; 1st ed. edition.

Leave a Reply

Your email address will not be published. Required fields are marked *