Generator class-

​Generator class is a sub element of id column.

Generator class is used to generate unique identifier

Hibernate supports 12 generators class currently

But most important are-

 (1)assigned

 (2)increment

Advertisements

6- Mapping in Hibernate

Mapping meaning in hibernate-

Connection between java class to database tables

Types of mapping in hibernate-

1- Inheritance Mapping-

When java classes are having inheritance, their tables can be created using 3 ways-

1- Table per class hierarchy
2–Table per sub-class hierarchy
3-Table per concrete class hierarchy

2- Collection Mapping-

When java class is having collection object, they can be stored according to their type of collection –

1-list mapping
2-set mapping
3-map mapping

3- Association mapping-

When java class is having reference of another java class, their tables can be created using 3 ways-

1-One to one
2-One to many or many to one
3-Many to many

5- Caching in hibernate

Cache definition-

Cache is a temporary data which is used to speed up the performance.

Types of caching in hibernate-

1-Session Cache/First level cache in hibernate-

  • First level cache is enabled by default.
  • First level cache can’t be disabled.
  • Objects are retrieved from current session..

2- SessionFactory Cache/Second level cache in hibernate-

  • Second level cache is disabled by default.
  • Second level cache is configured to use.
  • Objects can be retrieved from various sessions.

Extra-Query Level cache in hibernate-

  • Query Cache is used to cache the results of a query.

4- States of objects in hibernate

Hibernate object states-

5

1-Transient state-

  • When object is just created and has no primary key, the state is called transient.

2-Persistent state-

  • When sessio is opened and object is just saved in database [using save, saveOrUpdate, persist, merge etc method]
  • or retrieved from the database [using get, load etc method]. This state is called persistent.

3-Detached state-

  • When the session was closed, the state changes to detached.
  • or evict() , clear() etc methods are applied, The object is detached from its session.

3-Hibernate Classes and Interfaces

1- Configuration class – includes 2 things

(i)Database connection- Using either .cfg or .properties file
(ii)mapping – connection between java classes to database tables using either .hbm files or annotations

2-Session Interface-

  • Object of Session is created each time when any operation is required.
  •  Session objects are used to create physical connection with database.
  • Session is thread-safe.
  • Session holds a mandatory (first-level) cache of saved java objects in database.
  • Session wraps JDBC connection for Transaction.

3-SessionFactory Interface-

  • Object of SessionFactory should be created only once per database.
  • SessionFactory is used to create session objects.

4- Transaction Interface- 

  • Transaction represents unit of work with database.
  • Transaction is associated with session.

5-Query-

  • Query is used to create manual queries.

6-Criteria.

  • Criteria is used to apply conditions on the result set.

2-Hibernate definition

Hibernate Definition

  • Hibernate is pure ORM tool.
  • ORM stands for Object Relational mapping.
  • ORM is a concept to save/manipulate java objects to database tables.
  • The main goal of hibernate to avoid old JDBC code and focus more on business logic.

Advantages of Hibernate

  • No need to maintain lengthy JDBC code
  • write less code
  • Lazy Loading
  • Caching

Hibernate Architecture

5

 

Step 10:Type Casting, Boxing/InBoxing and OutBoxing -6th hour +code

Definitions

-Assigning a value of one type to a variable of another type is known as Type Casting.

Datatype hierarchy-
(Biggest datatype) double > float > long > int > char > short > byte (smallest datatype)

primitives
type-casting-different-forms
(i)Widening/Automatic type conversion/Up casting
-When value of small datatype is saved into big datatype.
i.e-
Continue reading “Step 10:Type Casting, Boxing/InBoxing and OutBoxing -6th hour +code”

Step 15:Oops- 3rd Concept:Abstraction: 10th hour + code

Definition-

Abstraction is a concept of showing only functionality and hiding procedure.

In java, only name of method is given and not the body.

-Abtraction can be applied on classes and methods using abstract keyword.

– In abstraction we focus on “what” the object does instead of “how”.

