Wednesday, September 09, 2009

Hibernate Schema tools

The hbm2ddl.auto column indicates the values for the hbm2ddl.auto property in the hibernate configuration file

The Purpose column indicates what is the impact of using the specified hbm2ddl.auto value

The Class Name column indicates how to achieve the “purpose” programmatically.

 

hbm2ddl.auto option

Purpose

Class name

create or create-drop or

create – to drop the schema and create it whenever SessionFactory is built

create-drop – to drop the schema when SessionFactory is closed.

 

SchemaExport

e.g.

SchemaExport ex = new SchemaExport( new Configuration().configure() );

Ex.create(false, true);

// false – don’t print sql to stdout

// true – Execute ddl immediately on database

Update

Update – Read the schema through JDBC driver (not reliable) and make schema changes to fit the schema to mappings

SchemaUpdate

e.g.

SchemaUpdate su =

New SchemaUpdate(new Configuration().configure() );

Su.execute(false); // don’t print ddl on stdout

Validate

Validator – compare mapping with metadata and throw exception if they do not match.

SchemaValidator

e.g.

new SchemaValidator(new Configuration().configure()).validate()

 

Hibernate configuration file simple example with explanation in comments

<!DOCTYPE hibernate-configuration SYSTEM

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

      <session-factory>

            <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>

            <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/lab</property>          

            <property name="hibernate.connection.username">meridian</property>

            <property name="hibernate.connection.password">meridian</property>

 

            <!--

             | Which variant of SQL to use 

             +-->

            <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>

 

            <!-- Use the C3P0 connection pool provider -->

            <!--

             | Minimum number of connections in the pool is 5. Atleast 5 connections should

             | be there in the pool

             +-->

            <property name="hibernate.c3p0.min_size">5</property>

            <!--

             | Maximum number of connections in the pool is 20. There are already 20 

             | connections and more connections are requested, then a runtime exception is thrown.

             | This is the only mandatory config. option for c3p0 (connection pool used by

             | hibernate out-of-the-box.

             +-->

            <property name="hibernate.c3p0.max_size">20</property>

            <!--

             | If a connection is not used for 300 seconds, remove it from the pool

             +-->

            <property name="hibernate.c3p0.timeout">300</property>

            <!--

             | Prepared statements are cached for performance benefits. If 50 prepared statements 

             | are cached, the oldest prepared statement is moved out of cache

             +-->

            <property name="hibernate.c3p0.max_statements">50</property>

            <!--

             | If a connection is idle for 3000 seconds, validate it.

             +-->

            <property name="hibernate.c3p0.idle_test_period">3000</property>

 

            <!-- Show and print nice SQL on stdout -->

            <!--

             | 1. Could also be hibernate.show_sql. 'hibernate' prefix is optional in xml files.

             | 2. Refer to org.hibernate.cfg.Environment for a list of all property names

             | 3. This could also be ...name="show_sql">${displaysql}</... Then a system property

             |    by name "displaysql" will have to be defined as in java -displaysql=true 

             +-->

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

           

            <!--

             | If the same property is specified in a properties file, this would have been

             | hibernate.show_sql = true 

             +-->

            <property name="format_sql">true</property>

 

            <!--

             | List of XML mapping files

             +-->

            <mapping resource="lab/Event.hbm.xml" />

            <mapping resource="lab/Phone.hbm.xml" />

      </session-factory>

</hibernate-configuration>

Tuesday, September 08, 2009

Simple Logging Facade 4 Java (SLF4J)

A peer of commons logging. Both are logging interfaces that applications have dependencies to. The calls to SLF4J or commons logging APIs is finally implemented by log4j or jdk logging.

hbm stands for Hibernate metadata model

 

Exception encountered in static block must be wrapped in a ExceptionInInitializerError

 

Hibernate term - transactional write-behind

Hibernate, while synchronizing the persistent object changes, optimizes the list of SQL statements issued. This optimization prevents foreign key violations but still be predictable.

Hibernate term - cascading save

When a new instance of a persistence object is created, it does not have to be saved explicitly as long as it is reachable from a already persistent instance.

Hibernate term - Automatic Dirty Checking

Hibernate determines changes made to state of persistent objects during the course of a session (a unit of work with the db) and automatically synchronizes those changes with the persistence store.

Starting HSQL Database manager

Presuming hsqldb.jar is present in the ‘lib’ sub-directory of the current directory,

 

java -classpath ./lib/hsqldb.jar  org.hsqldb.util.DatabaseManagerSwing &

Starting HSQLDB

Presuming hsqldb.jar is present in the ‘lib’ sub-directory of the current directory,

 

java -classpath ./lib/hsqldb.jar  org.hsqldb.server.Server &

Monday, September 07, 2009

Maven build lifecycles, phases, plug-ins and goals

Source

Maven default bindings

Source: http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Built-in_Lifecycle_Bindings

Each phase of a build lifecycle in implemented by a set of goals. These goals are part of plugins.

Which goals handle which phases is specified by binding the goals(s) to phases. Following is the list of default such bindings.

Some phases have goals binded to them by default. And for the default lifecycle, these bindings depend on the packaging value. Here are some of the goal-to-build-phase bindings.

Clean Lifecycle Bindings

clean

clean:clean

Default Lifecycle Bindings - Packaging ejb / ejb3 / jar / par / rar / war

process-resources

resources:resources

compile

compiler:compile

process-test-resources

resources:testResources

test-compile

compiler:testCompile

test

surefire:test

package

ejb:ejb or ejb3:ejb3 or jar:jar or par:par or rar:rar or war:war

install

install:install

deploy

deploy:deploy

Default Lifecycle Bindings - Packaging ear

generate-resources

ear:generateApplicationXml

process-resources

resources:resources

package

ear:ear

install

install:install

deploy

deploy:deploy

Default Lifecycle Bindings - Packaging maven-plugin

generate-resources

plugin:descriptor

process-resources

resources:resources

compile

compiler:compile

process-test-resources

resources:testResources

test-compile

compiler:testCompile

test

surefire:test

package

jar:jar and plugin:addPluginArtifactMetadata

install

install:install and plugin:updateRegistry

deploy

deploy:deploy

Default Lifecycle Bindings - Packaging pom

package

site:attach-descriptor

install

install:install

deploy

deploy:deploy

Site Lifecycle Bindings

site

site:site

site-deploy

site:deploy

 

Phases of the maven lifecycles

Source: http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference

Clean Lifecycle

pre-clean

executes processes needed prior to the actual project cleaning

clean

remove all files generated by the previous build

post-clean

executes processes needed to finalize the project cleaning

Default Lifecycle

validate

validate the project is correct and all necessary information is available.

initialize

initialize build state, e.g. set properties or create directories.

generate-sources

generate any source code for inclusion in compilation.

process-sources

process the source code, for example to filter any values.

generate-resources

generate resources for inclusion in the package.

process-resources

copy and process the resources into the destination directory, ready for packaging.

compile

compile the source code of the project.

process-classes

post-process the generated files from compilation, for example to do bytecode enhancement on Java classes.

generate-test-sources

generate any test source code for inclusion in compilation.

process-test-sources

process the test source code, for example to filter any values.

generate-test-resources

create resources for testing.

process-test-resources

copy and process the resources into the test destination directory.

test-compile

compile the test source code into the test destination directory

process-test-classes

post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.

test

run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.

prepare-package

perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above)

