Posts

Showing posts from January, 2015

Hibernate Component Mapping Example Using Annotations

Image
In my previous post we have seen the example of component mapping using hbm.xml, you can traverse my previous post from Component-Mapping example using hbm.xml . Here we are going to show you the example of component mapping using annotation. Once you create a component that component can be re-use by several places. Here is the text I copied from the documentation "A component is a contained object that is persisted as a value type and not an entity reference. The term "component" refers to the object-oriented notion of composition and not to architecture-level components" Here is a project table ER diagram we are using for this post. We will create a component class with ide,os,language,db fields. Tools and Technologies we are using here: JDK 7 Hibernate 4.3.7 MySql 5.1.10 Eclipse Juno 4.2 Maven 3.2 Overview of the Project Structure: Main Objects of this project are: pom.xml hibernate.cfg.xml annotated pojo database Step 1. Create dat...

Hibernate Component Mapping Example Using XML

Image
In this post we are going to implement the component mapping, component is again one of the important feature of hibernate, once you create a component you can re-use this component on several places where required. In this post we are implementing component mapping using hbm.xml file. Here is a project table ER diagram we are using for this post. Tools and Technologies we are using here: JDK 7 Hibernate 4.3.7 MySql 5.1.10 Eclipse Juno 4.2 Overview of the Project Structure: Main Objects of this project are: jar files hibernate.cfg.xml *.hbm.xml(mapping files) database Step 1. Create database script. CREATE DATABASE /*!32312 IF NOT EXISTS*/`hibernate_tutorial` USE `hibernate_tutorial`; /*Table structure for table `project` */ DROP TABLE IF EXISTS `project`; CREATE TABLE `project` ( `project_id` int(11) NOT NULL AUTO_INCREMENT, `project_name` varchar(255) DEFAULT NULL, `start_date` datetime DEFAULT NULL, `exp_end_date` datetime DEFAULT NULL, `language` va...

Java program to find missing number in an array

I just faced an interview where first round was technical written exam based on some logical question belongs to String, Array and Puzzle. One of the question was something like " We have a container having 100 balls each ball marked with a positive natural number from 1 to 100 for example 1,2,3,4,5.....100. Each ball marked with exactly only and only once number. So total 100 balls marked with 100 numbers ". Now one ball is randomly picked out from the container. Find the ball picked one with which number. Note: Try to find the missing one with quickest way. Lets try to solve this. Ok this can be doable with O(n). We have to iterate the natural numbers and compute the sum of natural numbers for example numbers to be calculated are from 1 to N. From the above problem statement N is 100 and the sum of N numbers can be expressed with Nx(N+1)/2 so sum will be 100X(100+1)/2 = 5050 and subtract the sum of array from 5050. Ok got it now lets implement this in java programing...

Hibernate Many To Many Mapping With Additional Columns Example

Image
In this post we are going to show you how to use Many-To-Many association with extra columns in hibernate using annotation. For Many-To-Many association example you can visit my previous post Many-To-Many association example using annotation or Many-To-Many association example using hbm.xml Here is Many-To-Many association mapping table ER diagram. Tools and Technologies we are using here: JDK 7 Hibernate 4.3.7 MySql 5.1.10 Eclipse Juno 4.2 Maven 3.2 Overview of the Project Structure: Main Objects of this project are: pom.xml hibernate.cfg.xml annotated pojo database Step 1. Create database script. CREATE DATABASE /*!32312 IF NOT EXISTS*/`hibernate_tutorial` USE `hibernate_tutorial`; /*Table structure for table `employee` */ DROP TABLE IF EXISTS `employee`; CREATE TABLE `employee` ( `employee_id` int(11) NOT NULL AUTO_INCREMENT, `doj` date DEFAULT NULL, `first_name` varchar(255) DEFAULT NULL, `last_name` varchar(255) DEFAULT NULL, PRIMARY KEY ...

Hibernate many to many mapping example using xml

Image
This article show you how to use Many-To-Many association in hibernate using hbm.xml mapping. For Many-To-Many association example using annotation you can visit my previous post Many-To-Many association example using annotation . Many-To-Many association means, One or more row(s) of a table are associated with one or more row(s) in another table. For example an employee may assigned with multiple projects, and a project is associated with multiple employees. In this post we are going to show you the same employee and project association using hbm.xml. Here is Many-To-Many association mapping table ER diagram. Tools and Technologies we are using here: JDK 7 Hibernate 4.3.7 MySql 5.1.10 Eclipse Juno 4.2 Overview of the Project Structure: Main Objects of this project are: jar files hibernate.cfg.xml *.hbm.xml(mapping files) database Step 1. Create database script. CREATE DATABASE /*!32312 IF NOT EXISTS*/`hibernate_tutorial` USE `hibernate_tutorial`; /*Table stru...

Hibernate many to many mapping example using annotation

Image
In this post we are going to show you how to use Many-To-Many association in hibernate using annotation. For One-To-Many association example you can traverse my previous post One-To-Many association example using annotation . Many-To-Many association means, One or more row(s) of a table are associated one or more row(s) in another table. For example an employee may assigned with multiple projects, and a project is associated with multiple employees. In this post we are going to show you the same employee and project association using annotation, below are the listed table for our this project. Employee table: employee_id first_name last_name doj 101 Tony Jha 2014-08-28 102 Zeneva S. Humphrey 2014-07-04 Project table: project_id project_name start_date exp_end_date 101 MARS 2013-01-01 2015-08-28 102 SBA 2014-10-02 2016-07-04 Employee_Proj table...

generator classes in hibernate example

In hibernate generator classes are used to generate unique identifiers for instances of the persistence class. Hibernate provides the list of built in generator classes to generate unique identifiers, all the generator classes implements the org.hibernate.id.IdentifierGenerator interface, and if your needs of unique identifiers is not solved by using built in generator classes, then you can create your own generator classes by implementing org.hibernate.id.IdentifierGenerator interface. Here is a example of custom generator class via implementing org.hibernate.id.IdentifierGenerator interface. Here are the list of built in generator classes provided by Hibernate. 1. org.hibernate.id.Assigned The default generator class provided by Hibernate, If you explicitly not provide any generator element then, Hibernate will use assigned as a default generator strategy. How to use: <id name="generatorId" column="generator_id"> <generator /> </id...