1- abstract class
– Only abstract class can have abstract methods

Continue reading “Step 15:Oops- 3rd Concept:Abstraction: 10th hour + code”

Step 16:Oops- 4th Concept:Encapsulation: 11th hour + code

Definition-

Encapsulation is a concept of binding data and code together in single unit.

-In java, data means data members, and code means methods.

– In java, in encapsulation, data members are private and access methods (setters and getters) are public.

1- Creation of encapsulation class-


package ncapsulsn;

public class ExEncapsulated {

private String name;
private int age;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

2- Usege of encapsulation class-

package ncapsulsn;

public class UseEncapsulated {

public static void main(String[] args) {
ExEncapsulated e1=new ExEncapsulated();
e1.setName("xyz");
e1.setAge(5);
ExEncapsulated e2=new ExEncapsulated();
e2.setAge(17);
System.out.println(e2.getAge());
}
}

Step 17:this and super keywords – 11th hour + code

this –

Definition- It’s a keyword which refers to current class.
this keyword is used to-
(1) Refer class variable –

package dis;

public class TisVar{
String name="java";

void show(){
System.out.println(this.name);
}
}

(2) Refer current class constructor-

package dis;

public class TisConst{

public TisConst(){
this(5);
System.out.println("a");
}

public TisConst(int age){
System.out.println("b");
}

public static void main(String[] args){
TisConst k=new TisConst();
}
}

(3) Invoke current class method-

Continue reading “Step 17:this and super keywords – 11th hour + code”

Step 20:String Handling -13th hour + code

String:– Generally string is a sequence of characters, but in java, strings are immutable objects. Immutable simply means  unmodifiable/unchangeable.

Creation of String object:-
1:- By string literal
2:- By new keyword

1:- String literal:-

String s1=”Welcome”; //1 object created
String s2=”Welcome”; //no object will be created

Description:- When a String is created, JVM checks it in string constant pool if it exists it returns reference else string object is submitted to pool.

op1

2:- By new keyword:-

String s=new String(“Welcome”);     //Creates 2 objects
String s1=new String(“Welcome”);   //1 object

Description:- new String object is created in nonpool(heap memory) and literal “Welcome” is created in string constant pool. The variable s is reference variable for object created in nonpool.

output1

Methods of String class– There are many functions which can be performed on string such as concatenation,trim,split,substring etc.

String Buffer & String Builder –

Both classes are used to create mutable strings.

Unlike Strings these are stored in heap memory.

Both classes have same methods, but each method in StringBuffer is synchronized so StringBuffer is thread safe.

StringBuilder is fast in performance.

code samples are here

1-Mostly Used String methods-


package strn;

public class ExStrings {

public static void main(String[] args) {

String nameLiteral = "kuldeep"; // 1 object creation
String nameobject = new String("kaushik"); // 2 object creation

System.out.println(nameLiteral.charAt(4));
System.out.println(nameLiteral.concat(nameobject));

String[] arr1 = nameLiteral.split("d");
System.out.println(arr1[0] + " and " + arr1[1]);

System.out.println(nameLiteral.length());

String fortrim = " deep ";
System.out.println(fortrim);
System.out.println(fortrim.trim());

System.out.println(nameLiteral.equals(nameobject));

System.out.println(nameLiteral.indexOf("e"));

System.out.println(nameLiteral.isEmpty());

System.out.println(nameLiteral.length());

System.out.println(nameLiteral.substring(4));

System.out.println(nameLiteral.substring(2, 5));

System.out.println(nameLiteral.compareTo(nameobject));

System.out.println(nameLiteral.contains("ul"));

System.out.println(nameLiteral.replace("ul", "lu"));

}
}

2-Mostly Used StringBuilder/StringBuffer methods-(both classes have same methods)


package strn;

public class ExStringB {

public static void main(String[] args) {

StringBuilder sb1=new StringBuilder("java");
System.out.println(sb1.reverse());

StringBuffer sb2=new StringBuffer("core");
System.out.println(sb2.reverse());
}
}