hibernate repository
In this section, you will learn how to use maven repository in your Hibernate project.
Given below a simple example to demonstrate you how to use Maven repository using pom.xml :
Example
In this example, we are going to generate simple java Hibernate project using Maven project builder.
For creating the project inside folder workspace , execute the below command in command prompt :
C:\workspace>mvn archetype:generate [INFO] Scanning for projects... [INFO] Searching repository for plugin with prefix: 'archetype'. [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Default Project [INFO] task-segment: [archetype:generate] (aggregator-style) [INFO] ------------------------------------------------------------------------ [INFO] Preparing archetype:generate [INFO] No goals needed for project - skipping [INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus .velocity.ContextClassLoaderResourceLoader'. [INFO] Setting property: velocimacro.messages.on => 'false'. [INFO] Setting property: resource.loader => 'classpath'. [INFO] Setting property: resource.manager.logwhenfound => 'false'. [INFO] [archetype:generate {execution: default-cli}] [INFO] Generating project in Interactive mode [INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven. archetypes:maven-archetype-quickstart:1.0) ........................................................................... Choose a number: (1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/2 4/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42/43/44) 15: : 15 .......................................................................... Define value for groupId: : net.roseindia Define value for artifactId: : HibernateTutorial Define value for version: 1.0-SNAPSHOT: : Define value for package: : net.roseindia Confirm properties configuration: groupId: net.roseindia artifactId: HibernateTutorial version: 1.0-SNAPSHOT package: net.roseindia Y: : y .......................................................................... [INFO] ------------------------------------------------------------------------- --- [INFO] Using following parameters for creating OldArchetype: maven-archetype-qui ckstart:RELEASE [INFO] ------------------------------------------------------------------------- --- [INFO] Parameter: groupId, Value: net.roseindia [INFO] Parameter: packageName, Value: net.roseindia [INFO] Parameter: package, Value: net.roseindia [INFO] Parameter: artifactId, Value: HibernateTutorial [INFO] Parameter: basedir, Value: C:\workspace [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] ********************* End of debug info from resources from generated POM *********************** [INFO] OldArchetype created in dir: C:\workspace\HibernateTutorial [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4 minutes 47 seconds [INFO] Finished at: Sat Apr 07 17:02:23 GMT+05:30 2012 [INFO] Final Memory: 9M/21M [INFO] ------------------------------------------------------------------------ |
In workspace folder, you will see the following structure, which is auto generated :
Create folder resources , put Hibernator's XML files inside resource folder as follows :
Adding dependencies for Hibernate and MySQL in pom.xml
Inside HibernateTutorial folder, you will get pom.xml file, add support for Hibernate and MySQL. Hibernate is required dom4j, commons-logging, commons-collections and cglib as dependency library, add it as follows :
<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.roseindia</groupId> <artifactId>HibernateTutorial</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>HibernateTutorial</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- MySQL database driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.9</version> </dependency> <!-- Hibernate framework --> <dependency> <groupId>hibernate</groupId> <artifactId>hibernate3</artifactId> <version>3.2.3.GA</version> </dependency> <!-- Hibernate library dependecy start --> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency> <!-- Hibernate library dependecy end --> </dependencies> </project>