Posts

Showing posts from October, 2014

Hibernate 4 one to one mapping example using xml

Image
In this post we are going to create a project to implement the step by step one-to-one association example using hbm.xml file. Before that lets have a look at what exactly one-to-one association in RDBMS ? One-to-One association means, Each row of a table is mapped with exactly one and only one row with another table. For example, we have passport_detail and person table each row of passport_detail table is mapped with exactly one and only one row of person table. One person has only one passport. In hibernate one-to-one association can be implemented vi two different ways: Primary key associations Unique foreign key associations In this tutorial we are going to implement 1st approach Unidirectional one-to-one association vi primary key association. For 2nd approach you may follow this tutorial one-to-one association using unique foreign key OK, lets start our project. Here is an one-to-one association mapping of person and passport_detail table ER diagram: Tools and...

Hibernate one-to-one association on a foreign key using hbm.xml

Image
In this post we are going to create a project to implement the step by step unidirectional one-to-one association example using hbm.xml file. Before that lets have a look at what exactly one-to-one association in RDBMS ? One-to-One association means, Each row of a table is mapped with exactly one and only one row with another table. For example, we have passport_detail and person table each row of passport_detail table is mapped with exactly one and only one row of person table. One person has only one passport. In hibernate one-to-one association can be implemented via two different ways: Primary key associations Unique foreign key associations In this tutorial we are going to implement 2nd approach unidirectional "unique foreign key associations". For 1st approach follow this tutorial one-to-one primary key associations Here is an one-to-one association mapping of person and passport_detail table ER diagram: Tools and Technologies we are using here: JDK 7 Hib...

org.hibernate.HibernateException: /hibernate.cfg.xml not found

Exception in thread "main" java.lang.ExceptionInInitializerError Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173) at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1940) at org.hibernate.cfg.Configuration.configure(Configuration.java:1921) at org.hibernate.cfg.Configuration.configure(Configuration.java:1901) at com.hbm.util.HibernateUtility.buildSessionFactory(HibernateUtility.java:13) at com.hbm.util.HibernateUtility. (HibernateUtility.java:9) ... 2 more If you found "org.hibernate.HibernateException: /hibernate.cfg.xml not found" error, means hibernate.cfg.xml file is not available inside your project's class path. configure() method of Configuration class try to load hibernate.cfg.xml file from your class path. So put your hibernate.cfg.xml file inside src folder, otherwise you have to provide the locat...

What are the core classes and interfaces of hibernate 4

Hibernate is an Open source Object Relational Mapping framework. It enables developer to develop classes to be persist in an object oriented way including inheritance and the Java collections framework. Current hibernate stable release is 4.3.5 that was released in April 2014. It has various classes and interfaces, but sometimes interviewer ask you to list down the core classes and interfaces of hibernate. Well hibernate has various classes and interfaces, but when we say core classes and interfaces those are listed below, they all are the exceptional role in hibernate library. Configuration (class) ServiceRegistry (interface) SessionFactory (interface) Session (interface) Transaction (interface) Query (interface) Configuration: Is a class resides inside the org.hibernate.cfg package. Configuration as it's name implies it is all about the configuration of the application, configuration of the application done only at the initialization time which is only once for t...

Hibernate 4 Mysql Maven Java 7 Eclipse CRUD example using annotation

Image
Again,we are going to create a step by step very basic hibernate 4 annotation based CRUD application using maven . We have already created the same application without using maven here . In this project we have a single table called user and we are going to perform crud operations on the user table data only. Technologies & Tools we are using here: JDK 7 Hibernate 4.0 MySql 5.5 Eclipse Juno 4.2 Maven 3.2.2 Note : please make sure that maven plugin is installed in your eclipse. Overview of the project structure: Main Objects of this project are: pom.xml hibernate.cfg.xml Annotated Entity class Database Here is a user table description with very few columns: 1. Create table query: /*Table structure for table `user` */ DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(50) DEFAULT NULL, `password` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1; ...

Hibernate4 Mysql Eclipse CRUD Example using Annotation

Image
In this post we are going to create a step by step very basic hibernate4 annotation based CRUD application. We have already created the same application using hbm.xml mapping file here . In this project we have a single table called user and we are going to play with user table only. Technologies/Tools we are using here: JDK 7 Hibernate 4.0 MySql 5.5 Eclipse Juno 4.2 Overview of the Project Structure: Project structure Main Objects of this project are: Jar files hibernate.cfg.xml Annotated Entity class Database Here is a user table description  with very few columns: 1. Create table query: /*Table structure for table `user` */ DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(50) DEFAULT NULL, `password` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1; 2. Create a Java project Create a lib folder (Optional): Righ...

Hibernate Mysql Eclipse crud example using hbm file with jar

Image
In this post we are going to create a very basic java CRUD application using hibernate to insert/update/select/delete records into MySql database. In this post we are using hibernate vi .hbm.xml file based not annotation based. In this project we have a single table called user and we are going to play with user table only. Technologies/Tools we are used here: JDK 7 Hibernate 4.0 MySql 5.0 Eclipse Juno project structure Our project will looks like this: To create a very basic hibernate crud application using hbm.xml file we need jar files hibernate.cfg.xml *.hbm.xml database Here is a user table with very few columns: 1. Create table query: /*Table structure for table `user` */ DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(50) DEFAULT NULL, `password` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1; 2. Create a ...

Why hashCode() method in java?

In java parent of all classes is Object, Object class is a super class of every classes and available inside java.lang package. hashCode() method is one of the Object class method mean every object implicitly or explicitly has a hashCode() method which returns a 32 bit signed integer number . The integer number return by the hashCode() method is the internal memory address of the object and it's useful when using the objects in a hash based collections or other algorithms that rely on hash codes such as HashMap, HashSet and Hashtable. hashCode() method is a native method so the implementation of hashCode() method is available inside the native code in JVM. general contract of hashCode() method are: whenever you invoked hashCode() method on the same object more than once during the execution of an application, the hashCode() method must return the same integer number. For example if you run the below statements more than once it'll return you the same integer number. publ...

Why getClass() method in java

getClass(): getClass method of java.lang.Object class is a final method means you cannot override the getClass method. Sometimes interviewer ask you to override the getClass method just to confuse you. But don't be confused all the method of Object class cannot be override except toString(),equals(Object o),hashCode(),clone() and finalize() once again getClass() is a final method of Object class and you cannot override the final methods . getClass method return the runtime class of the object which has method called. By using getClass method you will get the full information about the class such as number of methods in your class declared using getClass().getMethods() which will return you the array of methods, number of interfaces implemented by your class using getClass().getInterfaces() In nutshell you will get the whole information about your class using getClass() method. For example the below class is going to display you the number of methods,fields declared in Product...