Wednesday, November 7, 2012

JPA vs Hibernate


Many enterprise Java developers use lightweight persistent objects provided by open-source frameworks or Data Access Objects instead of entity beans: entity beans and enterprise beans had a reputation of being too heavyweight and complicated, and one could only use them in Java EE application servers. Many of the features of the third-party persistence frameworks were incorporated into the Java Persistence API, and as of 2006 projects like Hibernate (version 3.2) and Open-Source Version TopLink Essentials have become implementations of the Java Persistence API.

JPA is really a specification
Hibernate provides an implementation of the JPA specification. Vendors providing EJB3.0 containers will also be providing an implementation of the JPA spec, so that means Sun and IBM WebSphere and Oracle and all the other handsome players in the industry will provide an implementatio
http://www.coderanch.com/t/218819/ORM/databases/Hibernate-vs-JPA


What is JPA?

JPA is a framework for managing relational data for Java. It can be used with applications utilizing JSE (Java Platform, Standard Edition) or JEE (Java Platform, Enterprise Edition). Its current version is JPA 2.0, which was released on 10 Dec, 2009. JPA replaced EJB 2.0 and EJB 1.1 entity beans (which were heavily criticized for being heavyweight by the Java developer community). Although entity beans (in EJB) provided persistence objects, many developers were used to utilizing relatively lightweight objects offered by DAO (Data Access Objects) and other similar frameworks instead. As a result, JPA was introduced, and it captured many of the neat features of the frameworks mentioned above


What is Hibernate?

Hibernate is a framework that can be used for object-relational mapping intended for Java programming language. More specifically, it is an ORM (object-relational mapping) library that can be used to map object-relational model in to conventional relational model. In simple terms, it creates a mapping between Java classes and tables in relational databases, also between Java to SQL data types. Hibernate can also be used for data querying and retrieving by generating SQL calls. Therefore, the programmer is relieved from the manual handling of result sets and converting objects. Hibernate is released as a free and open source framework distributed under GNU license. An implementation for JPA API is provided in Hibernate 3.2 and later versions.



What is the difference between JPA and Hibernate?

JPA is a framework for managing relational data in Java applications, while Hibernate is a specific implementation of JPA (so ideally, JPA and Hibernate cannot be directly compared). In other words, Hibernate is one of the most popular frameworks that implements JPA. Hibernate implements JPA through Hibernate Annotation and EntityManager libraries that are implemented on top of Hibernate Core libraries. Both EntityManager and Annotations follow the lifecycle of Hibernate. The newest JPA version (JPA 2.0) is fully supported by Hibernate 3.5. JPA has the benefit of having an interface that is standardized, so the developer community will be more familiar with it than Hibernate. On the other hand, native Hibernate APIs can be considered more powerful because its features are a superset of that of JPA.

http://www.differencebetween.com/difference-between-jpa-and-vs-hibernate/#ixzz2BXu2zE00

Statement vs Prepared Statement



  • Statement vs Prepared Statement 


A prepared statement performs the following checks:

    Makes sure that the tables and columns exist
    Makes sure that the parameter types match their columns
    Parses the SQL to make sure that the syntax is correct
    Compiles and caches the compiled SQL so it can be re-executed without repeating these steps


http://stackoverflow.com/questions/8959036/statement-vs-prepared-statement-in-terms-of-precompilation



  • The prepared statement concept is not specific to Java, it is a database concept. Statement precompiling means: when you execute a SQL query, database server will prepare a execution plan before executing the actual query, this execution plan will be cached at database server for further execution.


The advantages of Prepared Statements are:

    As the execution plan get cached, performance will be better.
    It is a good way to code against SQL Injection as escapes the input values.
    When it comes to a Statement with no unbound variables, the database is free to optimize to its full extent. The individual query will be faster, but the down side is that you need to do the database compilation all the time, and this is worse than the benefit of the faster query.

http://webmoli.com/2008/10/23/back-to-basics-statement-vs-prepared-statement/

ls command howto


Output from ls -la Command

picard@spnode15$ ls -la

