Skip to content

nfriaa/hibernate-tutorial2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hibernate-tutorial2

XML Mapping

  • JavaSE 1.8
  • Hibernate 5
  • Maven 4
  • MySQL 5

contributions welcome Travis license

Description

A sample code to execute Queries under Hibernate ORM (not directly to sql)

1. Database and tables

We will use the same database and table structure in Tutorial 1

2. Create maven project and add dependencies

<!-- MySQL connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency> <!-- Hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.11.Final</version> </dependency> 

3. Create POJO (Plain Old Java Object) class

public class Product { private int id; private String name; private int price; // getters and setters here... } 

4. Create the hibernate config file '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.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/persist_db</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="show_sql">true</property>** <mapping file="src/main/java/net/isetjb/hibernatetutorial2/Product.hbm.xml"/> </session-factory> </hibernate-configuration> 

5. Create HibernateUtil.java

Hibernate Utility class with a convenient method to get Session Factory.

6. Create a main Application class

  • a class whith main method to test connection
  • implement CRUD operations

About

Hibernate tutorial 2 : XML Mapping

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages