You are on page 1of 6

Thus update(), saveOrUpdate(), lock(), replicate() and merge() are the candidate

options.

update(): Will throw an exception if there is a persistent instance with the same
identifier.

saveOrUpdate(): Either save or update

lock(): Deprecated

replicate(): Persist the state of the given detached instance, reusing the current
identifier value.

merge(): Returns a persistent object with the same identifier. The given instance
does not become associated with the session.

Hence, lock() should not be used straightway and based on the functional
requirement one or more of them can be chosen.

session.clear() detaches all objects.

JpaDao {
public void persist(E entity) {
if (entity.getId() == null) {
entityManager.persist(entity);
} else {
if (!entityManager.contains(entity)) {
entityManager.merge(entity);
}
}
}
}

difference between applicationcontext and beanfactory

req.getParameterValues("t1");

//Composition relationship (has-a)


class University{
private Department departments;

University(){
//Creating a university object depends on creating department objects.
//This indicates university is composed of depratments
departments = new Department()

}
}

//Aggregation relationship (is-a)


class Department{
//Department consists of persons
private Person p;

//Methods which use the person Obj.


}
UBS
How will you design online library management system using TDD and Agile ? Using
testcases for requirements.
2. Talk about concurrency utils i.e. Atomic package ?
3. What is a volatile keyword ?
4. What is a future ?
5. What is decorator design pattern ?
6. Design your own custom Threadpool executor with minimal functionality.
7. Explain Java Memory Model.
8. Why is AtomicInteger class better than a synchronized counter class ? What is
CAS ?
9. What is difference between ExecutorService and ForkJoinPool ?
10. Explain Producer Consumer Problem using Java Code.
11. How will you implement a blocking queue ?

2nd highest salary

SELECT *
FROM Employee Emp1
WHERE (N-1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary
)

java int array of two integers input fibonacci series

select count(*) as count,dept.DNAME


from emp
inner join dept on emp.DEPTNO = dept.DEPTNO
group by dept.DNAME

Dynamic loading of classes makes the implementation of multiple inheritance


difficult.Ambiguity

Can we override a static private method?


# In a class a method throws Null-pointer exception, in the subclass can we
override that method with method which throws runtime exception?
# Do you know about JDBC?
# How to create a immutable class?
# When do you override equal method?
# In a hash map i have inserted a key-value, if we update the key will it happen?
# String str = new String(); String str1 = "test"; where are string objects
created?
# What is enumerator?
# What is difference between vector and array-list?
# How are vector and array list size incremented?
# What are lambda expressions?
# Difference between iterator and Enumeration?
# What is persistence in java?
# Is session factory thread safe?
# Collection Mapping in hibernate
# what is first level caching, second level & third level caching?
# Difference Between Merge And Update Methods In Hibernate?
# Do you know any latest Java script technology?

public class MyStack {


private int maxSize;
private long[] stackArray;
private int top;

public MyStack(int s) {
maxSize = s;
stackArray = new long[maxSize];
top = -1;
}
public void push(long j) {
stackArray[++top] = j;
}
public long pop() {
return stackArray[top--];
}
public long peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
public boolean isFull() {
return (top == maxSize - 1);
}
public static void main(String[] args) {
MyStack theStack = new MyStack(10);
theStack.push(10);
theStack.push(20);
theStack.push(30);
theStack.push(40);
theStack.push(50);

while (!theStack.isEmpty()) {
long value = theStack.pop();
System.out.print(value);
System.out.print(" ");
}
System.out.println("");
}
}

jdbcTemplate.query("select string1,string2 from table where x=1", new


ResultSetExtractor<Map>(){
@Override
public Map extractData(ResultSet rs) throws SQLException,DataAccessException {
HashMap<String,String> mapRet= new HashMap<String,String>();
while(rs.next()){
mapRet.put(rs.getString("string1"),rs.getString("string2"));
}
return mapRet;
}
});

Employee DAO interface:


public interface EmployeeDao {

public String findEmployeeName(int empId);


public int findMaxSalary();
}
import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class EmployeeDaoImpl extends JdbcDaoSupport implements EmployeeDao{

@Override
public String findEmployeeName(int empId) {

String query = "select name from employee where emp_id=?";


Object[] inputs = new Object[] {empId};
String empName = getJdbcTemplate().queryForObject(query, inputs,
String.class);
return empName;
}

@Override
public int findMaxSalary() {

String query = "select max(salary) from employee";


int maxSalary = getJdbcTemplate().queryForInt(query);
return maxSalary;
}
}

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="com.mysql.jdbc.Driver" />


<property name="url" value="jdbc:mysql://localhost:3306/java2novice" />
<property name="username" value="user_name" />
<property name="password" value="password" />
</bean>

<bean id="employeeDAO" class="com.java2novice.dao.EmployeeDaoImpl">


<property name="dataSource" ref="dataSource" />
</bean>
</beans>

public class SpringDemo {

public static void main(String a[]){

String confFile = "applicationContext.xml";


ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext(confFile);
EmployeeDao empDao = (EmployeeDao) context.getBean("employeeDAO");
String empName = empDao.findEmployeeName(3);
System.out.println("Employee name: "+empName);
System.out.println("-----------------------------------------------");
int maxSal = empDao.findMaxSalary();
System.out.println("Max salary: "+maxSal);
}
}

MySQL query to find customers who have made 0 orders / no orders

select customer_name from customer


where customer_id not in (select customer_id from orders);

SELECT Persons.LastName, Persons.FirstName


FROM Persons
LEFT JOIN Orders ON Persons.id = Orders.Person_id
WHERE Orders.Person_id IS NULL;

@Test
public void testEquals_Symmetric() {
Person x = new Person("Foo Bar"); // equals and hashCode check name field
value
Person y = new Person("Foo Bar");
Assert.assertTrue(x.equals(y) && y.equals(x));
Assert.assertTrue(x.hashCode() == y.hashCode());
}

The serialization runtime associates with each serializable class a version number,
called a serialVersionUID, which is used during deserialization to verify
that the sender and receiver of a serialized object have loaded classes for that
object that are compatible with respect to serialization. If the receiver has
loaded a class for the object that has a different serialVersionUID than that of
the corresponding sender's class, then deserialization will result in an
InvalidClassException. A serializable class can declare its own serialVersionUID
explicitly by declaring a field named "serialVersionUID" that must be static,
final, and of type long:

ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;

Load : When ever the load() method is called, the hibernate creates a proxy object
of a POJO class, and it will set the id to the proxy object, then it returns
the proxy object to the program. Based on the operations performed on the proxy
object, the hibernate will decide whether to go cache or database to load the
data. This process is called lazy loading.

Get : When we call the get() method, then hibernate first goes to first level cache
and if that object doesnt exist in the first level cache then it goes to
database and loads the object from database. If Id doesnt exist in the database,
then get() method returns null. When get() method is called no proxy object
is created, hence it is called as early loading.

http://www.onlinetutorialspoint.com/hibernate/hibernate-session-differences-
between-load-and-get.html

Using @Qualifier in case of conflict

https://joychakravarty.wordpress.com/spring/singleton-beans-with-prototype-bean-
dependencies/ read it

You might also like