Problem Faced in Hibernate

Written by admin on . Posted in Hibernate

Here i will add all the problem related to Hibernate which i have faced while developing the Application in Hibernate.

Problem Type: java.lang.ClassCastException: org.hibernate.type.StringType cannot be cast to org.hibernate.type.VersionType

Problem In: Hibernate Tool While reverse engineering

Reason for this problem: In one of my table there was a field named version. Which Hibernate tool treated as field version and try to cast the same and it throws an exception becasue the type of the field was not what hibernate tool was expecting.

Resolution: Open the hbm configuration file for corresponding table and change the configuration of that field from <version> to <property> .

Other Detail:  More details could be found here

—————————————————————————————-

Difference between

<property name=”hibernate.show.sql”>true</property>  &

<property name=”show_sql”>true</property>

Second one is displaying the query while first one is not displaying query in console.

Why Hibernate is not working properly if default schema is provided explicitly

If  default schema is provided in connection URL and explicitly then it cause problem inn Hibernate

<property name=”hibernate.connection.url”>jdbc:mysql://localhost/abc</property>

<property name=”hibernate.default_schema”>abc</property>

—————————————

Problems: If using Hibernate Tools and there is two tables with same name only difference is for number or special character(ABC & ABC1 & ABC_) then tool will throw error saying the generated classes are same for theses tables.

Resolution: Manual class creation can be done. To exclude given Tables while creating mapping and class file reveng.xml file can be used.

 

Hibernate Association

Written by admin on . Posted in Hibernate

What is Hibernate Association: Any Table in DB could be connected to other tables in same or other DB. And these tables could be associated with each other via some keys(Foreign..). Those kind of scenario can be handled with association.

E.g: There are 2 table in DB, Student  & Subject. A student can study one subject or more then one subject. In that case every student will have only one entry in Student table but Subject table could contain more than one record for corresponding record in Student table.

Association mapping are used to map a Java Object to DB table.

Entities envoled in mappings:

  • Class (Persistence)(POJO)
  • DB Table
  • Mapping file(.hbm)

Rules associated to POJO:

  • POJO class needs to have default constructor as it is required by Hibernate.
  • Lazy loading doesn’t work with Final class. Its preferable to have Non Final classes.

Type of Association

Unidirectional Association:

  • Many To One XML Example
  • Many To One Annotation Example
  • One to One XML Example
  • One to One Annotation Example
  • One to Many XML Example
  • One to Many Annotation Example

Unidirectional Association with join Table

  • One to Many XML Example
  • One to Many Annotation Example
  • Many to One XML Example
  • Many to One Annotation Example
  • One to One XML Example
  • One to One Annotation Example
  • Many to Many XML Example
  • Many to Many Annotation Example

Bidirectional Association

  • One to Many
  • One to One XML Example
  • One to One Annotation Example

Bidirectional Association with join Table

  • One to Many XML Example
  • Many to Many XML Example

 

Hibernate Framework Basic

Written by admin on . Posted in Hibernate

What is Hibernate : Hibernate is an object-relational mapping (ORM) library for the Java language, providing a framework for mapping an object-oriented domain model to a traditional relational database. Hibernate solves object-relational impedance mismatch problems by replacing direct persistence-related database accesses with high-level object handling functions.

Hibernate facilitates the storage and retrieval of Java domain objects via Object/Relational Mapping.

What Hibernate Does:

  • Map Java class to database tables & vice versa
  • Data query and retrieval facility
  • Generate the SQL query based on the underline DB. and attempts to relieve the developer from manual result set handling and object conversion.
  • Make application portable to all relational DB.
  • Enhance performance by providing the different levels of cache(First, Second and Query level).

What is HQL: HQL is abbreviation of Hibernate Query Language. It is SQL inspired language provided by hibernate. Developer can write SQL like queries to work with data objects.

What is Dialect: Each SQL vendor has its own set of supported syntax. This is known as dialect. In order to generate appropriate sql query Hibernate needs to know, for which DB query needs to be generated. Hibernate does it by org.hibernate.dialect.Dialect class and its subclass for each vendor.

E.g.:

DB2       :   org.hibernate.dialect.DB2Dialect

MySQL  :   org.hibernate.dialect.MySQLDialect

Connection Pooling: Hibernate has its own internal connection pooling but it also support some third party connection pooling for production use.

  • c3p0 connection pool
  • Proxool connection pool
  • Obtaining connections from an application server, using JNDI