total 1600
drwx---s-x  11 picard   STAFF       1536 Jun 26 14:49 .
dr-xr-sr-x1300 bin      bin        20480 Jun 26 12:06 ..
-rw-------   1 picard   STAFF        948 Jun 06 09:46 .addressbook
-rw-------   1 picard   STAFF       3368 Jun 06 09:46 .addressbook.lu
-rw-------   1 picard   STAFF        193 Apr 02 10:06 .article
-rw-------   1 picard   STAFF       1035 May 20 12:30 .bash_history
drwx---S--   2 picard   STAFF        512 Jun 23 13:56 .mailpgp
-rw-------   1 picard   STAFF     128654 Jun 10 19:19 .newsrc
drwx------   4 picard   STAFF        512 May 29 07:01 .pgp
-rw-------   1 picard   STAFF      10196 Jun 26 14:33 .pinerc
-rwxr-xr-x   1 picard   STAFF       1047 May 27 14:15 .plan
-rw-------   1 picard   STAFF         35 Jun 17 09:23 .profile
-rw-------   1 picard   STAFF        371 Sep 08 1995  .signature
-rw-------   1 picard   STAFF      81691 Jun 20 10:34 3dtree.jpg
-rw-------   1 picard   STAFF      31156 Jan 03 10:19 HTMLBgnrGuide.txt
drwx---s-x   2 picard   STAFF        512 Apr 01 13:26 News
-rw-------   1 picard   STAFF      11760 Jul 23 1995  TUTORIAL
-rw-------   1 picard   STAFF        234 Feb 02 08:18 baen.txt
drwx---s-x   2 picard   STAFF        512 Mar 12 06:57 bin
-rw-------   1 picard   STAFF         71 Jul 31 1995  calendar
-rw-------   1 picard   STAFF     338912 May 02 1995  command.memos
-rw-------   1 picard   STAFF        747 Jun 24 13:12 dead.letter
-rw-------   1 picard   STAFF      10506 Jun 01 12:42 info.listserv
-rw-------   1 picard   STAFF     698675 Nov 01 1995  jim.kirk.letters
-rw-------   1 picard   STAFF        122 Jun 24 13:28 junk
drwx------   2 picard   STAFF       1536 Jun 25 12:40 mail
-rw-r-----   1 picard   STAFF       1397 May 28 12:50 mj.ultra
drwx---s-x   2 picard   STAFF        512 May 26 21:09 pine
-rw-------   1 picard   STAFF       1716 Jul 23 1995  print.txt
drwxr-sr-x   6 picard   STAFF       1024 Mar 27 10:54 public_html
drwx---s-x   3 picard   STAFF        512 Mar 31 07:24 rexx
drwx---s-x   2 picard   STAFF        512 Aug 08 1995  temp
-rwx--x--x   1 picard   STAFF        368 May 28 14:10 unpgp

picard@spnode15$

Let's look at this listing, and see what it tells us. At first glance, the left-most column looks like utter nonsense; drwxr--blah-blah-blah. What's going on here?

File Type. The first position/character in this column describes what type of entry each horizontal line represents. The first character will usually be either a - or a d. Symbols in the first (left most) position on the line represent:

-

    = Regular File
d

    = Directory

You can tell, just by looking at the output from ls -l which entries are files and which are directories.

Permissions. The rest of the first column (after the - or d) indicates access permissions which have been set either explicitly, by you, using the chmod command (which we'll look at later) or automatically, by the system, when the file (or directory) was created. We'll discuss this part of the listing in more detail when we get to the chmod command, which deals with setting file access permissions.

Directory Entries (and Hard-Link Count). The next column (immediately to the right of the access permissions) is a number which tells how many directory entries are under that item. For a regular file, this will typically be 1. For a directory, this will always be at least 2. The reason for this is that every directory always contains pointers to both itself, and its parent directory. You can see these two entries as the first two items in our example listing in Figure 12: the entries for . and .. (i.e. one period, and two periods). The single period . ), is the pointer to the current directory--the directory that you are "in" right now. Two periods .. ), point to the parent directory--the directory which contains the current directory. You can use these as convenient nicknames/shortcuts in some commands, when you want to describe a relative pathname.

Owner. The next column to the right displays the userid of the owner of this file or directory. In our listing, most of the files are owned by our hypothetical user, picard. When you create files and directories, you will be their owner, and your userid will show here, in place of picard.

Group. The next column (mostly "STAFF" in our example) shows the name of the user group which is connected with this entry. Each userid is a member of one or more groups, and access permissions may be set which determine what sort of access other members of the same group have to the file or directory named. Again, more on that when we get to the chmod command.

Size. The next column shows the file size in bytes (characters).

Date/Time. The next 3 columns show the date and time the file was last modified.

