Giter Site home page Giter Site logo

hibernate-tutorial2's Introduction

hibernate-tutorial2

Hibernate XML Mapping files (Obsolete !!! We will see in Tutorial 3 the mode "Hibernate Annotations")

contributions welcome Travis license

Description

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

  • JavaSE 8
  • Hibernate 5
  • Maven 4
  • MySQL 5

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 XML mapping file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="net.isetjb.hibernatetutorial2.Product" table="product">
        <meta attribute="class-description">
            Add your class description here.
        </meta>
        <id name="id" type="int" column="id">
            <generator class="native"/>
        </id>
        <property name="name" type="string">
            <column name="name" length="255" not-null="true"/>
        </property>
        <property name="price" type="int">
            <column name="price" not-null="true"/>
        </property>
    </class>
</hibernate-mapping>

5. Create the hibernate config file 'hibernate.cfg.xml' (in ressources folder)

<?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.url">jdbc:mysql://localhost:3306/persist_db?useTimezone=true&amp;serverTimezone=UTC</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        <property name="show_sql">true</property>
        <mapping file="src/main/java/net/isetjb/hibernatetutorial2/Product.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

6. Create HibernateUtil.java

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

7. Create main Application class

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

hibernate-tutorial2's People

Contributors

nfriaa avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.