Monday, 24 May 2021

Lambda Expression in Java 8

Lambda Expression













Lambda calculus was the big change in Mathematical world which came in 1930s.
Because of this programmers also started using it.

LISP is the 1st lang where lambda expressions came first.
Apart form LISP, C, CPP, PYTHON, SCALA, RUBBY, Objective C

Advantages

  1. To enable functional programming in java
  2. To write more readable, maintainable and concise code
  3. To use APIs very easily and effectively.
  4. To enable parallel processing
Lambda Expressionlambda expressions are a means to create anonymous classes of functional interfaces easily
     1. No Name
     2. No Return Type
     3. No Access Modifier

NOTE : Only use in case of functional interface.


Functional Interface : An interface having only one abstract method is known as Functional Interface.
Example : Runnable , Callable etc.

There are some rules to write lambda expression from which two main important rules are
1. If body is having only one statement then { } braces are optional
2. If passing parameter as an argument then argument type is optional

Example.

public void display(String str){
    System.out.println("Hello "+str);
}
Using lambda
(str)->System.out.println("Hello "+str);
or
str->System.out.println("Hello "+str);
int getValue(){
  return 5;
}
Using lambda
() -> 5
// concatenating two strings, it has 2 string parameters and they are concatenated in lambda body
(String s1, String s2) -> s1+s2;


Below are some example codes using lambda

1. Calculator program
























2. Threading



Scenarios

  1. (int x, int y) -> x+y; or (x, y) -> x + y; which one of these is a valid lambda expression?

    Both of them are valid lambda expressions if used in a correct context.


  2. (int x, y) -> x + y; is this a valid lambda expression?

    You can't have lambda expression where type for only one of the parameter is explicitly declared so this lambda expression is invalid.

Read More »

Monday, 17 May 2021

10.0 Collection Framework in Java

Collection Framework

Why there is need of collection framework ?

 Limitation of Array : 

1. Fixed in Size - If we give less size then we can't expand it at run time, If we give more space then unused space will be of no use.

2. They are Homogeneous in type, we can only store similar type of data into an Array.

3. There is underlying data structure behind this as a result of there is no additional supporting methods are available.

Collections are grow able in nature.

Note : Arrays are highly recommended  because of its high performance but memory wise collections are good.


What is Collection ?

Group of individual object as a  single entity.

Java provides several classes and interfaces to achieve this also know as Collection Framework.


There are 9 key Interface




Read More »

Sunday, 21 February 2021

1.4 Java Web Services


 

JAX-RS Annotation

@Path

The @Path annotation is used to specify the URI through which a resource and an API can be accessed. Resource in this case is the REST Web service itself. @Path("/").


@Path("/message")

public class MessageReceiveService {


@Path("/birthday")

