Now that we know what we are aiming for, let's start thinking about the storage layer of our application. As mentioned earlier, we will use ObjectDB to store our objects. We already have a document class that will hold information about our uploaded documents. So, let's create the Data Access Object to retrieve, store, modify, and delete our documents.
...
Let's add the ObjectDB repository after our "dependencies" tag:
Code Block language xml theme Eclipse title pom.xml - <repository> <repositories> <repository> <id>objectdb</id> <name>ObjectDB Repository</name> <url>http://m2.objectdb.com</url> </repository> </repositories>
Then add the following dependencies:
In the last dependency, the version tag contains the following string '${org.springframework-version}'. This is a variable and helps keeping your pom.xml more readable. If you look through your dependencies, you will see that many of them have the same version. This makes sense, because we don't want to use different versions within the same framework. To make it easier to change to a different (e.g. newer) version, we can use a variable to hold the version information and use that variable in all the dependencies that should have the same version. The version number then only needs to be declaredCode Block language xml theme Eclipse title pom.xml - <dependencies> <!-- Object DB --> <dependency> <groupId>com.objectdb</groupId> <artifactId>objectdb</artifactId> <version>2.6.3</version> </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>javax.persistence</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>javax.transaction</groupId> <artifactId>jta</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${org.springframework-version}</version> </dependency>
<dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.10</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${org.springframework-version}</version> </dependency>
In the last dependency, the version tag contains the following string '${org.springframework-version}'. This is a variable and helps keeping your pom.xml more readable. If you look through your dependencies, you will see that many of them have the same version. This makes sense, because we don't want to use different versions within the same framework. To make it easier to change to a different (e.g. newer) version, we can use a variable to hold the version information and use that variable in all the dependencies that should have the same version. The version number then only needs to be declared once in the beginning of the pom.xml like that:
Code Block language xml theme Eclipse title pom.xml - <properties> <properties> <org.springframework-version>4.2.5.RELEASE</org.springframework-version> </properties>
Now, replace all occurrences in your pom.xml of '4.2.5.RELEASE' with '${org.springframework-version}' (of course only for the spring framework dependency, not for dependencies that incidentally have the same version number). Your final pom.xml should look like this:
Code Block language xml theme Eclipse title pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>edu.asu.diging.tutorial.spring</groupId> <artifactId>fileManager</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>fileManager Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <org.springframework-version>4.2.5.RELEASE</org.springframework-version> </properties> <dependencies> <!-- Spring dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- Object DB --> <dependency> <groupId>com.objectdb</groupId> <artifactId>objectdb</artifactId> <version>2.6.3</version> </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>javax.persistence</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>javax.transaction</groupId> <artifactId>jta</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${org.springframework-version}</version> </dependency> </dependencies> <repositories><dependency> <repository> <id>objectdb<<groupId>aopalliance</id>groupId> <name>ObjectDB Repository<<artifactId>aopalliance</name>artifactId> <url>http://m2.objectdb.com</url><version>1.0</version> </repository> </repositories> <build> <finalName>fileManager</finalName> </build> </project>
persistence.xml
...
dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.10</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${org.springframework-version}</version> </dependency> </dependencies> <repositories> <repository> <id>objectdb</id> <name>ObjectDB Repository</name> <url>http://m2.objectdb.com</url> </repository> </repositories> <build> <finalName>fileManager</finalName> </build> </project>
persistence.xml
ObjectDB supports using JPA (Java Persistence API). Using JPA decouples our data access object we will implement from the concrete implementation, which makes it easier later on to switch to a different storage solution. To use ObjectDB/JPA, we first need to add a file called persistence.xml in which we configure what database to use. Create a new folder in 'src/main/resources' called "META-INF". In there create a file called 'persistence.xml' and paste the following content:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="FileManagerPU">
<provider>com.objectdb.jpa.Provider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="/path/to/your/dbfiles/folder/fileManager.odb"/>
<property name="javax.persistence.jdbc.user" value="admin"/>
<property name="javax.persistence.jdbc.password" value="admin"/>
</properties>
</persistence-unit>
</persistence> |
In the property 'javax.persistence.jdbc.url' replace the path to a folder in your system that should hold the db files. The last part of the path (fileManager.odb) is the file name of the file that will hold your objects.
servlet-context.xml
Next, add the following declaration to your servlet-context.xml:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<!-- Add JPA support -->
<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<!-- Add Transaction support -->
<bean id="myTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf" />
</bean>
<!-- Use @Transaction annotations for managing transactions -->
<tx:annotation-driven transaction-manager="myTxManager" /> |
You will most likely will see an error message after you saved the files. This is because you also need to declare the 'tx' namespace. Adjust the first tag (beans) so that it looks like that:
Code Block | |||||||||
---|---|---|---|---|---|---|---|---|---|
| <?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
| ||||||||
<beans xmlns="http://javawww.sunspringframework.comorg/xmlschema/ns/persistencebeans" xmlns:xsicontext="http://www.w3springframework.org/2001schema/XMLSchema-instancecontext" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence xmlns:xsi="http://javawww.sunw3.comorg/xml/ns/persistence/persistence_2_0.xsd">2001/XMLSchema-instance" <persistence-unit name="FileManagerPU"> <provider>com.objectdb.jpa.Provider</provider> <properties> xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" <property namexsi:schemaLocation="javax.persistence.jdbc.url" value="/path/to/your/dbfiles/folder/fileManager.odb"/> <property name="javax.persistence.jdbc.user" value="admin"/> <property name="javax.persistence.jdbc.password" value="admin"/> </properties> </persistence-unit> </persistence> |
...
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> |
Persistent entities
To be able to store objects using JPA, we need to let the framework know what classes it should be able to store. We do that by annotating our classes with '@Entity'. Open the Document class and add the following annotations:
...
Now that the backend is set up, let's take a look at the front end again.