Q: What is Hibernate?
A: Hibernate is a java-based Object/relational mapping(ORM) tool.
Q: What are ORM tools?
A: ORM tools provide automated solutions for the Object/relational paradigm mismatch problem, using metadata that describes the mapping between the objects and the database.
Q: What is Object/relational paradigm mismatch?
A: Object-Oriented paradigm is based on software engineering principles, whereas relational paradigm on mathematical principles. Object-oriented technology supports the building of applications out of networks of objects with both data and behavior. Relational technology supports the storage of data in tables and manipulation of that data using data manipulation language (DML). Because the underlying paradigms are different the two technologies do not work seamlessly, hence the name Object/relational paradigm mismatch.
Q: What role does the Session interface play in Hibernate?
A: The Session is a persistence manager that manages operation like storing and retrieving objects. Instances of Session are inexpensive to create and destroy. They are not threadsafe.
Q: What is SessionFactory interface?
A: The application obtains Session instances from a SessionFactory. SessionFactory instances are not lightweight and typically one instance is created for the whole application. If the application accesses multiple databases, it needs one per database
Q: What is Configuration interface?
A: The application uses a Configuration instance to specify the location of mapping documents and Hibernate-specific properties and then creates the SessionFactory.
Q: What is the naming convention for Hibernate XML mapping file extensions?
A: .hbm.xml
Q: What are the most common methods of configuring Hibernate?
A: 1. By placing hibernate.properties file in the classpath.
2. Including
Q: How can the mapping files be configured in Hibernate?
A: 1. Mapping files can be added to Configuration in the application code or,
2. They can be configured in hibernate.cfg.xml using the
Q: What happens when both hibernate.properties and hibernate.cfg.xml are in the classpath?
A: The settings of the XML configuration file will override the settings used in the properties.
Q: Since SessionFactory instances are not lightweight, where is the single created instance placed in J2EE environments?
A: Usually it is bound to JNDI, it will bind itself automatically if hibernate.session_factory_name is set to the name of directory node.
Q: How to set Hibernate to log all generated SQL to the console?
A: By setting the hibernate.show_sql property to true.
Q: In hibernate, what interfaces/classes must the persistent classes (classes that are mapped to database tables) implement/extend?
A: NONE, they can be regular POJOs.
Q: Does hibernate require persistent classes to implement Serializable?
A: Hibernate doesn't require that persistent classes implement Serializable. However, when objects are stored in an HttpSession or passed by value using RMI, serialization is necessary.
Q: What methods must the persistent classes implement in Hibernate?
A: Since Hibernate instantiates persistent classes using Constructor.newInstance(), it requires a constructor with no arguments for every persistent class. And getter and setter methods for all the instance variables.
Q: How can Hibernate be configured to access a instance variable directly and not through a setter method?
A: By mapping the property with access="field" in Hibernate metadata. This forces hibernate to bypass the setter method and access the instance variable directly while initializing a newly loaded object.
Q: What is dirty checking in Hibernate?
A: Hibernate automatically detects object state changes in order to synchronize the updated state with the database, this is called dirty checking. An important note here is, Hibernate will compare objects by value, except for Collections, which are compared by identity. For this reason you should return exactly the same collection instance as Hibernate passed to the setter method to prevent unnecessary database updates.
Q: What is the root level element in a hibernate mapping file?
A:
Q: Is it possible to declare mappings for multiple classes in one mapping file?
A: Yes, by using multiple
Q: How are the individual properties mapped to different table columns?
A: By using multiple
Q: What are derived properties?
A: The properties that are not mapped to a column, but calculated at runtime by evaluation of an expression are called derived properties. The expression can be defined using the formula attribute of the
Q: How can you make a property be read from the database but not modified in anyway (make it immutable)?
A: By using the insert="false" and update="false" attributes.
Q: How can a whole class be mapped as immutable?
A: By using the mutable="false" attribute in the class mapping.
Q: What is the use of dynamic-insert and dynamic-update attributes in a class mapping?
A: They tell hibernate whether to include unmodified properties in SQL INSERT and SQL UPDATE.
Q: How do you achieve table-per-class hierarchy while mapping classes in Hibernate?
A: By using several
Q: How do you achieve table-per-subclass while mapping classes in Hibernate?
A: By using
Q: Does hibernate allow mixing table-per-class hierarchy and table-per-subclass strategies?
A: No, you cannot have a
*******************************************
Essential Topics for Java Developers
Below is a list of concepts and technologies that every Java Web Developer must be familiar with. I update this list very often and provide you with links and references.
***************************************
Sometimes we would like to have the user not see some of the information in the url, especially while sending data using the GET method. For example if you are doing in a jsp, something like
Then the users browser will be ponting to something like:
http://localhost/urlencryption/jsp/process.jsp?id=101&item=22&conf=12234
here users will be able to see the "id", "item" and "conf" details clearly in the URL. What if some of this information is sequential in the database, some curious users or some hackers can modify this data and might access sensitive data which they are not supposed to see. In such cases it would be helpfull to encrypt the parameters in the query string. Now lets modify the jsp as below:
The helper class code is:
import java.io.IOException;import java.io.UnsupportedEncodingException;import java.net.URLDecoder;import java.net.URLEncoder;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import java.security.spec.InvalidKeySpecException;import java.security.spec.KeySpec;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.NoSuchPaddingException;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;public class EncryptionHelper { private static final String PASS_PHRASE = "passphrase"; public static String encrypt(String msg){ try { KeySpec keySpec = new DESKeySpec(PASS_PHRASE.getBytes()); SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(keySpec); Cipher ecipher = Cipher.getInstance(key.getAlgorithm()); ecipher.init(Cipher.ENCRYPT_MODE, key); //Encode the string into bytes using utf-8 byte[] utf8 = msg.getBytes("UTF8"); //Encrypt byte[] enc = ecipher.doFinal(utf8); //Encode bytes to base64 to get a string return new sun.misc.BASE64Encoder().encode(enc); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } public static String decrypt(String msg){ try { KeySpec keySpec = new DESKeySpec(PASS_PHRASE.getBytes()); SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(keySpec); Cipher decipher = Cipher.getInstance(key.getAlgorithm()); decipher.init(Cipher.DECRYPT_MODE, key); // Decode base64 to get bytes byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(msg); //Decrypt byte[] utf8 = decipher.doFinal(dec); //Decode using utf-8 return new String(utf8, "UTF8"); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return null; } public static String encodeURL(String url){ try { return URLEncoder.encode(url,"UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; }}
now the user's browser will show some obscured data like:
http://localhost/urlencryption/jsp/process.jsp?data=6ITbVZYPZNWDf%2F3J0lGo%2FJYXYr%2BdhKMHAcqjmqtfJ%2BA%3D
Now in the process.jsp we can get the original parameters by decoding, decrypting and parsing the decrypted string in the data parameter. The jsp decrypting the contents of data parameter will look something like:
Similarly, if the encrypted data can be decrypted and accessed in a servlet. A sample implementation of the doGet() method is shown below:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data = request.getParameter("data"); System.out.println("data: "+data); String decodedData = URLDecoder.decode(data); System.out.println("decodedData: "+decodedData); System.out.println("Decrypted data: "+EncryptionHelper.decrypt(data)); }
*************************************