    public Response printBdayMessage(){


@GET - If service method is returning data based on client request.

@POST - If you want to store the data

@PUT -If you want to modify the data

@DELETE - If you want to delete 


@QueryParam

public String hello(@QueryParam("name1") String name, @QueryParam("age1") int age){

return "Hello ..."+name+" age="+age;

}

http://wwww.localhost:8080/appname/skeleton/helloService/hello?name1=bikesh&age1=32


@PathParam

 @Path("/helloService")

 public class HelloService{

@Path("sayHello/{name1}/{age}")

public String sayHello(@Pathparam("name1") String name, @QueryParam("age1") int age){

return "Hello ..."+name+" age="+age;

}

 }

 http://localhost:8080/webAppname/rest/helloService/sayHello/venkat/32


@MatrixParam

 It is similar to QueryParam but in URL variables will be seperated by semicoloun.

 

@FormParam

    @POST

    @Path("/add")

    public Response addUser(

        @FormParam("name") String name,

        @FormParam("age") int age) {


        return Response.status(200)

            .entity("addUser is called, name : " + name + ", age : " + age)

            .build();


    }

@Consumes annotation specifies the list of media types consumed by a particular API or class

Examples:

@Consumes("application/json")

@Consumes("application/xml")


@Produces annotation specifies the list of the media types produced by a particular API or class

Examples:

@Produces("application/json")

@Produces("application/xml")

Read More »

Wednesday, 17 February 2021

1.3 Web Services - JAX-WS


 

JAX-WS - It is a specification (API - contain set of interfaces)

It is having some Annotations

NOTE : Recommended approach is create interface and then implementation service class

In interfaces and implementation all three annotations are mandatory

1. @WebService

    This is mandatory annotation in I+C

    Example








Three optional properties are there

serviceName - default value will be - ServiceClassName (It should be unique)

targetNamespace - default value will be - package name in reverse order (It should be unique)

endpointInterface - default value will be - Fully qualified name of interface

2. @WebMethod (Optional)

If you want to give different name for method in wsdl then you can use it otherwise original method name will appear in wsdl file.

3. @WebParam (Optional)

If you want to give different nave for variables in wsdl then you can use it otherwise original variable name will appear in wsdl file.




 CLIENT APPLICATION FOR APACHE CFX



Read More »

Tuesday, 16 February 2021

1.2 Web Services - JAX RPC API - AXIS1 Implementation


NOTE : It doesn't support collection type methods
 

STEPS TO CREATE WEB SERVICE IN ECLIPSE





STEPS TO CREATE CLIENT



Read More »

Monday, 15 February 2021

1.1 WEB SEVICES - SUMMARY


 

What is Web Services - Sharing information between two application on internet is know as web services.

There are other options to share information

1. Socket Programming (Both App should be in java)

2. RMI - Remote Method Invocation (Both App should be in java)

3. EJB - Session beans and entity beans (Both App should be in java)

4. RPC - Remote Procedure Call (Both App should be in CPP)

5. CORBA - Common Object Request Broker Architecture (Multi language app supported)

6. DCOM  - Distributed communication  (Both App should be in .NET)


NOTE : CORBA is the 1st Specification which supported different language app com. support.

But Every implementation provider given their own specification because of which in some cases they fail to share information.


In technical terms Web Services are nothing but specification(Set of interfaces) i.e set of rules.

WSDL - Web Service Definition language

It contains 

1. Service Name

2. Operations

3. Parameters

4. Data Type

5. End point URL

NOTE : If you want to make your web service public you can register it to UDDI

UDDI stands for Universal Description, Discovery, and Integration.


WEB SERVICE ARCHITECTURE


1. WSDL generation tool will generate WSDL document from java service class

2. WSDL registration to UDDI registry to make it available publicly

3. WSDL document received by client

4. Stub class generation tool will create stub classes in respective programming language

5. Client will use stub classes to pass the parameters into request

6. XML request will form using stub classes

7. HTTP protocol will take SOAP request to service API

8. Soap request will be read by skeleton classes 

9. Service operation will get perform 

10. Soap response will sent back to client.

11. Stub classes will read the response from soap response

12. Reading soap response

13. response read

14. response will be given to client


synchronous response returns to the client in the same HTTP connection as the request. With asynchronous responses, a client can send multiple requests and receive the responses in subsequent connections.
















Read More »

Wednesday, 7 March 2018

1.0 WEB SEVICES

Web service is a technology to communicate with different programming languages (Interchangeability)

Web service is a client server application for communication between applications.



There are two types of web services 

  1. SOAP
  2. RESTful
Web Service Components
  1. SOAP (Simple Object Access Protocol) - XML based
  2. WSDL (Web Service Description Language) - WSDL is a xml document containing information about web services such as method name, method parameter and how to access it.
  3. UDDI (Universal Description, Discovery and Integration) - UDDI is a directory of web service interfaces described by WSDL, containing information about web services.


SOAP Web Service

  • It is a XML-based protocol for accessing web services.
  •  It is platform independent and language independent
  • SOAP uses WSDL and doesn't have any other mechanism to discover the service.

RESTful Web Service

  • It stands for representational state transfer
  • RESTful web service permits different data format such as Plain Text, HTML, XML and JSON.



Read More »

Tuesday, 6 March 2018

24.0 Hibernate One-to-One Mapping

One-to-One mapping


Concept of Primary key and Foreign Key








Create the tables in you database



















ONE TO ONE UNI-DIRECTIONAL

Cascade Save Example

--------------------------------------------------------------------------------------------
Instructor.java
--------------------------------------------------------------------------------------------
package com.demo.spring.entitiy;



import javax.persistence.CascadeType;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.JoinColumn;

import javax.persistence.OneToOne;

import javax.persistence.Table;



@Entity

@Table(name="instructor")

public class Instructor {



 @Id

 @GeneratedValue(strategy=GenerationType.IDENTITY)

 @Column(name="id")

 private int id;

 

 @Column(name="first_name")

 private String first_name;

 

 @Column(name="last_name")

 private String last_name;

 

 @Column(name="email")

 private String email;



 @OneToOne(cascade=CascadeType.ALL)

 @JoinColumn(name="instructor_detail_id")

 private InstructorDetail instructorDetail;



 public Instructor(){}



 public Instructor(String first_name, String last_name, String email) {

  super();

  this.first_name = first_name;

  this.last_name = last_name;

  this.email = email;

 }



 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;

 }



 public InstructorDetail getInstructorDetail() {

  return instructorDetail;

 }



 public void setInstructorDetail(InstructorDetail instructorDetail) {

  this.instructorDetail = instructorDetail;

 }



 @Override

 public String toString() {

  return "Instructor [id=" + id + ", first_name=" + first_name

    + ", last_name=" + last_name + ", email=" + email

    + ", instructorDetail=" + instructorDetail + "]";

 }



 

}

--------------------------------------------------------------------------------------------
InstructorDetail.java
--------------------------------------------------------------------------------------------
package com.demo.spring.entitiy;



import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.Table;



@Entity

@Table(name="instructor_detail")

public class InstructorDetail {



