Friday, May 11, 2012

Why do we use hibernate annotation?

It is not something important as in "mandatory". It's a different possibility, with strength and weaknesses.
Advantages:
  • Compile-time checking : writing in Java (instead of Xml) is very user-friendly in the IDE nowadays. No more typos discovered when starting your application (incremental compilation), not that much to remember (completion)...
  • Localized with the code (class level) : instead of having to open two files (java and xml) to get the full story, with one annotated java file, you open only one file. This is less repetitive, faster in the long run.
  • Localized with the code (method or field level) : because the annotation go on a method (or field), there is no need to specify the method it belongs to. That redondant information is not given, which is shorter, and always coherent (even after a code refactoring for example). Maintenance is so much faster.
  • Tools (javadoc, other tools using reflection) can use the annotations for some other requirements.
  • Annotations are newer than the xml, the team used the input they had received at the time to provide better default values. Xml has some, but can't change much for compatibility reasons. Often, with the annotations technology, you write no annotation at all, and it works. Imagine the time-saving, especially during development.
http://stackoverflow.com/questions/1367187/why-do-we-use-hibernate-annotation

Thursday, May 10, 2012

Authentication and Authorization

Authentication
An authentication system is how you identify yourself to the computer. The goal behind an authentication system is to verify that the user is actually who they say they are.
There are many ways of authenticating a user. Any combination of the following are good examples.

