Friday, June 25, 2021

What is annotations?

 Annotations are meta data about a class that will be processed by compiler at runtime or compile time.

Ex: when we override a method of parent class then always we see a @Override annotation that is processed at compile time . It actually inform compiler that we are overriding a method .Then compiler check the classes .

when we use annotation in spring ,the it will scan java classes for special annotation.

Automatically register bean to spring container based on annotations.

Ex:

@Component("person")

public class Person extend Human{

}

here spring will scan for component and register this class as bean to spring container .

Using the bean id "person" we can able to retrieve the bean from spring container.

Retrieve bean from spring container,

Person person = context.getBean("person",Person.class);

here context is the ApplicationContext which is spring container.

Monday, June 21, 2021

What is bean scope?

- Scope refers to the life cycle of a bean .

-How long does the bean will alive

-How the bean will share

-How many instances will created

By default bean scope is singleton.

Singleton: Spring container will create single instance .All request for the bean will return a shared reference for that bean.


Other scopes:

-Prototype:Create new reference and return it .
 


Sunday, June 20, 2021

What is dependency injection?

Ans: Dependency means something that is dependent on something .When an object dependent on another object then that object is the dependent object. Ex: if we assume car is an object then tire ,door, machine is the dependent object of car. So, dependency injection means of injecting dependent object from object factory(Spring container) throw constructor/setter/autowiring .

Dependency can be injected throw -

1.Constructor injection

2.Setter injection

3.Autowiring.

Wednesday, June 16, 2021

Function of Spring container-->>

 --Create and manage object(IOC-Inversion of control)

--Inject object dependencies(Dependency injection)








--Spring container generically known as ApplicationContext

Ex:xml based





Sunday, June 6, 2021

Hibernate object life cycle state

Hibernate life cycle contains the following states: 

1.Transient

2.Persistent

3.Detached

1.Transient:The initial states of an object.when an object is created it entered into transient state.It is not associated with any session.So this state is not related to any db . In transient state object reside into heap memory.

Ex: Person person = new Person();//Here person is in transient state

person.setName("Lokman hossain");

2.Persistent:As soon as the object associated with the session ,it entered in the persistent state.Any modification in this state will effect the database .

ex:session.save(person);

session.persist(person);

session.saveOrUpdate(person);

session.merge(person);

session.lock(person);

3.Detached:When the session is closed the object entered into detached state.Changes will not effect the database.Object is no longer associated with hibernate session.

ex:session.close();

session.clear();

session.detach();

session.evict();


Tuesday, June 1, 2021

Hibernate one-To-many bi-directional:

 Hibernate one-To-many bi-directional:

Instructor class:

import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class Instructor {
private int id;
private String first_name;
private String last_name;
private String email;

private List<Course> courses;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@OneToMany(mappedBy = "instructor",cascade  = CascadeType.ALL)
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}

//convenient method for bi-directional relationship.
public void add(Course tempCourse) {
if(courses==null) {
courses = new ArrayList<Course>();
}
courses.add(tempCourse);
tempCourse.setInstructor(this);
}
}

Course class:

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Course {
private int id;
private String title;
private Instructor instructor;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "instructor_id")
public Instructor getInstructor() {
return instructor;
}

public void setInstructor(Instructor instructor) {
this.instructor = instructor;
}

@Override
public String toString() {
return "Course [id=" + id + ", title=" + title + ", instructor=" + instructor + "]";
}
}

Main class:

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class MainApp {

public static void main(String[] args) {

SessionFactory sessionFactory = (SessionFactory) new Configuration().configure("Hibernate.cfg.xml")
.addAnnotatedClass(Instructor.class).addAnnotatedClass(Course.class).buildSessionFactory();

Session session = sessionFactory.getCurrentSession();
try {
Transaction tx = session.beginTransaction();

Instructor i1 = new Instructor();

i1.setFirst_name("nishat");
i1.setLast_name("nabila");
i1.setEmail("nabila@gmail.com");

Course c1 = new Course();
c1.setTitle("math");
c1.setInstructor(i1);

Course c2 = new Course();
c2.setTitle("physics");
c2.setInstructor(i1);

Course c3 = new Course();
c3.setTitle("social science");
c3.setInstructor(i1);

List<Course> allCourse = new ArrayList<Course>();
allCourse.add(c1);
allCourse.add(c2);
allCourse.add(c3);

i1.setCourses(allCourse);
session.save(i1);

tx.commit();

}

finally {
session.close();

}

}
}






Fluent interface pattern

 public class UserConfigurationManager {     private String userName;     private String password;     private UserConfigurationManager() { ...