 @Id

 @GeneratedValue(strategy=GenerationType.IDENTITY)

 @Column(name="id")

 private int id;

 

 @Column(name="youtube_channel")

 private String youtubeChannel;

 

 @Column(name="hobby")

 private String hobby;

 

 public InstructorDetail(){

  

 }



 public InstructorDetail(String youtubeChannel, String hobby) {

  super();

  this.youtubeChannel = youtubeChannel;

  this.hobby = hobby;

 }



 public int getId() {

  return id;

 }



 public void setId(int id) {

  this.id = id;

 }



 public String getYoutubeChannel() {

  return youtubeChannel;

 }



 public void setYoutubeChannel(String youtubeChannel) {

  this.youtubeChannel = youtubeChannel;

 }



 public String getHobby() {

  return hobby;

 }



 public void setHobby(String hobby) {

  this.hobby = hobby;

 }



 @Override

 public String toString() {

  return "InstructorDetail [id=" + id + ", youtubeChannel="

    + youtubeChannel + ", hobby=" + hobby + "]";

 }

 

}


--------------------------------------------------------------------------------------------
CreateDemo.java
--------------------------------------------------------------------------------------------
package com.demo.spring.test;



import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;



import com.demo.spring.entitiy.Instructor;

import com.demo.spring.entitiy.InstructorDetail;



public class CreateDemo {



 public static void main(String[] args) {

  SessionFactory factory=new Configuration()

  .configure("hibernate.cfg.xml")

  .addAnnotatedClass(Instructor.class)

  .addAnnotatedClass(InstructorDetail.class)

  .buildSessionFactory();

  

  Session session=factory.getCurrentSession();

  

  

  try{

   session=factory.getCurrentSession();

   

   Instructor tempInstructor=new Instructor("Maurya","Sudarshan","sudarshan.maurya@tiwari.com");

   

   InstructorDetail tempInstructorDetail=new InstructorDetail("http://www.sudarshan.maurya.com","Luv 2 sleep"); 

   

   tempInstructor.setInstructorDetail(tempInstructorDetail);

   

   System.out.println("saving : " +tempInstructor);

   session.beginTransaction();

   

   session.save(tempInstructor);

   

   session.getTransaction().commit();

  }

  finally{

   session.close();

  }

 }



}


--------------------------------------------------------------------------------------------
OUTPUT
--------------------------------------------------------------------------------------------
Mar 06, 2018 4:13:33 AM org.hibernate.Version logVersion

INFO: HHH000412: Hibernate Core {5.2.14.Final}

Mar 06, 2018 4:13:33 AM org.hibernate.cfg.Environment <clinit>

INFO: HHH000206: hibernate.properties not found

Mar 06, 2018 4:13:34 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>

INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}

Mar 06, 2018 4:13:34 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure

WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)

Mar 06, 2018 4:13:34 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl 
buildCreator

INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/bikesh?useSSL=false]

Mar 06, 2018 4:13:34 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl 
buildCreator

