Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts

Friday, 4 November 2016

Hibernate4 : What is cause of error - Hibernate: “Field 'id' doesn't have a default value” ?


Error : Hibernate: “Field 'id' doesn't have a default value”

Cause : Sometimes changes made to the model or to the ORM may not reflect accurately on the database

(Changes made in model after database created)

Solution : Drop and Re-create the table / database

Monday, 25 April 2016

How to select multiple columns using Criteria API ?


Set Multiple projections
ProjectionList p1 = Projections.projectionList();
p1.add(<projection1>);
p1.add(<Projection2>);
c.setProjection(p1);


Example : Select names and address who are from Agra having age more than 60 in order of Name



Crietria c = session.createCriteria(Person.class);
c.add( Restrictions.like("city","AGRA") );
c.add( Restrictions.gt("age",60) );
c.addOrder( Order.asc("name") );

ProjectionList p1 = Projections.projectionList();
p1.add(Projections.property("name"));
p1.add(Projections.property("address"));
c.setProjection(p1);


List result = c.list();

Sunday, 24 April 2016

load vs. get method


load() vs. get()

1. Return value, if ID not found
  • load() method will throw an exception if the unique ID is not found in the database.
  • get() method will return null if the unique id is not found in the database.

2. Fetch technique
  • load() just returns a proxy or make a database hit if any other method than getId() is called
          - Lazy initialization ; offers better performance
  • get() will initialize the full objects using n number of database hits, if object could not found in session cache.

3. Use case
  • Only use the load() method if you are sure that the object exists.
  • If you are not sure that the object exists, then use one of the get() methods.

merge vs. update


saveOrUpdate()
 does the following :

  • if the object is already persistent in this session, do nothing.
  • if another object associated with the session has the same identifier, throw an exception.
  • if the object has no identifier property, save() it.
  • if the object's identifier has the value assigned to a newly instantiated object, save() it.
  • if the object is versioned (by a or ), and the version property value is the same value assigned to a newly instantiated object, save() it otherwise update() the object.

merge() is very different : 
  • if there is a persistent instance with the same identifier currently associated with the session, copy the state of the given object onto the persistent instance.
  • if there is no persistent instance currently associated with the session, try to load it from the database, or create a new persistent instance.
  • the persistent instance is returned.
  • the given instance does not become associated with the session, it remains detached.


So, Use : 
update()
if you are sure that the session does not contain an already persistent instance with the same identifier.

merge()
if you want to merge your modifications at any time without consideration of the state of the session.

Saturday, 23 April 2016

SQL vs. HQL


HQL vs. SQL

HQL is similar in appearance to SQL, but with some properties : 
  • HQL is fully object-oriented and understands various OOPs features
  • HQL is RDBMS independent