You are on page 1of 21

1 of 15

Consider the following Customer table:

idname city t

1001 Tim New York


1002 Roger New York H

Assumingthat necessaryimports are done.jdbctemplate is a valid reference of SpringjdbcTemplate, what is the output when
following code is executed?
String sql "update Customer set city? where city
System.out.printin (jdbcTemplate. update(sql,Texas "New York"))
w

ww

O wws

O N W
A

2
The
codewili throw PuntumeException
The code will throw RuntimeException.

Q2 of 15
Which of the following statement(s) is/are correct about methods of jdbcTemplate class?Choose any 2.

The queryForObject() method throws EmptyResultDataAccessExceptionif no data is present in table in table.

T h e query() method returns an empty list if no data is present in table.

T h e queryForObject{) method returnsnull if ae data is presentin table.

The query() method returns nullif no data is presentin table

Q3 of 15

Consider the following entity clasS


@Entity
@Table(name="card")
public class CreditCard{
@Id
private Integer cardId
Q3 of 15

Consider the following entity class:

@Entity
Table(name="card")
public class CreditCard{
@Id
private Integer cardId;
private String cardNumber
private LocalDate expiryDate;
Ilgetter and set cer methods
iullaiililiui

ASsuming that necessary im orts are done, entityManager is a valid reference ofJPA EntityManager and transactions are managed by
Spring, what is the output when following code is executed?

CreditCard card new Creditcanrds W


w ww
card.setCardId (101)3 Wwwww w
W

card.setCardNumber(25666545
entityManager.persist(card)s w
entityManager.persist(card)
w
ww
W
T h e c o d e will throwttiatt
runtimewwwa
exception after inserting o n e row i n card
t
table

ifi

O n e row will be inserted in credit card table

Ww
O n e row will be inserted in card table.

The code will throw runtime exception as unique constrant is violared. l Ass
Q4 of 15
Consider the following entity class:
@Entity
public class Customer{
@Id
1 Line 1
private Integer cust nerId;
private String name;
//getters and setters
www
www
ww
Which of the foliowing statement can be inserted at Line 1 to generate customerld using TABLE Strätegy?
ww

@Generatedvalue(strategy=GenerationType.TABLE)
@Generatevalue(strategy=GenerationType.TABLE)

@Generatedvaluestrategy="TABLE") E

@Generatedvaiuelstrategy=Generation.TABLE)

dAsessm
l Assessment- Persistence Layer using Spring ORM with Spring Boot

Q5 of 15
Consider the following table:

create table participant (


participant_id number,
name varchar2 (1e)

Assuming that springPhysir alNamingStrategy is followed, which of the following entity class is properly mapped to participant table?
Choose any 3.

@Entity@Table(name="participant") public class ParticipantEntity{ @ld prpte Integer participantld;


@Column(name="name") private String participantName; //getters and setters

@Entity @Table(name="participant") publicclassParticipantEntity{ @ld private Integer participantld; private


String participantName; //getters and setters}

@Entity @Table(name="participant") public class ParticipantEntity{ @ld @Columniname="participant_id")


private Integer participantld; @Column(name="name") private String participantName; /getters and setters}

@Entity public class Participant{ @ld private Integer participantid: @Columnname="name") private String
participantName; //getters and setters)
Q6 of 15

Consider the following entity class:

@Entity
public class Employee {
@Id

@GeneratedValue (strategy=GenerationType AUTO)


private Integer employeeId
private Integer salary
/getters & setters

Assume that following seqi ance exists and no records existsin the table:
create sequence hibernate sequence start with 1 increment by 13
Assuming that necessary imports are done, entityManager is a valid reference ofJPA EntityManager and transactions are managed by
Spring. what is the output when following code is executed?

Employee employee =
new Employee()
Employee.setsalary(1000e);
entityManager.persist(employee)
System.out.println (employee.getEmployeeid ())

O2
null
02
O null

Q7 of 15
Which of the following anr tation you will for C
use
injecting EntityManager?

@PersistenceContext
O @PersistentContext

O@PersistContext
None of these

Q8 of 15
Consider the following tables and entity classes:

Transaction (t'ansactuorn_id, offer_price) where transactuon_id is


primarykey
Passenger(passenger_id pässenger_rname. t_id) where passenger_id Is primary key and t_id is farergn key rafarring ro se5sm
Lratiat
Q8 of 15
Consider the following tables and entity classes:

Transaction (transaction_id,
offer_price) where transaction_id is primary key.
Passenger(passenger_id. passenger_name, tid) where passenger_id is primary key and jd is foreign key referring to
transaction_id of Transaction table.
RE
@Entity
@Table(name ="Passenger") Ex
public class Passeng Entity {
@Id
private Inte er passengerId; Po
private String passengerName;
1/Line 1 Gro
private TransactionEntity transactionEntity;
//setter and getter methods
Orde

@Entity PQL
Table(name= "Transaction")
public class TransactionEntity { Update
@Id
@Column(name-"transaction_id") ; Quz:
private Integer transId;
private Double offerPrice;
Exercise-
//setter and getter methods
Apperd

C fiE l 2 op wferi ierted at Line 1 brings Une to-one relatinship betacen P t


Entity
Table(name="Transaction")
public class TransactionEntity {
@Id
@Column(name= "transaction_id");
private Integer transId;
private Double offerPrice;

/setter and ge ter metthods


wwP

www

Which of the following options when inserted at Line 1 brings one-to-one relationship between PassengerEntity and TransactionEntity
w
class?

@oneToonefcascade=CascadeType.ALL)@oincolumntname-"transaction id", unique=true)

@oneToOne(cascade=CascadeType.ALL)@joincolumn(name="trans id")

O@OneToonetcascade=CascadeType.ALL) @Joincolumn(name-"tid unique=true)

@OneToOnelcascade=CascadeType.ALL)@JoinColumntname="passenger_id", unique=true

Q9 of 15
Consider the following tatles and entity classes
Q9 of 15

Consider the following tables and entity classes:

Customer (custoner_id, custormer_name) where customer_id is primary key. to customer_id in the Customer table.
Transaction (transaction_jd, offer_price,.cust_id) where cust_id is foreign key referring

@Entity
public class Customer{
@Td
private Integer customerId;
private String custonerName;
1/Line 1
//setter and gettter ethods

@Entity
public class Transaction{
@Id
@Column(name "transaction_id")
private Integer transId;
private Double offerPrice;
//setter and getter methods

Which of the following option(s) can be inserted at Line 1 to bring one-to-many relationship between Customer and Transaction entity
classes?Choose any 2.

n@OneToMany(cascade=CascadeType.ALL) @JoinColumn(name="customer_id ") private


Set<TransactionEntity> transaction;
ioOneTaManvlcasca adeTine Al _ -+
with Spring Boot
l Assessment Persistence Layer using Spring ORM
walt

@Id wir

@column(name="transaction_id");
private Integer transId;
lillt
private Double offerPrice;
//setter and getter methods

il

Which of the follovwing option(s) can be inserted at Line 1 to bring one-to-many relationship between Customer and Transaction entity
:il

classes?Choose any2. 1

@OneToMany(c scade=CascadeType.ALL)@JoinColumn(name="customerid ") private


Set<Transaction ntity> transaction

@OneToMany(cascade=CascadeType.ALL) @oinColumn(name-"custid ") private Set<Transaction Entity»


transaction;

@OneToManylcascade=CascadeType.ALL) @JoinColumn(name="cust_id", unique=true) private


Set<TransactionEntity> transaction,
@OneToMany(cascade=CascadeType.ALL) @JoinColumn(name="cust_id ") private List<TransactionEntity>
transaction;

Q10 of 15
Consider the foilowing table and entity class:

Instructorinetructor_id.instructor_name.skill) where instructor_id is primary key.


Q10 of 15
Consider the following table and ent ty class:

Instructorinstructor_id,instructor_name.skill) where instructor_id is primary key.

@Entity
public class Instructor{
@Id
private Integer instructorId;
private 5tring instructorName;
private String skill;
/getter and setter

Assumingthat Instructor entity class is mapped with Instructor table and entityManager is a valid reference of JPA EntityManager
which of the following are correct optlon(s) to retrieve all instructors whose skili is "Java?

1. Query query-entityManager.createQuery("select i from Instructor i where i.skill :skill")


query.setParameter ("skill", "Java");
query.getResultList()
2. Query queryentitylanager.createQuery("select i from Instructor i where i.skill ? 1 ");
query.setParameter (1, "Java");
query.getResultList)

3. Query query-entitymanager.cneateQuery("select i from Instructor i where i.skill ? ");


query.setPanameten(e ava)3
query getResultList()

teQuery("select i from Instructor i where i. skill = ?1 ");


4.Queryquery=entitylanager.C
i from Instructor i where 1.skill ?1 ");
query-entityManager.createQuery("select
2. Query
query.setParameter (1, "Java");

query.getResultlist();

query=entityManager.createQuery("select i from Instructor i where 1.skill ?");


3. Query
query.setParameter(0, "Java"):
query-getResultlist)

i from Instructor i where i.skill ? 1 ");


4. Query query=entityManager.createQuery("select
query.setParameter (0, "Java");
query.getResultlist();

OOnly 1. 2 and 4
Only 1 and 2

O n l y 1 and 4

O Only 2 and 4

Q11 of 15
e tat Customer entity ciass Is mapped to following customer table: lAss
Q11 of 15
customer table
Assume that Customer entity class is mapped to following

id namecity
=

1Tim Texas 43
2 Monica Plano 23
3Chris Washingtorn|28 transactions are managed by
Assuming that necessary imports a e done, entityManager is a valid reference of JPA EntityManager, and
Spring, what is the output when fo owing code is executed?
entityManager.c 2ateQuery (update customer c
set c.city ?1 where c.age?2);
Query query =

query.setParameter(1, "Plano")
query.setParameter (2,24);
List<Customer> result query.getResultList()

I t will update 2 rows in customer table

O It will throw llegalStateException.

I t will throw NoOResultException.

I t will neither throw exception nor update the table


Q12 of 15

Consider the following entity classes and corresponding tables:

QEntity
@Table(name = "employee")

public class EmployeeEntity {


@Id
private Integer employeeId;
private String employeeName;
@OneToone (cascade =CascadeType .ALL
@Joincolumn (name = "laptop_id", unique true)

private LaptopEntity laptopEntity;


ligetter and setter

@Entity
@Table(name = "laptop")

public class LaptopEntity{


@Id
private Integer laptopId
private String model;
/getter and setter

Employee
@Entity
@Table(name "laptop")
public class LaptopEntity{
@Id
private Integer laptopId;
private String model;
1/getter and se ter

Employee

employee_idlemployee_namelaptop_id
3001 Tom 540
3002 Jack 541
Laptop
laptopid model
r

540 HP4120
541 Acer423
AssumingthatEmployee and Laptop tables are presentindatabase and mapped to Employee entity class and Laptop entity class.
entityManager is valid reference ofJPA EntityManager,and ransactions are managed by Spring. What is the output when following
code is executed?

EmployeeEntity employee entityManager.find(EmployeeEntity.class, 3001);


entityanager. remove(employee)
employee_jd employee_name laptop_Jd
3001 Tom 540
3002 jack 541
Laptop
laptopjdmodel
540 HP4120
541 Acer423
ASsuming that Employee and Laptop tables are present in database and mapped to Employee entity class and Laptop entityclass,
entityManager is valid reference of JPA EntityManager, and transactions are managed by Spring. what is the output when following
Code is executed?

EmployeeEntity employee - entityManager.find(EmpioyeeEntity.class, 3001);


entityManager.remove(em loyee);

O n erow of Employee table with employee_id=3001 is removed and no changes in Laptop table.

One row will be deleted from Employee table as well as Laptop table

one row of Laptop table withlaptop_id=540 is removed and no changes in Employee table

Integrity constraint violation exception will be thrown

Q13 of 15
Q13 of 15
In Spring JDBC, to map each row of the table to an
object of model class which interface needs to be implemented?

RowMapper
MapRow

O oWMap
Mapper

Q14 of 15
Consider the following professor table with professor_id as primary key:
professor_id|department|
1001 Electronics
1002 Mechanical

1003
1004 Civil
1005 Mechanical
14 of 15
Consider the foliowing professor table with professor_id as primary key:

professor id department
|1001 Electronics
1002 |Mechanical
1003 EEE
1004 Civil
1005 Mechanical
1006 EEE
ASsuming that Professor e y class is mapped with professor table, entityManager is valid reference of JPA EntityManager, and
transactions are managed y Spring. what is the output when
following code is executed?
Query
queryentityManager.createQuery
-

query.setParameter ("EEE",1);
("UPDATE Professor p SET p.department 1 where p.professorId 22"):

query.setParameter (1881, 2)
int num= query.executeUpdate()
System.out.println (num) ;

O
professor d|ldepartment fntroduction
1001 Electronics
1002 |Mechanical Selection T
1003 EEE
Restriction -
=

|1004 Civil
|1005 |Mechanical
|1006 EEE
Exercise: JPQ=
Assuming that Professor entity class is mapped with professor table,
transactionsare
managed by Spring, what is the
entityManager is valid reference of JPA EntityManager, and PQL ARgregat-
output when following code is executed?
Query query entityMi 1ager.createQuery("UPDATE Professor p SET p.department = ?1 where p.professorId= ?2");
Grouping -The
query.setParameter("E",1);
query.setParameter(10 2); Ordering -The
int num=
query.execut ipdate();
System.out.println(nu
JPQL Functions

Update and Del

O Quiz:JPQL
O2 Exercise: JPQL

The code willthrow runtime exception. Appendix


A

lAssessment Persistend
l Assessment Persistence Layer using
Spring
-

ORM with Spring Boot


Contents
Consider the following screen table:

screen_idmovie_namerating|| Introc5
1001 Avengers 4.3
1002 Inception 4.5 SelectiF
1003 Deadpool 4
1004 Alien 3.9
Restric
1005 Deadpool4
Exercise
1006 Deadpool4
Assuming that Screen entity class is mapped with screen table, entityManager is
transactions are managed by Spring. What
valid reference ofJPA EntityManager, and PQL Ag
is the output when following code is executed?
Query updateQuery entityManager.createQuery("UPDATE Screen SET S.movieName ?1 where s.screenId
s Grouping
=
?2");
Query deleteQuery entityManager.createQuery("DELETE FROM Screen s where s.movieName = 'Deadpool ");
=

updateQuery.setParameter (1, "Deadpool") Ordering-


updateQuery.setParameter (2, 1002):
updateQuery.executeUpdate();
int num= deleteQuery. executeUpdate() PQL Functi
System.out.println(num);
Update and

Quiz :JPQL
O3
Exercise : JPQI
4

Appendix
OTwo JPQL queries cannot perform operation on the same en
Assassmen -Persiste

You might also like