Spring JDBC and Hibernate Flashcards

1
Q

Which class is used to fetch JDBC connections from database?

A

DataSource class is used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which are minimum required configurations for creating a database connection?

A

Driveclassname,
URL,
username,
password

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which class of DBCP is used to create and pool database connection?

A

BasicDataSource

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which tag is used to read properties file?

A

context:property-placeholder and its location property is used to define path of file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which utility is provided in spring that helps in reading data from database or ORM tools?

A

JDBCTemplate, HibernateTemplate are available which do the heavylifting of cleaning up resources.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which methods are used to fetch data and execute DML instructions?

A

To get the data queryXXXmethods are used and for inserting, updating and deleting execute() method is used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which overloaded methods are used for querying different data types of single row and multiple rows

A
queryForInt
queryForObject
queryForList
queryForMap
queryForRowSet
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does queryForMap return?

A

queryForMap returns single row in a Map map.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why should bind variables be used in place of appending to prepare query?

A

Preparing query manually makes it prone to SQL injection. So bind variables are used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Using bind variables with JDBC template

A
JDBC template takes extra parameter Object[] for passing arguments.
jdbcTemplate.queryForObject("Select * from Students where id=?", new Object[] {id}, new StudentMapper());
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which interface is used to map from row to domain objects?

A

RowMapper interface is used to convert from row to domain object. mapRow(ResultSet rs) should be implemented.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which method is used for inserting, updating and deleting rows from table using jdbc template?

A

update() method is used to execute these statements and int returned represents the number of rows affected.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Which template variant can be used to create statement using names?

A
NamedParameterJdbcTemplate can be used.
String sql = "insert into students (NAME) values (:name)";
Map params = new HashMap<>();
params.put("name", name);
jdbcTemplate.update(sql, params);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to use SQL batch operations using JDBC template?

A

SqlParameterSource[] params = SqlParameterSourceUtils.createBatch(employees.toArray());
jdbcTemplate.batchUpdate(“query”, params);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which interface is provided to set batch values to prepare statement and fire batch operation in jdbctemplate

A

BatchPreparedStatementSetter is provided. It has method that gives you prepared statement and index that you can use to fetch value at that index and set that in prepared statement. The batching logic is implemented by the template.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is advantage of ORM framework.

A

1) It hides the underlying database and so we can change that in future without change in application.
2) Makes integration with database easy by automatically converting beans and relations to sql query.
3) Reduces development time.

17
Q

In which ways can mapping for class to column name be provided? Including id field, etc?

A

XML or by annotation.

18
Q

How to provide hibernate mapping configuration using xml file

A
hibernate mappings are usually called class.hbm.xml format. 
class name="classname" table = "courses
    id name = "id"
    generator class="assigned"
    id over
    property name="name"
    property name="price"
19
Q

Which things are needed to start hibernate integration with spring?

A

1) Datasource bean BasicDataSource
2) A session factory - LocalSessionFactoryBean. Provide datasource as dependency and provide all hibernate configuration files.
3) hibernate properties - which includes dialect (database specific), hbm2ddl.auto to convert from hibernate to DDL queries, show sql which will print the sql created by hibernate

20
Q

Should Hibernate template be used for Spring JPA integration??

A

Using EntityManager class is recommended instead of HibernateTemplate.

21
Q

How to get list of objects from HibernateTemplate?

A

return hibernateTemplate.executeFind(new HibernateCallback>() {

		public List doInHibernate(Session session) throws HibernateException, SQLException {
			return session.createCriteria(Course.class).setMaxResults(100).list();
		}
	});
22
Q

How to inject EntityManager inside a bean where it is needed?

A

@PersistenceContext annoatation can be used to inject EntityManager into a class.

23
Q

Is EntityManager an interface or class?

A

It is an interface. All communication happens through EntityManager.

24
Q

Which method is used to find an object using some property like id?

A

entityManager.find(Person.class, id);

25
Q

Which method is used to insert of update an entity using EntityManager?

A

entityManager.merge(person);

26
Q

Which method is used to delete somthing using EntityManager?

A

If you have to delete by id and you don’t have person object, then you first have to query person object using entityManager.findById(id) and then use that person object to get it deleted.
entityManager.remove(person) is used to delete.

27
Q

How to find list of all records of particular type using EntityManager. Similar to Select * From Person table?

A

First we will have to create a TypedQuery and then use it to gget all results.

TypedQuery query = entityManager.createNamedQuery(“find_all_persons”, Person.class);
List persons = query.getResultList();

28
Q

EntityManager is part of which JSR?

A

It is part of JPA (Java Persistence API) JSR.

29
Q

What is TypedQuery?

A

TypedQuery gives you option of specifying the type of entity when creating the query. So there is no need for explicit cast. Whereas normal Query requires type cast from Object to required type.

30
Q

What is entityManager.createNamedQuery()

A

Similar to how constatns is defined. Named query is way to define it by giving it a namw. You can define the query either in hibernate configuration xml or using annotation on the entity class.

31
Q

How to define NamedQuery in entity class?

A

@NamedQuery(name=”find_all_persos”, query = “select p from Person p”)

The query is an HQL query.

32
Q

How to automatically fire sql DDLs and DMLs when spring boot data applications starts?

A

Keeping a file named data.sql is automatically picked up from classpath and is fired by spring.

33
Q

Why is “data.sql” file useful in Spring boot data application?

A

It is useful to initialize database with some initial data.

34
Q

Provide different types of queries that can be made using Hibernate?

A

Native Query - This query is SQL query that can be directly executed on database using database client.

entityManager.createNativeQuery(“”) can be used to create it.

NamedQuery - Similar to constant, queries can be provided names and defined in either hibernate configuration file or on entity class using annotation. When creating query just name of query needs to be provided to EntityManager.

Query q = entityManager.createNamedQuery(“name”);

TypedQuery - Usually query returns objects but typed query gives you option to provide entity type, so you don’t need to explicitly cast the result.

TypedQuery tq = entityManager.createNamedQuery(“name”, Person.class);