package

take the compiled code and package it in its distributable format, such as a JAR.

pre-integration-test

perform actions required before integration tests are executed. This may involve things such as setting up the required environment.

integration-test

process and deploy the package if necessary into an environment where integration tests can be run.

post-integration-test

perform actions required after integration tests have been executed. This may including cleaning up the environment.

verify

run any checks to verify the package is valid and meets quality criteria.

install

install the package into the local repository, for use as a dependency in other projects locally.

deploy

done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

Site Lifecycle

pre-site

executes processes needed prior to the actual project site generation

site

generates the project's site documentation

post-site

executes processes needed to finalize the site generation, and to prepare for site deployment

site-deploy

deploys the generated site documentation to the specified web server

 

Nice maven article

http://blogs.plexibus.com/2007/12/02/maven-guide-part-one-basics/

Friday, September 04, 2009

Creating a empty maven project structure

Maven can itself create empty project structures when you want to start a new code base. To create an empty project structure, maven needs to be told the kind of project (simple servlet web application or JSF web application with Spring and hibernate or Web application with spring, hibernate and spring MVC etc…). Maven knows the details of the directory structure for each project type through an archetype. So a simple servlet web application will have its own archetype that details how the directory structure of a simple servlet web application looks like.

 

So to start with execute

 

mvn archetype:generate

 