Password based authentication
Requires the user to know some predetermined quantity (their password).


  • Advantages: Easy to impliemnt, requires no special equipemnt.
  • Disadvantages: Easy to forget password. User can tell another user their password. Password can be written down. Password can be reused.
  • Device based authentication
    Requires the user to posses some item such as a key, mag strip, card, s/key device, etc.


  • Advantages: Difficult to copy. Cannot forget password. If used with a PIN is near useless if stolen.
  • Disadvantages: Must have device to use service so the user might forget it at home. Easy target for theft. Still doesn't actually actively identify the user.

  • Biometric Authentication


  • My voice is my passport. Verify me. This is from the movie sneakers and demonstrates one type of biometric authentication device. It identifies some physical charactistic of the user that cannot be seperated from their body.
    Retina Scanners:


  • Advantages: Accurately identifies the user when it works.
  • Disadvantages: New technology that is still evolving. Not perfect yet.

  • Hand Scanners:


  • Advantages: Difficult to seperate from the user. Accurately identifies the user.
  • Disadvantages: Getting your hand stolen to break into a vault sucks a lot more than getting your ID card stolen.
  • Authorization
    Once the system knows who the user is through authentication, authorization is how the system decides what the user can do.
    A good example of this is using group permissions or the difference between a normal user and the superuser on a unix system.
    There are other more compicated ACL (Access Control Lists) available to decide what a user can do and how they can do it. Most unix systems don't impliment this very well (if at all.)

    http://www.acm.uiuc.edu/workshops/security/auth.html

    Error: no 'server' JVM at '...'

    Some workstation or server will not come with server JVM after you install JDK on it. When you run server JVM by 'java -server MyApp', you will get the error such as:
    Error: no `server' JVM at `C:\Program Files\Java\jre1.6.0\bin\server\jvm.dll'.
    There is a little trick to get the server JVM up and running:
    1. Copy 'server' folder from the JDK's JRE's bin folder (example: C:\Program Files\Java\jdk1.6.0\jre\bin\server)
    2. Paste the 'server' folder to JRE's bin folder(example: C:\Program Files\Java\jre1.6.0\bin)
    3. Done
    http://thiamteck.blogspot.com/2007/10/error-no-server-jvm-at.html

    Tuesday, May 8, 2012

    How does Java hashmap work?


    A hashmap works like this (this is a little bit simplified, but it illustrates the basic mechanism):
    It has a number of "buckets" which it uses to store key-value pairs in. Each bucket has a unique number - that's what identifies the bucket. When you put a key-value pair into the map, the hashmap will look at the hash code of the key, and store the pair in the bucket of which the identifier is the hash code of the key. For example: The hash code of the key is 235 -> the pair is stored in bucket number 235. (Note that one bucket can store more then one key-value pair).
    When you lookup a value in the hashmap, by giving it a key, it will first look at the hash code of the key that you gave. The hashmap will then look into the corresponding bucket, and then it will compare the key that you gave with the keys of all pairs in the bucket, by comparing them with equals().
    Now you can see how this is very efficient for looking up key-value pairs in a map: by the hash code of the key the hashmap immediately knows in which bucket to look, so that it only has to test against what's in that bucket.
    Looking at the above mechanism, you can also see what requirements are necessary on thehashCode() and equals() methods of keys:
    • If two keys are the same (equals() returns true when you compare them), their hashCode()method must return the same number. If keys violate this, then keys that are equal might be stored in different buckets, and the hashmap would not be able to find key-value pairs (because it's going to look in the same bucket).
    • If two keys are different, then it doesn't matter if their hash codes are the same or not. They will be stored in the same bucket, but the hashmap will use equals() to tell them apart.

    http://stackoverflow.com/questions/6493605/how-does-java-hashmap-work

    How HashMap works in Java

    HashMap works on principle of hashing, we have put () and get () method for storing and retrieving object form hashMap.When we pass an both key and value to put() method to store on HashMap, it uses key object hashcode() method to calculate hashcode and they by applying hashing on that hashcode it identifies bucket location for storing value object.
    While retrieving it uses key object equals method to find out correct key value pair and return value object associated with that key. HashMap uses linked list in case of collision and object will be stored in next node of linked list.
    Also hashMap stores both key+value tuple in every node of linked list.

    What will happen if two different HashMap key objects have same hashcode?
    They will be stored in same bucket but no next node of linked list. And keys equals () method will be used to identify correct key value pair in HashMap.

    Read more: http://javarevisited.blogspot.com/2011/02/how-hashmap-works-in-java.html#ixzz1uJEyIZJ7

    Thursday, April 12, 2012

    Joining Three or More Tables

    Although each join specification joins only two tables, FROM clauses can contain multiple join specifications. This allows many tables to be joined for a single query.
    The titleauthor table of the pubs database offers a good example of a situation in which joining more than two tables is helpful. This Transact-SQL query finds the titles of all books of a particular type and the names of their authors:
    USE pubs
    SELECT a.au_lname, a.au_fname, t.title
    FROM authors a INNER JOIN titleauthor ta
       ON a.au_id = ta.au_id JOIN titles t
       ON ta.title_id = t.title_id
    WHERE t.type = 'trad_cook'
    ORDER BY t.title ASC
    
    Here is the result set:
    au_lname          au_fname             title      
    ----------------- -------------------- ---------- 
    Blotchet-Halls    Reginald             Fifty Years in Buckingham Palace 
       Kitchens
    Panteley          Sylvia               Onions, Leeks, and Garlic: 
       Cooking Secrets of the Mediterranean
    O'Leary           Michael              Sushi, Anyone?
    Gringlesby        Burt                 Sushi, Anyone?
    Yokomoto          Akiko                Sushi, Anyone?
    
    (5 row(s) affected)
    
    Notice that one of the tables in the FROM clause, titleauthor, does not contribute any columns to the results. Also, none of the joined columns, au_id and title_id, appear in the results. Nonetheless, this join is possible only by using titleauthor as an intermediate table.
    The middle table of the join (the titleauthor table) can be called the translation table or intermediate table, because titleauthor is an intermediate point of connection between the other tables involved in the join.
    When there is more than one join operator in the same statement, either to join more than two tables or to join more than two pairs of columns, the join expressions can be connected with AND or with OR.

    http://msdn.microsoft.com/en-us/library/aa213227%28v=sql.80%29.aspx

    Tuesday, April 3, 2012

    What's the difference between a URI and a URL?


    From RFC 3986:
    A URI can be further classified as a locator, a name, or both. The term "Uniform Resource Locator" (URL) refers to the subset of URIs that, in addition to identifying a resource, provide a means of locating the resource by describing its primary access mechanism (e.g., its network "location"). The term "Uniform Resource Name" (URN) has been used historically to refer to both URIs under the "urn" scheme [RFC2141], which are required to remain globally unique and persistent even when the resource ceases to exist or becomes unavailable, and to any other URI with the properties of a name.
    So any URL is a URI, but some URIs aren't URLs, they're URNs instead. Except the ones which are both URNs and URLs.
    Clear as mud?
    EDIT: Note also Roger Pate's clearer answer. (I can't delete this answer as it's accepted, nor do I think it's worth just copying his text into here, but please read it...)