Spring JDBC and Hibernate Flashcards
Which class is used to fetch JDBC connections from database?
DataSource class is used.
Which are minimum required configurations for creating a database connection?
Driveclassname,
URL,
username,
password
Which class of DBCP is used to create and pool database connection?
BasicDataSource
Which tag is used to read properties file?
context:property-placeholder and its location property is used to define path of file.
Which utility is provided in spring that helps in reading data from database or ORM tools?
JDBCTemplate, HibernateTemplate are available which do the heavylifting of cleaning up resources.
Which methods are used to fetch data and execute DML instructions?
To get the data queryXXXmethods are used and for inserting, updating and deleting execute() method is used.
Which overloaded methods are used for querying different data types of single row and multiple rows
queryForInt queryForObject queryForList queryForMap queryForRowSet
What does queryForMap return?
queryForMap returns single row in a Map map.
Why should bind variables be used in place of appending to prepare query?
Preparing query manually makes it prone to SQL injection. So bind variables are used.
Using bind variables with JDBC template
JDBC template takes extra parameter Object[] for passing arguments. jdbcTemplate.queryForObject("Select * from Students where id=?", new Object[] {id}, new StudentMapper());
Which interface is used to map from row to domain objects?
RowMapper interface is used to convert from row to domain object. mapRow(ResultSet rs) should be implemented.
Which method is used for inserting, updating and deleting rows from table using jdbc template?
update() method is used to execute these statements and int returned represents the number of rows affected.
Which template variant can be used to create statement using names?
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 to use SQL batch operations using JDBC template?
SqlParameterSource[] params = SqlParameterSourceUtils.createBatch(employees.toArray());
jdbcTemplate.batchUpdate(“query”, params);
Which interface is provided to set batch values to prepare statement and fire batch operation in jdbctemplate
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.
What is advantage of ORM framework.
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.
In which ways can mapping for class to column name be provided? Including id field, etc?
XML or by annotation.
How to provide hibernate mapping configuration using xml file
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"
Which things are needed to start hibernate integration with spring?
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
Should Hibernate template be used for Spring JPA integration??
Using EntityManager class is recommended instead of HibernateTemplate.
How to get list of objects from HibernateTemplate?
return hibernateTemplate.executeFind(new HibernateCallback>() {
public List doInHibernate(Session session) throws HibernateException, SQLException { return session.createCriteria(Course.class).setMaxResults(100).list(); } });
How to inject EntityManager inside a bean where it is needed?
@PersistenceContext annoatation can be used to inject EntityManager into a class.
Is EntityManager an interface or class?
It is an interface. All communication happens through EntityManager.
Which method is used to find an object using some property like id?
entityManager.find(Person.class, id);