Monday, September 21, 2009

trying to learn Javascript

JavaScript is an interpreted programming language with object-oriented (OO) capabilities. Syntactically, the core JavaScript language resembles C, C++, and Java, with programming constructs such as the if statement, the while loop, and the && operator. The similarity ends with this syntactic resemblance, however. JavaScript is a loosely typed language, which means that variables do not need to have a type specified. Objects in JavaScript map property names to arbitrary property values. In this way, they are more like hash tables or associative arrays (in Perl) than they are like structs (in C) or objects (in C++ or Java). The OO inheritance mechanism of JavaScript is prototype-based like that of the little-known language Self. This is quite different from inheritance in C++ and Java. Like Perl, JavaScript is an interpreted language, and it draws inspiration from Perl in a number of areas, such as its regular-expression and array-handling features.

Book I am into currently


It's a book that will teach you how compilers work and you can build your own complier..yupp...u can!!

Thursday, August 6, 2009

Sorting using Native SQL on Hibernate..

Sorting...** from Issue Tracking Web App...

static
public java.util.List getListData2(
Class classBean, String strKey, Object value)
{
java.util.List result = new java.util.ArrayList();

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

Criteria criteria =
session.createCriteria(classBean);
criteria.addOrder(Order.asc("name"));
if (strKey != null) {
criteria.add(Restrictions.like(strKey, value));
}
result = criteria.list();

tx.commit();
session.close();
return result;
}

Thursday, July 30, 2009

Running Native SQL on Hibernate


Execution of native SQL queries is controlled via the SQLQuery interface, which is obtained by calling Session.createSQLQuery(). The following sections describe how to use this API for querying.

The most basic SQL query is to get a list of scalars (values).

sess.createSQLQuery("SELECT * FROM CATS").list();
sess.createSQLQuery("SELECT ID, NAME, BIRTHDATE FROM CATS").list();

These will return a List of Object arrays (Object[]) with scalar values for each column in the CATS table. Hibernate will use ResultSetMetadata to deduce the actual order and types of the returned scalar values.

To avoid the overhead of using ResultSetMetadata, or simply to be more explicit in what is returned, one can use addScalar():

sess.createSQLQuery("SELECT * FROM CATS")
.addScalar("ID", Hibernate.LONG)
.addScalar("NAME", Hibernate.STRING)
.addScalar("BIRTHDATE", Hibernate.DATE)

This query specified:

the SQL query string

the columns and types to return

This will return Object arrays, but now it will not use ResultSetMetadata but will instead explicitly get the ID, NAME and BIRTHDATE column as respectively a Long, String and a Short from the underlying resultset. This also means that only these three columns will be returned, even though the query is using * and could return more than the three listed columns.

It is possible to leave out the type information for all or some of the scalars.

sess.createSQLQuery("SELECT * FROM CATS")
.addScalar("ID", Hibernate.LONG)
.addScalar("NAME")
.addScalar("BIRTHDATE")

This is essentially the same query as before, but now ResultSetMetaData is used to determine the type of NAME and BIRTHDATE, where as the type of ID is explicitly specified.

How the java.sql.Types returned from ResultSetMetaData is mapped to Hibernate types is controlled by the Dialect. If a specific type is not mapped, or does not result in the expected type, it is possible to customize it via calls to registerHibernateType in the Dialect.

Entity Queries

The above queries were all about returning scalar values, basically returning the "raw" values from the resultset. The following shows how to get entity objects from a native sql query via addEntity().

sess.createSQLQuery("SELECT * FROM CATS").addEntity(Cat.class);
sess.createSQLQuery("SELECT ID, NAME, BIRTHDATE FROM CATS").addEntity(Cat.class);

This query specified:

the SQL query string

the entity returned by the query

Assuming that Cat is mapped as a class with the columns ID, NAME and BIRTHDATE the above queries will both return a List where each element is a Cat entity.

If the entity is mapped with a many-to-one to another entity it is required to also return this when performing the native query, otherwise a database specific "column not found" error will occur. The additional columns will automatically be returned when using the * notation, but we prefer to be explicit as in the following example for a many-to-one to a Dog:

sess.createSQLQuery("SELECT ID, NAME, BIRTHDATE, DOG_ID FROM CATS").addEntity(Cat.class);

This will allow cat.getDog() to function properly.

Wednesday, July 29, 2009

This is what I am reading now!!


Buying Options
Hibernate: A Developer's Notebook
Print $24.95
Print+Ebook $27.45 Ebook $19.99
(PDF)
Safari Books Online
What is this?




Print £18.99
What is this?

Hibernate: A Developer's Notebook
By James Elliott
May 2004
Pages: 190
Series: Developer's Notebooks
ISBN 10: 0-596-00696-9 | ISBN 13: 9780596006969




Description
Hibernate: A Developer's Notebook shows you how to use Hibernate to automate persistence: you write natural Java objects and some simple configuration files, and Hibernate automates all the interaction between your objects and the database. You don't even need to know the database is there, and you can change from one database to another simply by changing a few statements in a configuration file. If you've needed to add a database backend to your application, don't put it off. It's much more fun than it used to be, and Hibernate: A Developer's Notebook shows you why.
Full Description

