Saturday, January 25, 2014

Spring Course: Video 136 - V147 Hibernate

Video 136: Introduction Hibernate
nothing here

Video 137: A simple Hibernate Query
1- add the dependencies to pom.xml, we add spring-orm and hibernate-core

2- in datasource.xml add a bean of AnnotationSessionFactoryBean

as you can see you set the datasource, the hibernateProperties you add hibernate.dialect which is the database that you are using.
PackagesToScan, here we set the packages that have the Entities

3- now in UsersDao we were using select statements, now we will use Hibernate,

as you can see, we added SessionFactory, we defined a session(), and we use createQuery() method.

4- now create your entities:

as you see, we use @Entity, @Table and so on...

Video 138: Saving Objects
to save an object to database

Video 139 Validation Groups and Password Encryption
currently, we have the following:

as you can see the password check the size to be less than 15,

however, as we want to encrypt the password before saving we will write this

Hibernate will throw an exception here, because when we encrypt the password the size will be more than 15,

In order to fix this issue, we should tell spring to use the validation only when we submit the form not when we save to database

In order to do that we should use something called Validation Groups,

1- define 2 interfaces PersistanceValidationGroup and FormValidationGroup, sure the name is not important.

2- no simply you do this

as you can see, for each validation you assign the group that you want,
in case of passoword size we assigned it to the FormValidationGroup.

3- now we should tell the form to use the FormValidationGroup:

as you can see we use @Validated(FormValidationGroup.class).

4- we should tell hibernate to use PersistanceValidationGroup

as you can see, we add this to the bean definition as property.

Video 140: Translating Hibernate Exceptions to Spring Exceptions
if you remember we defined this ErrorHandler before

as you can see the handler catch all DataAccessException.

However, Hibernate doesnt throw this exception, so inorder to catch Hibernate database exceptions we should do exception translation:

1- add this bean PersistenceExceptionTranslationPostProcessor

2- add @Repository to classes that do hibernate operations:

Video 141: Queries With Criteria
in order to run queires with Criteria in hibernate

Video 142: Mapping Many-to-one Relationships
in order to define Many-to-one relationship, for example offers and users relationship

Join column is the freign key

Video 143: Restriction on Joind Tables
for example, if you want to get offers related to a specific user

Video 144: Multiple Criteria
in order to make a query with multiple criteria

Video 145: Updating Objects

as you can see we are using session().update or session().save.

also there is another method which is session().saveOrUpdate().

Video 146: Deleting Object

as you can see we createQuery() in order to delete

Video 147: Completing the Offers DAO
Nothing important here

No comments:

Post a Comment