Maven 2 + Hibernate 3.2 + Mysql Example (XML Mapping) : 1. Table Creation
Maven 2 + Hibernate 3.2 + Mysql Example (XML Mapping) : 1. Table Creation
2 + MySQL
Example (XML Mapping)
This quick guide show you how to use Maven to generate a simple Java project,
and uses Hibernate to insert a record into MySQL database.
Tools & technologies used in this article :
1. Maven 2.2.1
2. JDK 1.6.0_13
3. Hibernate 3.2.3.GA
4. MySQL 5.0
1. Table Creation
MySQL script to create a stock table.
DROP TABLE IF EXISTS `stock`;
CREATE TABLE `stock` (
`STOCK_ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`STOCK_CODE` VARCHAR(10) NOT NULL,
`STOCK_NAME` VARCHAR(20) NOT NULL,
PRIMARY KEY (`STOCK_ID`) USING BTREE,
UNIQUE KEY `UNI_STOCK_NAME` (`STOCK_NAME`),
UNIQUE KEY `UNI_STOCK_ID` (`STOCK_CODE`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Maven will generate all the Javas standard folders structure for you (beside resources
folder, quick start archetype #15 does not contains the resources folder)
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</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 -->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</project>
Issue the mvn eclipse:eclipse, Maven will download all Hibernate and MySQL libraries
automatically and put into Mavens local repository. At the same time, Maven will add
the downloaded libraries into Eclipse .classpath for dependency purpose. Like it again
:) , no need to find the library and copy it myself.
<hibernate-mapping>
<class name="com.mkyong.common.Stock" table="stock" catalog="mkyong">
<id name="stockId" type="java.lang.Integer">
<column name="STOCK_ID" />
<generator class="identity" />
</id>
<property name="stockCode" type="string">
/**
* Model class for Stock
*/
public class Stock implements java.io.Serializable {
public Stock() {
}
Note
Create the model class and mapping files are quite tedious in large application, With Hibernate
tools, this can be generate automatically, check this article Hibernate tools to generate it
automatically.
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property
name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">password</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/mkyong</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="com/mkyong/common/Stock.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
Set the show_sql property to true will output the Hibernate SQL statement. Hibernate
Dialect is telling your Hibernate application which SQL it has to generate to talk to your
database. Please refer this article for other database dialect Hibernate dialect
collection.
package com.mkyong.persistence;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Session;
import com.mkyong.persistence.HibernateUtil;
session.beginTransaction();
Stock stock = new Stock();
stock.setStockCode("4715");
stock.setStockName("GENM");
session.save(stock);
session.getTransaction().commit();
}
}