Do you enjoy writing software, except for the database code? Hibernate:A Developer's Notebook is for you. Database experts may enjoy fiddling with SQL, but you don't have to--the rest of the application is the fun part. And even database experts dread the tedious plumbing and typographical spaghetti needed to put their SQL into a Java program. Hibernate: A Developers Notebook shows you how to use Hibernate to automate persistence: you write natural Java objects and some simple configuration files, and Hibernate automates all the interaction between your objects and the database. You don't even need to know the database is there, and you can change from one database to another simply by changing a few statements in a configuration file. Hibernate: A Developer's Notebook walks you through the ins and outs of using Hibernate, from installation and configuration, to complex associations and composite types. Two chapters explore ways to write sophisticated queries, which you can express either through a pure Java API, or with an SQL-inspired, but object-oriented, query language. Don't let that intimidate you though: one of the biggest surprises in working with Hibernate is that for many of the common real-world application scenarios, you don't need an explicit query at all. If you've needed to add a database backend to your application, don't put it off. It's much more fun than it used to be, and Hibernate: A Developer's Notebook shows you why. Here's what a few reviewers had to say: "I'm sitting on an airplane after finishing Hibernate: A Developer's Notebook. It's rare to find a book on a new Java technology that you can get through on a domestic flight. That this notebook effectively and succinctly tackles object-relational mapping makes it, and Hibernate, even more impressive. Many books in this category would need to be checked luggage. With this book, you travel first class." --Mike Clark "A simple persistence framework deserves a simple book, and this one delivers. The examples are well described and easy to understand, yet sophisticated enough to demonstrate Hibernate in a real-world context. Jim, I'm a new fan." --Bruce Tate About the new Developer's Notebook Series from O'Reilly: Developer's Notebooks are a new book series covering important new tools for software developers. Developer's Notebooks stress example over explanation and practice over theory. They are about learning by doing; by experimenting with tools and discovering what works. "All lab, no lecture," with a thoughtful lab partner to guide the way.

Hibernate:Here You go with your First App

The first class

Next, we create a class that represents the event we want to store in the database; it is a simple JavaBean class with some properties:

package org.hibernate.tutorial.domain;

import java.util.Date;

public class Event {
private Long id;

private String title;
private Date date;

public Event() {}

public Long getId() {
return id;
}

private void setId(Long id) {
this.id = id;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}
This class uses standard JavaBean naming conventions for property getter and setter methods, as well as private visibility for the fields. Although this is the recommended design, it is not required. Hibernate can also access fields directly, the benefit of accessor methods is robustness for refactoring.

The id property holds a unique identifier value for a particular event. All persistent entity classes (there are less important dependent classes as well) will need such an identifier property if we want to use the full feature set of Hibernate. In fact, most applications, especially web applications, need to distinguish objects by identifier, so you should consider this a feature rather than a limitation. However, we usually do not manipulate the identity of an object, hence the setter method should be private. Only Hibernate will assign identifiers when an object is saved. Hibernate can access public, private, and protected accessor methods, as well as public, private and protected fields directly. The choice is up to you and you can match it to fit your application design.

The no-argument constructor is a requirement for all persistent classes; Hibernate has to create objects for you, using Java Reflection. The constructor can be private, however package or public visibility is required for runtime proxy generation and efficient data retrieval without bytecode instrumentation.

Save this file to the src/main/java/org/hibernate/tutorial/domain directory.

Hibernate:Do it urself

Adapted from :docs.jboss.org

1.1. Part 1 - The first Hibernate Application
For this example, we will set up a small database application that can store events we want to attend and information about the host(s) of these events.
Note
Although you can use whatever database you feel comfortable using, we will use
HSQLDB (an in-memory, Java database) to avoid describing installation/setup of any particular database servers.
1.1.1. Setup
The first thing we need to do is to set up the development environment. We will be using the "standard layout" advocated by alot of build tools such as
Maven. Maven, in particular, has a good resource describing this layout. As this tutorial is to be a web application, we will be creating and making use of src/main/java, src/main/resources and src/main/webapp directories.
We will be using Maven in this tutorial, taking advantage of its transitive dependency management capabilities as well as the ability of many IDEs to automatically set up a project for us based on the maven descriptor. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.hibernate.tutorials
hibernate-tutorial
1.0.0-SNAPSHOT
First Hibernate Tutorial


${artifactId}



org.hibernate
hibernate-core



javax.servlet
servlet-api



org.slf4j
slf4j-simple



javassist
javassist



Tip
It is not a requirement to use Maven. If you wish to use something else to build this tutoial (such as Ant), the layout will remain the same. The only change is that you will need to manually account for all the needed dependencies. If you use something like Ivy providing transitive dependency management you would still use the dependencies mentioned below. Otherwise, you'd need to grab all dependencies, both explicit and transitive, and add them to the project's classpath. If working from the Hibernate distribution bundle, this would mean hibernate3.jar, all artifacts in the lib/required directory and all files from either the lib/bytecode/cglib or lib/bytecode/javassist directory; additionally you will need both the servlet-api jar and one of the slf4j logging backends.
Save this file as pom.xml in the project root directory.