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=”http://xmlns.jcp.org/xml/ns/persistence” xmlns:xsi=”http://www.w3.org/2001/ XMLSchema-instance”

xsi:schemaLocation=”http://xmlns.jcp.org/xml/ns/persistence 

http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd”>

<persistence-unit name=”kundera”>

<provider>com.impetus.kundera.KunderaPersistence</provider>

<class>kundera.Catalog</class>

<properties>

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

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

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

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

<property name=”kundera.ddl.auto.prepare” value=”create” />

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

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

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

</properties>

</persistence-unit>

</persistence>

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=”kundera.client.property” 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″?>

<clientProperties>

<datastores>

<dataStore>

<name>mongo</name>

<connection>

<properties>

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

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

</properties>

<servers>

<server>

<host>192.160.140.160</host>

<port>27017</port>

</server>

<server>

<host>192.161.141.161</host>

<port>27018</port>

</server>

</servers>

</connection>

</dataStore>

</datastores>

</clientProperties>

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 *