E4X With Prototype.js

<html>
<head>
<!– linking prototype.js to this html page –>
<script type=”text/javascript” src=”http://script.aculo.us/prototype.js”> </script>

<!–  e4x=1 should be defined like that –>
<script type=”text/javascript ; e4x=1″>

<!– This js function is called if “click me”  on the main web page is clicked–>
function loadXML () {
var xmlMusic=new XML() ;

<!– xml definition–>
xmlMusic =
<mp3>
<music genre=”classical”>
<artist>Ludwig van Beethoven</artist>
<song>Fifth Symphony</song>
</music>
<music genre=”jazz”>
<artist>Grover Washington, Jr.</artist>
<song>The Best Is Yet to Come</song>
</music>
<music genre=”classical”>
<artist>Johann Sebastian Bach</artist>
<song>Double Concerto in D- for Two Violins</song>
</music>
<music genre=”jazz”>
<artist>Dave Brubeck</artist>
<song>Take Five</song>
<song>Truth Is Fallen</song>
</music>
<music genre=”classical”>
<artist>Johannes Brahms</artist>
<song>Piano Sonata No. 1 in C major</song>
</music>
</mp3>

<!– selecting “Ludwig van Beethoven” and saving to var xmlMusicShow–>
var xmlMusicShow = xmlMusic.music[0].artist;

<!– inserting “Ludwig van Beethoven” to div “showXML” –>
document.getElementById(“showXML”).innerHTML = xmlMusicShow;

<!– showing that hidden div “showXML” with new value “”Ludwig van Beethoven” –>
<!– Using prototype.js feature $(“someDivID”)  –>
$(‘showXML’).show();
}

</script>
</head>

<body>
<p onclick=”loadXML();” > click me </p>

<div id=”showXML” style=”display:none;”>

</div>
</body>
</html>

Hibernate Interview Questions

Q. What is Hibernate?

Hibernate is powerful, high performance object/relational persistence and query service. Hibernate lets developers develop persistent class following object-oriented principles such as association, inheritance, polymorphism, composition and collections.

 

Hibernate is pure Java object-oriented mapping(ORM) and persistence framework that allows you to map plain old Java objects to relational database tables using XML configuration files. Its purpose is to relieve the developer from a significant amount of relational data persistence-related programming tasks.

 

Q. How will you configure Hibernate?
The configuration files hibernate.cfg.xml (or hibernate.properties) and mapping files *.hbm.xml are used by the Configuration class to create (i.e. configure and bootstrap hibernate) the SessionFactory, which in turn creates the Session instances. Session instances are the primary interface for the persistence service.

• hibernate.cfg.xml (alternatively can use hibernate.properties): These two files are used to configure the hibernate sevice (connection driver class, connection URL, connection username, connection password, dialect etc). If both files are present in the classpath then hibernate.cfg.xml file overrides the settings found in the hibernate.properties file.

• Mapping files (*.hbm.xml): These files are used to map persistent objects to a relational database. It is the best practice to store each object in an individual mapping file (i.e mapping file per class) because storing large number of persistent classes into one mapping file can be difficult to manage and maintain. The naming convention is to use the same name as the persistent (POJO) class name. For example Account.class will have a mapping file named Account.hbm.xml. Alternatively hibernate annotations can be used as part of your persistent class code instead of the *.hbm.xml files.

 

 

Q. What is SessionFactory?

SessionFactory is an interface from which the application gets Session instances. SessionFactory caches generate SQL statements and other mapping metadata that hibernate uses at runtime. Typically one instance of SessionFactory at startup is created for the whole application.

 

Configuration cfg = new Configuration ();

cfg.addResource(“myConfig.hbm.xml”);

cfg.setProperties(System.getProperties() );

SessionFactory sessions = cfg.buildSessionFactory();

 

Q. What is configuration interface in hibernate?

The application uses the configuration interface to specify the location of mapping documents and hibernate specific properties and then creates a SessionFactory out of it.

 

 

Q. Is SessionFactory a thread safe object ?

Yes, SessionFactory is a threadsafe, so many threads can request for session and immutable cache of compiled mappings for a single database.

 

Q. What is session?

Session is a lightweight and non thread safe object that represents a single-unit-of-work with the database. Sessions are opened by SessionFactory and closed when all the work is complete. It represents a persistence manager that manages operations like storing and retrieving objects from database. Instances of sessions are inexpensive to create and destroy.

A single-threaded, short-lived object representing a conversation between the application and the persistent

Store.

 

