Create your own Java customized annotations

Annotations is a relatively new technology, it was added to the language in Java5. An annotation is a meta data that can be attached to a java class, member, function or a parameter. It gives information about the element that is annotated. An example is the Deprecated annotation that specifies this function is old and better not used. Java5 comes with a set of annotations but you can also can create your own. In this post I will show you how to create your own annotations, use them and also refer to them during runtime. Steps for creating customized annotations: 1. Creating the annotation interface First you create the annotation interface which has specific structure. In this example the name of the annotation is ‘InvokeMultiple’ and it has 1 field ‘numberOfTimesToInvoke’. Meta-Annotations Meta annotations are actually annotations being used by annotations. @Target – specifies the type pf element this annotation is attached to. ElementType.TYPE-can be applied to any element of a class ElementType.FIELD-can be applied to a field or property ElementType.METHOD-can be applied to a method level annotation ElementType.PARAMETER-can be applied to the parameters of a method ElementType.CONSTRUCTOR-can be applied to constructors ElementType.LOCAL_VARIABLE-can be applied to local variables ElementType.ANNOTATION_TYPE-indicates that Continue reading Create your own Java customized annotations

Hibernate, lesson2 – Queries

In the last post I have showed you a very basic example of usage in Hibernate. In this post I would like to show you all the different ways of executing a query while using Hibernate. The examples in this post use the same example from the last post. 1. Using the session functions The session object itself gives you methods to persist objects to the DB. For example: This code will fetch a row from the DB with primary key=111 and convert it to Contact object. Which is the equivalent to this SQL code: Another example: This code will insert a new row to the table. In other words this is the equivalent to this SQL code: Another example: This code will update a row in the table where the primary key is 111. In other words this is the equivalent to this SQL code: But as you probably noticed, the possibilities here are vary narrow. But there are other and better ways creating queries in Hibernate. 2. Using a Query object with HQL HQL is a query language designed by Hibernate It resembles SQL but has more options to it. Example: This will return a single result. That Continue reading Hibernate, lesson2 – Queries

Hibernate, lesson 1 – first encounter

Hibernate is one of the most popular java technologies there is and it seems it is not going anywhere for at least 10 years. If you are a java developer you can’t afford not to know it, at least on the surface. This post is for those of you who never used hibernate, or used it and just want to start from scratch. If hibernate seems like a tech-monster for you, you’ll come to the right place. I’ll show you how to get hibernate to work in a very short time. What do you need? DB connection Basic SQL knowledge Java knowledge + IDE Step 1 – Download Hibernate Download Hibernate Core¬† zip file form Unzip it. The most impotent file is hibernate3.jar. Locate it under the root. Other important jars can be found under the lib folder Step 2 – Start a new java project with your IDE Add the hibernate3.jar to the classpath. Also add these jars: dom4j, log4j, slf4j-api,¬† slf4j-jcl, commons-logging, commons-collection, javassist, jta, antlr (most of them come with hibernate) Add also the driver jar + license to the classpath. Step 3 – Create a java bean A bean is a java class with a Continue reading Hibernate, lesson 1 – first encounter

Handeling session cookies with Java

Let’s say you are creating an HTTP request using Java. You are probably doing it with the use of the URL object like this: But sometimes you will also want to use cookies to send along with you request. To do so first we will learn how to read cookies from the request. Getting the cookies from a URL connection After the function uc.connect(); you are able to get cookies from your request. Here is the result of this example: PREF=ID=f0fd8b4c16931772:TM=1261387922:LM=1261387922:S=LLhLkHSpgfXkqxIe; expires=Wed, 21-Dec-2011 09:32:02 GMT; path=/; NID=30=C6Hb5SV2-pqYymnnrZmTAprCvDMrFwo6wzg9We7rvPztXmA_fTWxv-GPmDVXPsIBOOkecj1Ms4skmTWT-uUN5iW_-ZYiPUNNJXE8CC44xWVORe8Yyu1jI32XL51tvzCz; expires=Tue, 22-Jun-2010 09:32:02 GMT; path=/;; HttpOnly Sending HTTP request with cookies: Notice that the value of the property ‘Cookie’ is a string which contains cookies in the format of cookieName=CookieValue with a ‘;’ as a delimiter. Notice you have to set the cookie value before the connect() function. download source