INFO: HHH10001001: Connection properties: {user=root, password=****}

Mar 06, 2018 4:13:34 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl 
buildCreator

INFO: HHH10001003: Autocommit mode: false

Mar 06, 2018 4:13:34 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl
$PooledConnections <init>

INFO: HHH000115: Hibernate connection pool size: 1 (min=1)

Mar 06, 2018 4:13:34 AM org.hibernate.dialect.Dialect <init>

INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect

saving : Instructor [id=0, first_name=Maurya, last_name=Sudarshan, email=sudarshan.maurya@tiwari.com,
 instructorDetail=InstructorDetail 
[id=0, youtubeChannel=http://www.sudarshan.maurya.com, hobby=Luv 2 sleep]]

Hibernate: insert into instructor_detail (hobby, youtube_channel) values (?, ?)

Hibernate: insert into instructor (email, first_name, instructor_detail_id, last_name) values (?, ?, ?, ?)


Cascade Delete Example

--------------------------------------------------------------------------------------------
DeleteDemo.java
--------------------------------------------------------------------------------------------
package com.demo.spring.test;



import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;



import com.demo.spring.entitiy.Instructor;

import com.demo.spring.entitiy.InstructorDetail;



public class DeleteDemo {



 public static void main(String[] args) {

  SessionFactory factory=new Configuration()

  .configure("hibernate.cfg.xml")

  .addAnnotatedClass(Instructor.class)

  .addAnnotatedClass(InstructorDetail.class)

  .buildSessionFactory();

  

  Session session=factory.getCurrentSession();

  

  

  try{

   session=factory.getCurrentSession();

   

   session.beginTransaction();

   

   int theId=1;

   Instructor tmpInstructor=session.get(Instructor.class, theId);

   

   System.out.println("Found  instuctor : "+ tmpInstructor);

   if(tmpInstructor !=null){

    System.out.println("deleting : " + tmpInstructor);

    session.delete(tmpInstructor);

   }

   

   

   session.getTransaction().commit();

  }

  finally{

   session.close();

  }

 }



}


--------------------------------------------------------------------------------------------
OUTPUT
--------------------------------------------------------------------------------------------
Mar 06, 2018 4:11:08 AM org.hibernate.Version logVersion

INFO: HHH000412: Hibernate Core {5.2.14.Final}

Mar 06, 2018 4:11:08 AM org.hibernate.cfg.Environment <clinit>

INFO: HHH000206: hibernate.properties not found

Mar 06, 2018 4:11:08 AM org.hibernate.annotations.common.reflection.java
.JavaReflectionManager <clinit>

INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}

Mar 06, 2018 4:11:08 AM org.hibernate.engine.jdbc.connections.internal
.DriverManagerConnectionProviderImpl configure

WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)

Mar 06, 2018 4:11:08 AM org.hibernate.engine.jdbc.connections.internal
.DriverManagerConnectionProviderImpl buildCreator

INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL 
[jdbc:mysql://localhost:3306/bikesh?useSSL=false]

Mar 06, 2018 4:11:08 AM org.hibernate.engine.jdbc.connections.internal
.DriverManagerConnectionProviderImpl buildCreator

INFO: HHH10001001: Connection properties: {user=root, password=****}

Mar 06, 2018 4:11:08 AM org.hibernate.engine.jdbc.connections.internal
.DriverManagerConnectionProviderImpl buildCreator

INFO: HHH10001003: Autocommit mode: false

Mar 06, 2018 4:11:08 AM org.hibernate.engine.jdbc.connections.internal
.DriverManagerConnectionProviderImpl$PooledConnections <init>

INFO: HHH000115: Hibernate connection pool size: 1 (min=1)

Mar 06, 2018 4:11:09 AM org.hibernate.dialect.Dialect <init>

INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect

Hibernate: select instructor0_.id as id1_0_0_, instructor0_.email as email2_0_0_, 
instructor0_.first_name as first_na3_0_0_, instructor0_.instructor_detail_id
 as instruct5_0_0_, instructor0_.last_name as last_nam4_0_0_, instructor1_.id
 as id1_1_1_, instructor1_.hobby as hobby2_1_1_, instructor1_.youtube_channel
 as youtube_3_1_1_ from instructor instructor0_ left outer join instructor_detail
 instructor1_ on instructor0_.instructor_detail_id=instructor1_.id where instructor0_.id=?

Found  instuctor : Instructor [id=1, first_name=Bikesh, last_name=Kumar, 
email=bikesh.kumar@tiwari.com, instructorDetail=InstructorDetail [id=1,
 youtubeChannel=http://www.bikesh.kumar.com, hobby=Luv 2 code]]

deleting : Instructor [id=1, first_name=Bikesh, last_name=Kumar, 
email=bikesh.kumar@tiwari.com, instructorDetail=InstructorDetail [id=1,
 youtubeChannel=http://www.bikesh.kumar.com, hobby=Luv 2 code]]

Hibernate: delete from instructor where id=?

Hibernate: delete from instructor_detail where id=?



















ONE TO ONE BI-DIRECTIONAL



Cascade Read Example

--------------------------------------------------------------------------------------------
Instructor.java
--------------------------------------------------------------------------------------------
package com.demo.spring.entitiy;



import javax.persistence.CascadeType;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.JoinColumn;

import javax.persistence.OneToOne;

import javax.persistence.Table;



@Entity

@Table(name="instructor")

public class Instructor {



 @Id

 @GeneratedValue(strategy=GenerationType.IDENTITY)

 @Column(name="id")

 private int id;

 

 @Column(name="first_name")

 private String first_name;

 

 @Column(name="last_name")

 private String last_name;

 

 @Column(name="email")

 private String email;



 @OneToOne(cascade=CascadeType.ALL)

 @JoinColumn(name="instructor_detail_id")

 private InstructorDetail instructorDetail;



 public Instructor(){}



 public Instructor(String first_name, String last_name, String email) {

  super();

  this.first_name = first_name;

  this.last_name = last_name;

  this.email = email;

 }



 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;

 }



 public InstructorDetail getInstructorDetail() {

  return instructorDetail;

 }



 public void setInstructorDetail(InstructorDetail instructorDetail) {

  this.instructorDetail = instructorDetail;

 }



 @Override

 public String toString() {

  return "Instructor [id=" + id + ", first_name=" + first_name

    + ", last_name=" + last_name + ", email=" + email

    + ", instructorDetail=" + instructorDetail + "]";

 }



 


}

--------------------------------------------------------------------------------------------
Instructordetail.java
--------------------------------------------------------------------------------------------
package com.demo.spring.entitiy;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="instructor_detail")
public class InstructorDetail {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="youtube_channel")
private String youtubeChannel;
@Column(name="hobby")
private String hobby;
@OneToOne(mappedBy="instructorDetail",cascade=CascadeType.ALL)
private Instructor instructor;
public Instructor getInstructor() {
return instructor;
}

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

public InstructorDetail(){
}

public InstructorDetail(String youtubeChannel, String hobby) {
super();
this.youtubeChannel = youtubeChannel;
this.hobby = hobby;
}

public int getId() {
return id;
}

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

public String getYoutubeChannel() {
return youtubeChannel;
}

public void setYoutubeChannel(String youtubeChannel) {
this.youtubeChannel = youtubeChannel;
}

public String getHobby() {
return hobby;
}

public void setHobby(String hobby) {
this.hobby = hobby;
}

@Override
public String toString() {
return "InstructorDetail [id=" + id + ", youtubeChannel="
+ youtubeChannel + ", hobby=" + hobby + "]";
}
}

--------------------------------------------------------------------------------------------
GetInstructordetaildemo.java
--------------------------------------------------------------------------------------------
package com.demo.spring.test;

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

import com.demo.spring.entitiy.Instructor;
import com.demo.spring.entitiy.InstructorDetail;

public class GetInstructorDetaildemo {

public static void main(String[] args) {
SessionFactory factory=new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Instructor.class)
.addAnnotatedClass(InstructorDetail.class)
.buildSessionFactory();
Session session=factory.getCurrentSession();
try{
session=factory.getCurrentSession();
session.beginTransaction();
int theId=2;
InstructorDetail tempInstructorDetail=session.get(InstructorDetail.class, theId);
System.out.println("tempInstructorDetail : "+tempInstructorDetail);
System.out.println("the associated instructor : "+tempInstructorDetail.getInstructor());
session.getTransaction().commit();
}
finally{
session.close();
}
}

}


--------------------------------------------------------------------------------------------
OUTPUT
--------------------------------------------------------------------------------------------
Mar 07, 2018 12:53:04 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.14.Final}
Mar 07, 2018 12:53:04 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 07, 2018 12:53:04 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Mar 07, 2018 12:53:04 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Mar 07, 2018 12:53:04 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/bikesh?useSSL=false]
Mar 07, 2018 12:53:04 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Mar 07, 2018 12:53:04 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Mar 07, 2018 12:53:04 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Mar 07, 2018 12:53:05 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Hibernate: select instructor0_.id as id1_1_0_, instructor0_.hobby as hobby2_1_0_, instructor0_.youtube_channel as youtube_3_1_0_, instructor1_.id as id1_0_1_, instructor1_.email as email2_0_1_, instructor1_.first_name as first_na3_0_1_, instructor1_.instructor_detail_id as instruct5_0_1_, instructor1_.last_name as last_nam4_0_1_ from instructor_detail instructor0_ left outer join instructor instructor1_ on instructor0_.id=instructor1_.instructor_detail_id where instructor0_.id=?
tempInstructorDetail : InstructorDetail [id=2, youtubeChannel=http://www.amitesh.kumar.com, hobby=Luv 2 eat]
the associated instructor : Instructor [id=2, first_name=Amitesh, last_name=Kumar, email=amitesh.kumar@tiwari.com, instructorDetail=InstructorDetail [id=2, youtubeChannel=http://www.amitesh.kumar.com, hobby=Luv 2 eat]]




Cascade Delete

--------------------------------------------------------------------------------------------
DeleteinstructorDetailDemo.java
--------------------------------------------------------------------------------------------
package com.demo.spring.test;

import org.hibernate.Session;

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

import com.demo.spring.entitiy.Instructor;

import com.demo.spring.entitiy.InstructorDetail;

public class DeleteInstructorDetailDemo {


public static void main(String[] args) {

SessionFactory factory=new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Instructor.class)
.addAnnotatedClass(InstructorDetail.class)
.buildSessionFactory();

Session session=factory.getCurrentSession();

try{
session=factory.getCurrentSession();
session.beginTransaction();

int theId=2;

InstructorDetail tempInstructordetail=session.get(InstructorDetail.class, theId);
System.out.println("Instructordetail : "+tempInstructordetail);

System.out.println("The associated instructor : "+tempInstructordetail.getInstructor());

System.out.println("Deleting tempInstructorDetail : "+tempInstructordetail);
session.delete(tempInstructordetail);

session.getTransaction().commit();
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
factory.close();
session.close();
}

}



}

--------------------------------------------------------------------------------------------
OUTPUT
--------------------------------------------------------------------------------------------

Mar 07, 2018 2:00:56 AM org.hibernate.Version logVersion

INFO: HHH000412: Hibernate Core {5.2.14.Final}
Mar 07, 2018 2:00:56 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 07, 2018 2:00:56 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Mar 07, 2018 2:00:56 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Mar 07, 2018 2:00:56 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/bikesh?useSSL=false]
Mar 07, 2018 2:00:56 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Mar 07, 2018 2:00:56 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Mar 07, 2018 2:00:56 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Mar 07, 2018 2:00:56 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Hibernate: select instructor0_.id as id1_1_0_, instructor0_.hobby as hobby2_1_0_, instructor0_.youtube_channel as youtube_3_1_0_, instructor1_.id as id1_0_1_, instructor1_.email as email2_0_1_, instructor1_.first_name as first_na3_0_1_, instructor1_.instructor_detail_id as instruct5_0_1_, instructor1_.last_name as last_nam4_0_1_ from instructor_detail instructor0_ left outer join instructor instructor1_ on instructor0_.id=instructor1_.instructor_detail_id where instructor0_.id=?
Instructordetail : InstructorDetail [id=2, youtubeChannel=http://www.amitesh.kumar.com, hobby=Luv 2 eat]
The associated instructor : Instructor [id=2, first_name=Amitesh, last_name=Kumar, email=amitesh.kumar@tiwari.com, instructorDetail=InstructorDetail [id=2, youtubeChannel=http://www.amitesh.kumar.com, hobby=Luv 2 eat]]
Deleting tempInstructorDetail : InstructorDetail [id=2, youtubeChannel=http://www.amitesh.kumar.com, hobby=Luv 2 eat]
Hibernate: delete from instructor where id=?
Hibernate: delete from instructor_detail where id=?
Mar 07, 2018 2:00:58 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop

INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/bikesh?useSSL=false]


















Delete without Cascade

--------------------------------------------------------------------------------------------
InstructorDetail.java
--------------------------------------------------------------------------------------------

package com.demo.spring.entitiy;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="instructor_detail")
public class InstructorDetail {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="youtube_channel")
private String youtubeChannel;
@Column(name="hobby")
private String hobby;
@OneToOne(mappedBy="instructorDetail",cascade={CascadeType.DETACH,CascadeType.MERGE,
CascadeType.PERSIST,CascadeType.PERSIST,CascadeType.REFRESH})
private Instructor instructor;
public Instructor getInstructor() {
return instructor;
}

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

public InstructorDetail(){
}

public InstructorDetail(String youtubeChannel, String hobby) {
super();
this.youtubeChannel = youtubeChannel;
this.hobby = hobby;
}

public int getId() {
return id;
}

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

public String getYoutubeChannel() {
return youtubeChannel;
}

public void setYoutubeChannel(String youtubeChannel) {
this.youtubeChannel = youtubeChannel;
}

public String getHobby() {
return hobby;
}

public void setHobby(String hobby) {
this.hobby = hobby;
}

@Override
public String toString() {
return "InstructorDetail [id=" + id + ", youtubeChannel="
+ youtubeChannel + ", hobby=" + hobby + "]";
}
}

--------------------------------------------------------------------------------------------
DeleteInstructorDetailDemo.java
--------------------------------------------------------------------------------------------
package com.demo.spring.test;

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

import com.demo.spring.entitiy.Instructor;
import com.demo.spring.entitiy.InstructorDetail;

public class DeleteInstructorDetailDemo {

public static void main(String[] args) {
SessionFactory factory=new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Instructor.class)
.addAnnotatedClass(InstructorDetail.class)
.buildSessionFactory();
Session session=factory.getCurrentSession();
try{
session=factory.getCurrentSession();
session.beginTransaction();
int theId=6;
InstructorDetail tempInstructordetail=session.get(InstructorDetail.class, theId);
System.out.println("Instructordetail : "+tempInstructordetail);
tempInstructordetail.getInstructor().setInstructorDetail(null);;
System.out.println("Deleting tempInstructorDetail : "+tempInstructordetail);
session.delete(tempInstructordetail);
session.getTransaction().commit();
}
catch(Exception e)
{
e.printStackTrace();
}
finally{

session.close();
}

}

}



--------------------------------------------------------------------------------------------
OUTPUT
--------------------------------------------------------------------------------------------
Mar 07, 2018 2:19:51 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.14.Final}
Mar 07, 2018 2:19:51 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 07, 2018 2:19:52 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Mar 07, 2018 2:19:52 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Mar 07, 2018 2:19:52 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/bikesh?useSSL=false]
Mar 07, 2018 2:19:52 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Mar 07, 2018 2:19:52 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Mar 07, 2018 2:19:52 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Mar 07, 2018 2:19:52 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Hibernate: select instructor0_.id as id1_1_0_, instructor0_.hobby as hobby2_1_0_, instructor0_.youtube_channel as youtube_3_1_0_, instructor1_.id as id1_0_1_, instructor1_.email as email2_0_1_, instructor1_.first_name as first_na3_0_1_, instructor1_.instructor_detail_id as instruct5_0_1_, instructor1_.last_name as last_nam4_0_1_ from instructor_detail instructor0_ left outer join instructor instructor1_ on instructor0_.id=instructor1_.instructor_detail_id where instructor0_.id=?
Instructordetail : InstructorDetail [id=6, youtubeChannel=http://www.sudarshan.maurya.com, hobby=Luv 2 sleep]
Deleting tempInstructorDetail : InstructorDetail [id=6, youtubeChannel=http://www.sudarshan.maurya.com, hobby=Luv 2 sleep]
Hibernate: update instructor set email=?, first_name=?, instructor_detail_id=?, last_name=? where id=?
Hibernate: delete from instructor_detail where id=?


















After delete


Read More »