Then Maven will ask you the kind of project you want to create by listing the archetype that it knows about and asking you to select the archetype. Then it wail ask other details about your project (group id, artifact id etc… ) and after all project details are provided, will create the empty project structure for you.

 

Source

Hibernate Getting Started

Source: Hibernate Reference documentation

Hibernate Getting Started


Hibernate Mapping File - Specifies how instances of persistent classes are to be persisted and loaded. This specification is laid out by detailing the properties that are mapped to columns, classes that are mapped to tables and the relationship between classes mapped in DB semantics like foreign keys (and join tables?).

Basic Hibernate Mapping File (there are multiple listings of the same file. Each listing builds upon the previous listing):

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  <!-- This file will first be looked up in classpath, then on the web. -->
<hibernate-mapping package="org.hibernate.tutorial.domain">
[...]
</hibernate-mapping>


<hibernate-mapping package="org.hibernate.tutorial.domain">
  <class name="Event" table="EVENTS">
    <!-- All classes that represent persistent entities, must be mapped to a table -->
    <!-- This tells hibernate to persist and load instances of Event class to the EVENTS table. Each instance of Event is a row in EVENTS table -->
  </class>
</hibernate-mapping>

<hibernate-mapping package="org.hibernate.tutorial.domain">
  <class name="Event" table="EVENTS">
    <id name="id" column="EVENT_ID">
 
    <!--
The class Event bean has a property (or getter and setter) by name "id" that serves as a key or id of Event class. The id has to be stored to the column EVENT_ID. -->
      <generator class="native"/>
        <!-- Use hibernate's key generation strategy (How are identifier values generated?) to generate the Keys for Events. Other strategies include Data base generated keys, Globally unique keys, application generated keys -->
    </id>
  </class>
</hibernate-mapping>

<hibernate-mapping package="org.hibernate.tutorial.domain">
  <class name="Event" table="EVENTS">
    <id name="id" column="EVENT_ID">
      <generator class="native"/>
    </id>
    <!-- Other properties of Event class. If a property is not mapped, it is not considered persistent. -->
    <property name="date" type="timestamp" column="EVENT_DATE"/>
      <!-- Event has a getDate() and setDate() and the value is to be stored in the column named EVENT_DATE. Type is hibernate mapping type. There are converters that convert between Java to SQL data types and visa-versa. If type not specified, hibernate will try to determine the mapping type and conversion type. This automatically (detected through reflection - can impact start up performance) type may not be what you want. For example java.util.Date can map to "date" (only date e.g. July 4th, 2009), "timestamp" (both date and time e.g. July 4th, 2009 10:00:00 HRS) or "time" (only time e.g. 10:00:00 HRS). Then you explictly specify the type -->
    <property name="title"/>
      <!-- Event has a getTitle() and setTitle(). Column name is not specified, so use the property name as the column name. -->
  </class>
</hibernate-mapping>


"Hibernate mapping file" specified how to load and store objects of the persistent class

 

Attitude, to me is more important than facts, education, money, circumstances, failures, successes and what others think and say

