Hello
This is the part 2 of the series about working with maven, spring, and hibernate together. In this post we will be using Linux Mint Nadia, a cousin of Ubuntu 12.10 and Netbeans.
Creating the project
- Click: File > New Project
- Categories: Maven > Project: Web Application
- Click Next
- Project Name: springDataMVCFamily
- GroupId: net.djomeda.tutorials
- Click: Next
- Make sure you have the setting like shown bellow
- Click: File> New File
- Categories: Web> File Types: Standard Deployment Descriptor (web.xml)
- Click: Next
- Go to $TUTORIAL_HOME/src/main
- create folder “resources” such as shown bellow
- Do the same for $TUTORIAL_HOME/src/test/resources
- Right Click springDataMVCFamily > New > Java Package
- net.djomeda.tutorials.springdatamvcfamily.controller
- net.djomeda.tutorials.springdatamvcfamily.form
- net.djomeda.tutorials.springdatamvcfamily.model
- net.djomeda.tutorials.springdatamvcfamily.repository
- net.djomeda.tutorials.springdatamvcfamily.service
- Right Click: Other Sources > /src/main/resources > New > Other
- Categories: Other > File Types: properties file
- Create family.properties
- Locate project files in the projects tab
- Collapse the plus signe and click on pom.xml as shown bellow
- Edit pom as shown bellow
- Let’s build our project: click on clean and build icon or press shift+ F11
- Let’s run our project: click on run icon or press F6
After filling the 2 parts above the IDE will fill the rest of the fields
From my experiences it’s always good to have a web.xml file after creating a java web application
Set the location to your WEB-INF folder which is assumed to be automatically generated by netbeans
We will note $TUTORIAL_HOME as where you created the project example: /media/Repo/netbeansworkspace/springDataMVCFamily for me.
Once done Netbeans will automatically reload the folder structure and inside the IDE it will show like “Other Sources” in there, we will create our properties files to hold our parameters etc.
It will be loaded in netbeans as “Other Test Sources”
Let’s create the various java packages needed for this project
Now let’s create prepare our project for all needed dependencies
[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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.djomeda.tutorials</groupId>
<artifactId>springDataMVCFamily</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>springDataMVCFamily</name>
<properties>
<hibernate.version>4.2.1.Final</hibernate.version>
<mysql.connector.version>5.1.8</mysql.connector.version>
<slf4j.version>1.6.6</slf4j.version>
<logback.version>1.0.7</logback.version>
<spring.version>3.2.2.RELEASE</spring.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!– Spring MVC –>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.0.2.GA</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>${logback.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
[/xml]
In order to build with maven, make sure the computer has access to internet
If the build is successful and am sure it will , 😉 we can move to running the project
Voila, now that we are sure that our project is completely set and can run without an issue, let’s move on to the next part with is about creating models with JPA.