Sunday, February 21, 2010

Hibernate proxy

Hibernate proxy

Proxies are created dynamically by subclassing your object at runtime. The subclass has all the methods of the parent, and when any of the methods are accessed, the proxy loads up the real object from the DB and calls the method for you. Very nice in simple cases with no object hierarchy. Typecasting and instanceof work perfectly on the proxy in this case since it is a direct subclass. By default Hibernate creates a proxy for each of the class you map in mapping file. This class contain the code to invoke JDBC. This class is created by hibernate using CGLIB.


Proxies are the mechanism that allows Hibernate to break up the interconnected cloud of objects in the database into smaller chunks, that can easily fit in memory. Proxies are created dynamically by subclassing your object at runtime. The subclass has all the methods of the parent, and when any of the methods are accessed, the proxy loads up the real object from the DB and calls the method for you.


A class can be mapped to a proxy instead to a table. When you actually call load on session it returns you proxy. This proxy may contain actual method to load the data. Object proxies can be defined in one of two ways. First, you can add a proxy attribute to the class element. You can either specify a different class or use the persistent class as the proxy.


For example:

<class name="Loc"
proxy="com.ch01.Loc">

lt;/class>

The second method is to use the lazy attribute. Setting lazy="true"is a shorthand way of defining the persistent class as the proxy.

Read more...

Sunday, February 7, 2010

JSP Program to Delete a Cookie Values

JSP is a dynamic scripting capability for web pages that allows Java as well as a few special tags to be embedded into a web file (HTML/XML, etc). The suffix traditionally ends with .jsp to indicate to the web server that the file is a JSP files. JSP is a server side technology - you can’t do any client side validation with it. The JSP assists in making the HTML more functional. Servlets on the other hand allow outputting of HTML but it is a tedious process. It is easy to make a change and then let the JSP capability of the web server you are using deal with
compiling it into a servlet and running it.

Sample JSP Program to Delete a Cookie Value:

<%
Cookie killCookie = new Cookie(“Ck”, null);

KillCookie.setPath(“/”);

killCookie.setMaxAge(0);

response.addCookie(killCookie);

%>
JSP pages are focused around HTML (or XML) with Java codes and JSP tags inside them. When a web server that has JSP support is asked for a JSP page, it checks to see if it has already compiled the page into a servlet. Thus, JSP pages become servlets and are transformed into pure Java and then compiled, loaded into the server and executed.

Read more...

Thursday, February 4, 2010

STRING OBJECTS in JAVA

The Java String class (java.lang.String) is a class of object that represents a character array of arbitrary length. While this external class can be used to handle string objects, Java integrates internal, built-in strings into the language.

An important attribute of the String class is that once a string object is constructed, its value cannot change (note that it is the value of an object that cannot change, not that of a string variable, which is just a reference to a string object). All String data members are private, and no string method modifies the string’s value.


CONVERTING OBJECTS TO STRINGS


The String + operator accepts a non-string operand, provided the other operand is a string. The action of the + operator on non-string operands is to convert the non-string to a string, then to do the concatenation. Operands of native types are converted to string by formatting their values. Operands of class types are converted to a string by the method toString()

that is defined for all classes. Any object or value can be converted to a string by explicitly using one of the static valueOf() methods defined in class String:

String str = String.valueOf (obj);
If the argument to valueOf() is of class type, then valueOf() calls that object’s toString() method. Any class can define its own toString() method, or just rely on the default. The output produced by toString() is suitable for debugging and diagnostics. It is not meant to be an elaborate text representation of the object, nor is it meant to be parsed. These conversion rules also apply to the right-hand side of the String += operator.

CONVERTING STRINGS TO NUMBERS


Methods from the various wrapper classes, such as Integer and Double, can be used to convert numerical strings to numbers. This is often necessary for command line arguments where the parameter list is available to the
main () method as a string array. The wrapper classes contain static methods ( such as parseInt() ) which convert a string to its own internal data type.

These can be used with class names rather than creating a separate object. Be aware that these particular conversion methods throw a NumberFormatException and must be used in the context of a try and catch block combination.

Read more...
Blog Widget by LinkWithin

JS-Kit Comments

  © Blogger template Newspaper III by Ourblogtemplates.com 2008

Back to TOP