Maven 3 + Hibernate 3.6 + Oracle 11g Example (Annotation)
2015-09-07 10:31
330 查看
This tutorial will reuse and modify the previous Hibernate3.6 XML mapping tutorial, but replace the Hibernate mapping file (hbm) with Hibernate / JPA Annotation code.
Technologies in this article :
Maven 3.0.3
JDK 1.6.0_13
Hibernate 3.6.3.final
Oracle 11g
Note
Since Hibernate 3.6, the annotation is integrated into the
File :
Update
To this :
Technologies in this article :
Maven 3.0.3
JDK 1.6.0_13
Hibernate 3.6.3.final
Oracle 11g
1. pom.xml
No change in ·pom.xml· file, all previous Hibernate3.6 XML mapping tutorial dependency can be reused.Note
Since Hibernate 3.6, the annotation is integrated into the
hibernate-core.jarmodule. In previous version, for example, Hibernate 3.2, you need to include extra
hibernate-annotations.jarto make it works.
2. Delete Hibernate Mapping file (hbm)
Delete the “DBUser.hbm.xml” file, it’s no longer require.
3. Update Model
Update “DBUser.java“, puts JPA annotation code inside.
File : DBUser.java
package com.mkyong.user; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity @Table(name = "DBUSER") public class DBUser implements java.io.Serializable { private int userId; private String username; private String createdBy; private Date createdDate; public DBUser() { } public DBUser(int userId, String username, String createdBy, Date createdDate) { this.userId = userId; this.username = username; this.createdBy = createdBy; this.createdDate = createdDate; } @Id @Column(name = "USER_ID", unique = true, nullable = false, precision = 5, scale = 0) public int getUserId() { return this.userId; } public void setUserId(int userId) { this.userId = userId; } @Column(name = "USERNAME", nullable = false, length = 20) public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } @Column(name = "CREATED_BY", nullable = false, length = 20) public String getCreatedBy() { return this.createdBy; } public void setCreatedBy(String createdBy) { this.createdBy = createdBy; } @Temporal(TemporalType.DATE) @Column(name = "CREATED_DATE", nullable = false, length = 7) public Date getCreatedDate() { return this.createdDate; } public void setCreatedDate(Date createdDate) { this.createdDate = createdDate; } }
4. Update Hibernate Configuration File
Update “hibernate.cfg.xml“, replace the “
mapping resource” with “
mapping class”
Update
hibernate.cfg.xml, from this :
<hibernate-configuration> <session-factory> <!-- ..... --> <mapping resource="com/mkyong/user/DBUser.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
To this :
<hibernate-configuration> <session-factory> <!-- ..... --> <mapping class="com.mkyong.user.DBUser"></mapping> </session-factory> </hibernate-configuration>
5. Hibernate Utility
No update on “HibernateUtil.java“, since Hibernate 3.6, both XML mapping and annotation are sharing the same “
org.hibernate.cfg.Configuration” class.
6. Review Final Project Structure
Review your project structure :7. Run It
No update on “App.java“, as well, just run it, and you should be seeing the same result as previous Hibernate3.6 XML mapping tutorial.
相关文章推荐
- 问题:oracle decode;结果:oracle中的decode的使用
- Maven 3 + Hibernate 3.6 + Oracle 11g Example (XML Mapping)
- Oracle Database - ORA-01720 - grant option does not exist for 'AnObject'
- oracle数据库表的解锁问题
- oracle 10g dataguard物理standby配置 (数据文件以文件系统存储)
- oracle加固
- ORACLE 数据库 rman 全备份
- oracle完全删除表空间
- Oracle pctfree和pctused详解
- 解决在mybatis中使用CTE进行oracle查询数据类型为long的字段出现流关闭问题
- 解决在mybatis中使用CTE进行oracle查询数据类型为long的字段出现流关闭问题
- oracle的备份与恢复
- JDBC连接Oracle数据库时出现的ORA-12505错误及解决办法
- oracle9i与10g与11g的区别
- wampServer连接oracle
- oracle 10g的long类型处理bug
- Mysql与Oracle的区别
- oracle安装和介绍
- Oracle数据库中的优化方案
- Oracle数据库表更新问题