Q. What role does session interface play in hibernate?

  1. Wraps a JDBC Connection
  2. Factory for Transaction
  3. Holds a mandatory(first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier

 

 

Q. What are detached objects, transient object, and persistent object?

Transient Objects:

            Transient Objects are the objects or instances of persistent classes that are not currently in session.

 

Detached Objects:

            A detached object is an object that has been persistent but the session is closed but still can be attached to another session and those objects can be passed across layers all the way up to the presentation layer without having to use any Data Transfer Objects.

 

Pros: Can be passed up to presentation layer without use of DTO’s

          These detached objects get modified outside a transaction and later on re attached to a new transaction through another session.

Cons: Working with detached objects is quite cumbersome and using Data Transfer Object for the presentation layers                                                                     is better than using detached objects.

 

Persistent Object:

            Persistent Object is the object that is in session currently. It has a representation in the database and an identifier value and might have been saved or loaded.

           

 

Q. What is the difference between load and get in hibernate?

If you are sure that there exist row in database that you are seeking to retrieve then you use load()

If you are not sure then you use get()

 

 

Q. How does Hibernate distinguish between transient and detached objects?

Hibernate uses the version property if there exist one      or     uses identifier value.

 

Q. What is ORM?

ORM stands for Object Relational Mapping. It’s a programmed and translucent perseverance of objects in Java application into the database tables using the metadata that defines the mapping between the objects and the database. It works by transferring the data from one representation into another.

            ORM is Object Relational Mapping which transforms objects in Java, which is based on O-O principle, into the table of databases and for this reads the metadata which has all the mappings of objects and tables in database.

 

Q. What does an ORM solution comprises of?

1. It should have an API for performing basic CRUD operation on the objects of persistent classes.

2. Should have an API or language for specifying queries that refer to the classes and the properties of classes

3. Should have an ability for specifying mapping metadata.

4. It should have a technique of ORM implementation to interact with transactional object to perform dirty checking , lazy association and other optimization functions.

 

Q. What are different levels of ORM quality?

There are 4 levels defined for ORM quality

i.                     Pure relational:

The entire application, including the UI is designed around the relational model and SQL based relational operation.

ii.                   Light Object Mapping:

The entities are represented as classes that are mapped manually to the relational tables. The code is hidden from the business logic using specific design patterns. This approach is successful for applications with a less number of entities, or applications with common, metadata-driven data models. This approach is most known to all.

iii.                  Medium Object Mapping:

The application is designed around an object model. The SQL code is generated at build time. And the association between objects is supported by the persistence mechanism, and queries are specified using an object-oriented expression language. This is best suited for medium-sized applications with some complex transactions. Used when the mapping exceed 25 different database products at a time.

iv.                 Full Object Mapping

Full Object Mapping supports sophisticated object modeling: composition, inheritance, polymorphism and persistence. The persistence layer implements transparent persistence: persistent classes do not inherit any special base class or have to implement or special interface. Efficient fetching strategies and caching strategies are implemented transparently to the application.

 

 

Q. What are the benefits of ORM and Hibernate?

  • Productivity: Hibernate provides much of the functionality, so developers focuses on business logic
  • Maintainability: LOC is very less
  • Performance: Automated persistence works fast
  • Vendor Independence: Easier for cross platform, does not depend on any database.

 

Q. What are the best practices for Hibernate?

1.       Use JavaBeans

2.       Implement equals() and hashCode(), but don’t use id if the id field is surrogate key

3.       Implement serializable interface, useful when migrating to multi-processor cluster.

4.       Persistence class should not be final.

 

 

Q.  What are the core interfaces of hibernate?

The core interfaces of hibernate are:

  1. Session Interface
  2. SessionFactory Interface
  3. Configuration Interface
  4. Transaction Interface:

This is not a mandatory interface like Session,  SessionFactory and Configuration interface. It abstracts

the code from any kind of transaction implementation such as JDBC transactions, JTA transaction.

  1. Query and Criteria Interface:

 

Q. What is Callback Interface?

This interface is used in the application to receive a notification when some object event (when an object is loaded, saved or deleted) occurs. This is useful for implementing certain kind of generic functionality.

 

Q. What are extension interface?

When the functionality provided by hibernate is not enough then hibernate provides a way so that user can include other interfaces for user desired functionality.

 

Q. What is Hibernate Query Language (HQL)?

Hibernate offers a query language which is powerful and provides flexible mechanism to query, store, update and retrieve objects from database. This HQL is an object oriented extension to SQL.

 

Q. What is the difference between load() and get() ?

Load()—use if you are sure that the object exists

Get()—use if you are not sure that the object exist

 

Load()—it will throw an exception if the unique id is not found in the database

Get()—it will return the null value if the unique id is not found in the database

 

Load()—just return a proxy by default and database and database won’t be hit until the proxy is first invoked

Get()—it will hit the database directly.

 

Q. What is the difference between and merge and update?

Use update() if you are sure that the session does not contain an already persistent instance with the same identifier, and merge() if you want to merge your modifications at any time without consideration of the state of the session.

 

 

Q. Define cascade and inverse option in one-many mapping?

Cascade – enable operation to cascade to child entities.

Cascade = “all | none | save-update | delete | all-delete-orphan”

 

Inverse -  mark this collection as the “inverse” end of a bidirectional association.

Inverse=”true|false”

 

Essentially “inverse” indicates which end of a relationship should be ignored, so when persisting a parent who has collection of children, should you ask the parent for its list of children or ask the children who the parents are?

 

Q. What are derived properties?

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 formula attribute of the element.


 

Q. What is dirty checking?

Automatic dirty checking or dirty checking is a feature in hibernate that saves the effort of explicitly asking Hibernate to update the database when we modify the state of an object inside a transaction.

 

Q. What do you mean by fetch strategy?

A fetching strategy is the strategy Hibernate will use for retrieving associated objects if the application needs to navigate through the association. Fetch strategies may be declared in the O/R mapping metadata, or over-ridden by a particular HQL or Criteria Query.

 

Q. What do you mean by lazy fetching?

It’s a fetching strategy used by Hibernate to constraint the retrieval of associated objects when application just needs the objects and doesn’t need to navigate the association.

 

 

 

Q. What is table per subclass, table per class hierarchy and table per concrete class

These all are inheritance mapping based

Table Per Subclass:

           

 

Q. What is lazy initialization?

Book—–publisher

Many to one

If you retrieve book then publisher is also retrieved with it.

If you use getPublisher without closing the session then publisher is accessible, but if we access publisher after session is closed then its an error. This feature of Hibernate is called lazy initialization. This avoids unnecessary database queries and enhances the performance.

 

 

Q. What is unidirectional and bidirectional mapping?

If we want to get reference of child table or object while retrieving parent table or object and not vice-versa, then this type of mapping done is called unidirectional mapping.

 

If we want to get reference of child table or object while retrieving parent table or child and also vice-versa, the this type of mapping done is called bidirectional mapping.

 

 

Q. Explain about collection mapping, association mapping, component mapping, inheritance mapping?

 

Collection Mapping:

            It’s a kind of mapping done in Hibernate to map a collection defined in persistent java object. Basically we can use Bag, Set, List, Array, and Map in collection mapping as defined in Java Objects. Hibernate basically requires persistent collection-valued fields be declared as an interface type.

 

Association Mapping:

            It’s a mapping done in Hibernate generally to show the relation between persistent object. The different mapping often used is one-one, one-many, many-one, many-many.

 

Component-mapping:

            It’s a mapping done in Hibernate to map component (composition in OOP) which is later persisted as value type and not entity type.

 

Q. What is inheritance mapping strategy?

It’s a typical strategy to map “whole object graph” which are related to each other through inheritance. We use these inheritance mapping strategies

1. Table per class hierarchy   2. Table per subclass   3. Table per concrete class

 

Q. What is table per subclass mapping?

It’s an inheritance mapping strategy in which we create table for each subclasses in the inheritance hierarchy and map it according to that in our mapping metadata.

 

 

Q. What is table per class hierarchy?

It’s an inheritance mapping strategy in which a single table is created for all classes in the inheritance hierarchy and map it accordingly in our mapping file.

 

 

Q. What is table per concrete class?

Its one of the inheritance mapping strategy in which we create table for each concrete class and map it accordingly in our mapping file in Java side. Disadvantage is that we could repeat the same properties in the concrete classes, including inherited properties.

 

 

Q. Explain about Interceptors and events?

Its an interface that allows programmers to inspect and/or change property values and inspection occurs before property values are written and after they are read from database. It uses:

 

 

 

 References:

http://faqs.javabeat.net/hibernate/hibernate-interview-questions-faqs-3.php

http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=78&t=001252

http://www.javalobby.org/java/forums/t104442.html

http://anupsabbi.com/index.php?content=articles/interview_questions/hibernate.html

http://www.java-interview.com/Hibernate_Interview_Questions.html

http://www.developersbook.com/hibernate/interview-questions/hibernate-interview-questions-faqs-2.php

http://www.hibernate.org/hib_docs/reference/en/html/objectstate.html

 

 

Follow

Get every new post delivered to your Inbox.