Search This Blog

Monday, 16 January 2012

Insert,Update,Delete via Procedures and Functions

In the previous post, we saw how to create custom queries for Entity updating, insertion and deletion.Hibernate allows the use of both SQL procedures and functions to perform the Create/Delete and Update operations. I used the same entity class (Item) but instead of using the auto-increment property, I used the assigned generator. The reason for the change was issues faced in getting the insert procedure to work with auto-increment value. Hibernate needs this value which it is the result of the session.save method.

Monday, 9 January 2012

Creating Custom Insert,Update,Delete Queries

At start up Hibernate generates standard CRUD queries for each Entity that is mapped in Hibernate. However just like custom loaders, Hibernate provides with the option of using our own custom queries for the Create, Update and Delete (CUD) Operations

Creating Custom Entity Loaders

At its basic level Hibernate deals with SQL. All the fancy methods and HQL strings that we write end up being converted into the SQL that is understood by the database. Hibernate generates the appropriate SQL best suited for the Database provider we are using based on the dialect we have set in our hibernate configuration file.
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
There is a very rare probability that we may find the SQL queries generated unsuitable for our use. In such cases Hibernate comes with the option of overriding these queries with our own SQL queries.

Sunday, 8 January 2012

Composite Foreign Key referencing Nonprimary Keys

In the previous post we had a scenario wherein the foreign key was not actually referring to the primary key but instead to another natural key. Let us take this not -so normal scenario a step further into the weird. What if the natural key is formed by a group of columns as opposed to the single column natural key we saw earlier ?

Tuesday, 3 January 2012

Foreign Key referencing Nonprimary Keys

Usually a foreign key references a Primary Key. However it is possible to have an extreme scenario wherein the foreign key refers a unique column in another table. To illustrate the above case let us create a simple(but highly contrived) example.

Sunday, 1 January 2012

Compisite Identifiers And Associations-2

Continuing from the previous post, if Student were to have a many-to-one association with Books, then the foreign-key(user_name) would also be present in this association.