Maven 3 + Hibernate 3.6 + Oracle 11g Example (Annotation)

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 :

  1. Maven 3.0.3
  2. JDK 1.6.0_13
  3. Hibernate 3.6.3.final
  4. 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.jar module. In previous version, for example, Hibernate 3.2, you need to include extra hibernate-annotations.jar to 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.

Bye bye AnnotationConfiguration
Read this – AnnotationConfiguration is deprecated in Hibernate 3.6

6. Review Final Project Structure

Review your project structure :

folder 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.

Reference

  1. Hibernate 3.6 documentation
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: insurance resources()

  • Pingback: alkaline water()

  • Pingback: electricity()

  • Pingback: water ionizer payment plan()

  • Pingback: paypal loans()

  • Pingback: alkaline water brands()

  • Pingback: payment plan()

  • Pingback: how to become an electrician in ma()

  • Pingback: house blue()

  • Pingback: locksmiths durham nc()

  • Pingback: orange county electricians union()

  • Pingback: water ionizer loan()

  • Pingback: car parking()

  • Pingback: car parking()

  • Pingback: laan penge nu og her 18 aar()

  • Pingback: mobile porn()

  • Pingback: lan penge nu uden renter()

  • Pingback: tvpackages.net()

  • Pingback: online casinos()

  • Pingback: light gloves()

  • Pingback: Blocked Sited in Dubai 2015()

  • Pingback: kangen()

  • Pingback: water ionizer()

  • Pingback: streaming movies()

  • Pingback: weight loss techniques()

  • Pingback: Blue Coaster33()

  • joshy

    Gr8 work, you have always been very helpful ..Thanks

  • michoser

    Hi,

    DBUser.java:[11,1] annotations are not supported in -source 1.3
    (use -source 5 or higher to enable annotations)
    @Entity

    I am taking this error when I run mvn compile. I am using ubuntu 12.04, openjdk-7-jdk

    I googleit but I didn’t found anything about it. Can anyone help me ?

    • rahul

      I think it is taking jre 1.3 version. Hence saying annotation not supported, use jre 5 or higher. Open dos & type java -version or check your environmental var in system->props.

  • http://None Cristiano

    Great tutorial. Best one found on the net.

    Just one problem upon launching. File “hibernate.cfg.xl” cannot be found in “src/main/resources” folder. Here’s the error:

    Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found
    Exception in thread “main” java.lang.ExceptionInInitializerError
    at com.mkyong.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)
    at com.mkyong.util.HibernateUtil.(HibernateUtil.java:8)
    at com.mkyong.App.main(App.java:11)
    Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found
    at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1497)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1519)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1506)
    at com.mkyong.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
    … 2 more

    Any hint?

    Thanks. Bye.

  • Anirtak

    You dont use com.mkyong.util.HibernateUtil.shutdown() in the App class ??

  • eric

    Great post! one question, how do you annotate multi columns unique key? Thanks!

  • Pingback: Maven 2 + Hibernate 3.2 + MySQL Example (Annotation)()

  • anil pandeyh

    this is good post no doubt.
    it can be great if explanation of annotations is given.
    and it should be there

  • C

    Best tutorials I have seen on the topic. Thanks.

  • Arun

    It’s not incrementing , beside it starts with zero and after that no increment (Exception in thread “service” org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update)

    Thank You

  • http://www.skodeg-o.com/feed Dalen

    This is a great post! Thank you very much!