JPA interview questions and answers and high level overview - part 2
This is a continuation of JPA and Hibernate interview Questions and answers part 1.
Q. What is an EntityManager?
A. The entity manager javax.persistence.EntityManager provides the operations from and to the database, e.g. find objects, persists them, remove objects from the database, etc. Entities which are managed by an EntityManager will automatically propagate these changes to the database (if this happens within a commit statement). These objects are known as persistent object. If the Entity Manager is closed (via close()) then the managed entities are in a detached state. These are known as the detached objects. If you want synchronize them again with the database, the a Entity Manager provides the merge() method. Once merged, the object(s) becomes perstent objects again.
The EntityManager is the API of the persistence context, and an EntityManager can be injected directly in to a DAO without requiring a JPA Template. The Spring Container is capable of acting as a JPA container and of injecting the EntityManager by honoring the @PersistenceContext (both as field-level and a method-level annotation).
Here is a sample PersonDaoImpl using the Person entity
package com.mycompany.myapp.impl; //... import com.mycompany.myapp.model.Person import java.util.ArrayList; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import org.springframework.stereotype.Repository; @Repository("personDao") public class PersonDaoImpl implements PersonDao { @PersistenceContext private EntityManager em; @Override public List<Person> fetchPersonByFirstname(String fName) { Query query = em.createQuery( "from Person p where p.firstname = :personName", Person.class ); query.setParameter("firstName", fName); List<Person> persons = new ArrayList<Person>(); List<Person> results = query.getResultList(); return persons; } public Person find(Integer id) { return em.find(Person.class, id); } }
Q. What us an Entity?
A. A class which should be persisted in a database it must be annotated with javax.persistence.Entity. Such a class is called Entity. An instances of the class will be a row in the person table. So, the columns in the person table will be mapped to the Person java object annotated as @Entity. Here is the sample Person class.
package com.mycompany.myapp.model; //.... @SuppressWarnings("serial") @Entity @Table(name = "person", schema = "dbo", catalog = "my_db_schema") @Where(clause = "inactiveFlag = 'N'") @TypeDefs( { @TypeDef(name = "IdColumn", typeClass = IdColumnType.class)}) public class UnitTrustPrice implements java.io.Serializable { private int id; private String firstName; private String surname; private byte[] timestamp; private char inactiveFlag; public Person() { } //.. other constructors @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "person_id", nullable = false, precision = 9, scale = 0) @Type(type = "int") public int getId() { return this.id; } public void setId(int id) { this.id = id; } @Version @Column(name = "timestamp", insertable = false, updatable = false, unique = true, nullable = false) @Generated(GenerationTime.ALWAYS) public byte[] getTimestamp() { return this.timestamp; } public void setTimestamp(byte[] timestamp) { this.timestamp = timestamp; } @Column(name = "InactiveFlag", nullable = false, length = 1) public char getInactiveFlag() { return this.inactiveFlag; } public void setInactiveFlag(char inactiveFlag) { this.inactiveFlag = inactiveFlag; } @Column(name = "first_name", nullable = false, length = 30) public String getFirstName() { return this.firstName; } public void setFirstName(String fName) { this.firstName = fName; } @Column(name = "surname", nullable = false, length = 30) public String getSurname() { return this.surname; } public void setSurname(String sName) { this.surname = sName; } //.......... }
Q. What are the dependency jars required for a JPA application?
A Include the relevant jar files via your Maven pom.xml file.
<!-- JPA and hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> </dependency> <!-- validator --> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> </dependency>
Labels: Hibernate, Hibernate Tutorial, JPA
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home