File Name. Finally, we have the file name. As discussed earlier, UNIX is very accommodating with regard to the length of file names. However there are some qualifications: File names cannot (usually) contain blank spaces, or the characters /, *, or ?. This is because / is used to separate levels of a pathname, * is a "wildcard" character which is expanded by the system to mean "any number of any characters," and ? is a wildcard representing "any single character." MS-DOS users in particular should recognize these "wildcard" characters.


http://docweb.cns.ufl.edu/docs/d0107/ar07s04.html



  • Link counter


Most file systems that support hard links use reference counting. An integer value is stored with each physical data section. This integer represents the total number of links that have been created to point to the data. When a new link is created, this value is increased by one. When a link is removed, the value is decreased by one. If the link count becomes zero, the operating system usually automatically deallocates the data space of the file if no process has the file opened for access. The maintenance of this value assists users in preventing data loss. This is a simple method for the file system to track the use of a given area of storage, as zero values indicate free space and nonzero values indicate used space.

On POSIX-compliant operating systems, such as many Unix-variants, the reference count for a file or directory is returned by the stat() or fstat() system calls in the st_nlink field of struct stat.



  • Link count: File vs Directory 

One of the results of the ls -l command  is the link count.

1. What is the link count of a file?
The link count of a file tells the total number of links a file has.the number of hard-links a file has.
The soft-link is not part of the link count since the soft-link's inode number is different from the original file.

2. How to find the link count of a file or directory?
any new file created will have a link count 1.
By default, a file will have a link count of 1

$ touch  test.c
$ ln test.c test-hardlink.c
ls -lai
total 8
3145743 drwxrwxr-x 2 vagrant vagrant 4096 Mar 31 11:44 .
3145730 drwxr-xr-x 6 vagrant vagrant 4096 Mar 31 11:44 ..
3145744 -rw-rw-r-- 2 vagrant vagrant    0 Mar 31 11:44 test.c
3145744 -rw-rw-r-- 2 vagrant vagrant    0 Mar 31 11:44 test-hardlink.c

$ ln -s test.c test-softlink.c
ls -lai
total 8
3145743 drwxrwxr-x 2 vagrant vagrant 4096 Mar 31 11:45 .
3145730 drwxr-xr-x 6 vagrant vagrant 4096 Mar 31 11:44 ..
3145744 -rw-rw-r-- 2 vagrant vagrant    0 Mar 31 11:44 test.c
3145744 -rw-rw-r-- 2 vagrant vagrant    0 Mar 31 11:44 test-hardlink.c
3145745 lrwxrwxrwx 1 vagrant vagrant    6 Mar 31 11:45 test-softlink.c -> test.c

3. Does the link count decrease whenever the hard-link is deleted?
When the hard link file is moved or deleted, the link count of the original file gets reduced.

rm test-hardlink.c
ls -lai
total 8
3145743 drwxrwxr-x 2 vagrant vagrant 4096 Mar 31 11:47 .
3145730 drwxr-xr-x 6 vagrant vagrant 4096 Mar 31 11:44 ..
3145744 -rw-rw-r-- 1 vagrant vagrant    0 Mar 31 11:44 test.c
3145745 lrwxrwxrwx 1 vagrant vagrant    6 Mar 31 11:45 test-softlink.c -> test.c

4. When does the link count of a directory change?
A directory "xyz" is created and the default link count of any directory is 2. The extra count is because for every directory created, a link gets created in the parent directory to point to this new directory.
link count of a directory minus 2 gives you the total number of sub-directories present in the directory.

mkdir xyz
ls -ld xyz/
drwxrwxr-x 2 vagrant vagrant 4096 Mar 31 11:50 xyz/
mkdir -p xyz/abc
mkdir -p xyz/efg
ls -ldia xyz
3145746 drwxrwxr-x 4 vagrant vagrant 4096 Mar 31 11:51 xyz

http://www.theunixschool.com/2012/10/link-count-file-vs-directory.html

  • Q1: The Unix inode structure contains a reference count. What is the reference count for? Why can't we just remove the inode without checking the reference count when a file is deleted?


    Inodes contain a reference count due to hard links. The reference count is equal to the number of directory entries that reference the inode. For hard-linked files, multiple directory entries reference a single inode. The inode must not be removed until no directory entries are left (ie, the reference count is 0) to ensure that the filesystem remains consistent.


  • On a storage device, a file or directory is contained in a collection of blocks

Information about a file is contained in an inode, which records information such as the owner, when the file was last accessed, how large it is, whether it is a directory or not, and who can read from or write to it. The inode number is also known as the file serial number and is unique within a particular filesystem
https://developer.ibm.com/tutorials/l-lpic1-104-6/