“The longer I live, the more I realize the impact of attitude on life. Attitude, to me is more important than facts. It is more important than the past, than education, than money, than circumstances, than failures, than successes, than what other people think or say or do. It is more important than appearance, giftedness or skill. It will make or break a company …a church …a home. The remarkable thing is we have a choice everyday regarding the attitude we will embrace for that day. We cannot change our past …we cannot change the fact that people will act in a certain way. We cannot change the inevitable. The only thing we can do is play on the one string we have, and that is our attitude. I am convinced that life is 10% what happens to me and 90% how I react to it. And so it is with you. We are in charge of our attitudes.”

- Charles Swindoll through Julie911

Thursday, September 03, 2009

Logic that needs to be executed only in the JSF Render Response Phase

if (FacesContext.getCurrentInstance().getRenderResponse()) {
    // Logic that needs to be executed only in the JSF Render Response Phase
}
 
Source - balusc

 

Have you found joy in your life? 'Has your life brought joy to others?

“You know, the ancient Egyptians had a beautiful belief about death. When their souls got to the entrance to heaven, the guards asked two questions. Their answers determined whether they were able to enter or not. ‘Have you found joy in your life?’ ‘Has your life brought joy to others?’”

- The Bucket List through Julie911

Wednesday, September 02, 2009

Look around you - played in Lost

Oh, look around you
Look down the bar from you
The lonely faces that you see
Are you sure that this is where you want to be
These are your friends
But are they real friends
Do they love you the same as me
Are you sure that this is where you want to be
You seem in such a hurry to live this kind of life
You've caused so many pain and misery
Look around you, take a good look
And tell me what you see
Are you sure that this is where you want to be
Don't let my tears persuade you, I had hoped I wouldn't cry
But lately, teardrops seem a part of me
Oh, look around you, take a good look
At all the local used-to-be's
Are you sure that this is where you want to be

Willie Nelson

Tree conflicts

Friday, August 28, 2009

Medicare

Medicare is health insurance for people age 65. Should have legally entered and lived in u.s. for 5 years. There are 2 parts to it,

Part A - in patient services. This is free as it is paid for through medicare taxes when one worked.

Part B - out patient and other medical expenses - need to pay a premium ($96.40 in 2008)

media multitasking v. task multitasking

http://www.npr.org/templates/story/story.php?storyId=112334449&ft=1&f=5

Our abilities, frailties and virtues

“Ninety percent of the world’s woe comes from people not knowing themselves, their abilities, their frailties, and even their real virtues. Most of us go almost all the way through life as complete strangers to ourselves.“
— Sydney J. Harris through julie911

Thursday, August 27, 2009

Nice trick with xargs

ls | xargs  -t -I{} echo {} {}.new

 

-t echo the command that is going to get executed before executing

-I{} Insert a new line where{} appears

 

e.g.

 

$ ls

first.txt  second.txt

 

$ ls | xargs  -t -I{} echo {} {}.new

echo first.txt first.txt.new

first.txt first.txt.new

echo second.txt second.txt.new

second.txt second.txt.new

Building a massive system is like building a simple API - Eric

In response to Steve indicating that Eric is pretty good at building big systems from the scratch

Good schooling info

One more school to the list - Chinmaya Vidyalaya

Top grade schools in chennai

From here : http://www.indusladies.com/forums/schools/4665-age-criteria-admission-lkg-chennai.html

 

PSBB, DAV, chettinad vidhyasharm, State Bank of India Officers Association (SBOA) school, Vidya mandir, PS senior

Schooling for kids in Chennai

Nice thoughts from http://goodschoolsofchennai.blogspot.com/

 

1. Identifying the right skill and potential in the kid 

2. Identify what interests the child. 
3. Get your aspirations in line with what the Kids interest is.
4. Understand what needs to be provided to the kid to thrive on his interest.
5. Providing the right environment at home, school and the places that we visit regularly to inspire them to pursue their interest.
6. Providing the tools for them to nurture their talent. 
7. Giving the quality time and feedback for them to improve. 
8. Providing the freedom for them to express their point of views.

Wednesday, August 26, 2009

God is behind me - My father

When we came across a pretty helpful Delta airways staff while checking in the baggage at Salt lake city Airport on their (my parents) way to India.