注意
本文已过时,并且某些信息在最新的Hibernate开发中不再有效。 您应该参考最新的《 Maven 3 + Hibernate 3.6.3 + Oracle 11g示例(注释)》教程。
本教程将修改以前的Maven 2 + Hibernate 3.2 + MySQL示例(XML映射) ,并用注释代码替换Hibernate XML映射文件。
本文使用的工具和技术:
- Maven的2.2.1
- JDK 1.6.0_13
- 休眠3.2.3.GA
- MySQL 5.0
1.创建项目基础架构
在Maven + Hibernate(XML映射文件)+ MySQL示例中创建项目基础结构
2.添加JBoss存储库
要下载Hibernate注释库,需要pom.xml
JBoss存储库。
<repositories>
<repository>
<id>JBoss repository</id>
<url>http://repository.jboss.com/maven2/</url>
</repository>
</repositories>
3.添加Hibernate批注依赖项
在pom.xml
添加hibernate-annotations和hibernate-commons-annotations依赖项。
<dependency>
<groupId>hibernate-annotations</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.0.GA</version>
</dependency>
<dependency>
<groupId>hibernate-commons-annotations</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.0.0.GA</version>
</dependency>
档案: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>com.mkyong.common</groupId>
<artifactId>HibernateExample</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>HibernateExample</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>JBoss repository</id>
<url>http://repository.jboss.com/maven2/</url>
</repository>
</repositories>
<dependencies>
<!-- MySQL database driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<!-- Hibernate core -->
<dependency>
<groupId>hibernate</groupId>
<artifactId>hibernate3</artifactId>
<version>3.2.3.GA</version>
</dependency>
<!-- Hibernate annotation -->
<dependency>
<groupId>hibernate-annotations</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.0.GA</version>
</dependency>
<dependency>
<groupId>hibernate-commons-annotations</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.0.0.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 -->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</project>
4.更新项目类路径
在命令提示符下发出“ mvn eclipse:eclipse
”,以下载依赖项库并更新Eclipse的项目类路径。
5.更新HibernateUtil.java
更新“ HibernateUtil
”以使用“ AnnotationConfiguration ”而不是“ Configuration ”来构建Hibernate会话工厂。
以前正在使用“配置” –用于Hibernate XML映射文件
return new Configuration().configure().buildSessionFactory();
将其更改为“ AnnotationConfiguration” –支持休眠注释
return new AnnotationConfiguration().configure().buildSessionFactory();
文件:HibernateUtil.java
package com.mkyong.persistence;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new AnnotationConfiguration().configure().buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
6.更新模型类
更新“ Stock.java”以使用注释,如下所示:
股票.java
package com.mkyong.common;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity
@Table(name = "stock", catalog = "mkyong", uniqueConstraints = {
@UniqueConstraint(columnNames = "STOCK_NAME"),
@UniqueConstraint(columnNames = "STOCK_CODE") })
public class Stock implements java.io.Serializable {
private Integer stockId;
private String stockCode;
private String stockName;
public Stock() {
}
public Stock(String stockCode, String stockName) {
this.stockCode = stockCode;
this.stockName = stockName;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "STOCK_ID", unique = true, nullable = false)
public Integer getStockId() {
return this.stockId;
}
public void setStockId(Integer stockId) {
this.stockId = stockId;
}
@Column(name = "STOCK_CODE", unique = true, nullable = false, length = 10)
public String getStockCode() {
return this.stockCode;
}
public void setStockCode(String stockCode) {
this.stockCode = stockCode;
}
@Column(name = "STOCK_NAME", unique = true, nullable = false, length = 20)
public String getStockName() {
return this.stockName;
}
public void setStockName(String stockName) {
this.stockName = stockName;
}
}
7.删除现有的Hibernate XML映射文件
删除现有的Hibernate XML映射文件–“ Stock.hbm.xml”,这不再是必需的。
8.更新Hibernate配置文件
更新Hibernate配置文件– hibernate.cfg.xml。
以前,它具有带有“映射资源”标签的Hibernate XML映射文件。
<mapping resource="com/mkyong/common/Stock.hbm.xml"></mapping>
使用“映射类”标签将其更改为模型类
<mapping class="com.mkyong.common.Stock"></mapping>
档案:hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"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 class="com.mkyong.common.Stock"></mapping>
</session-factory>
</hibernate-configuration>
9.审查项目结构
听起来好像修改了几个文件,请仔细检查并确保文件夹结构如下:
10.运行并查看输出
运行您的App.java,它将在“股票”表中插入一条新记录。 结果应与先前的Hibernate XML映射文件示例相同。
Maven + Hibernate + MySQL
...
Hibernate: insert into mkyong.stock (STOCK_CODE, STOCK_NAME) values (?, ?)
做完了
下载它– Maven-Hibernate-annotation-MySQL-Example.zip (8KB)
翻译自: https://mkyong.com/hibernate/maven-hibernate-annonation-mysql-example/