Monday, May 31, 2021

Hibernate one-to-one mapping:

 Hibernate one-to-one mapping:

Person class:

@Entity

@Table(name="person")

public class Person {

private int personId;

private String name;

private PersonDetails personDetails;

@Id //used for primary key 

@GeneratedValue(strategy = GenerationType.IDENTITY) //mysql sequence generation policy

public int getPersonId() {

return personId;

}


public void setPersonId(int personId) {

this.personId = personId;

}


public String getName() {

return name;

}


public void setName(String name) {

this.name = name;

}

        //cascade type meaning what will happen for associated entity if this entity insert,delete ,update,merge or detach

//defining fetch type meaning if we fetch data then we want to fetch associated entity or not  

@OneToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)

@JoinColumn(name = "fk_id")

public PersonDetails getPersonDetails() {

return personDetails;

}

public void setPersonDetails(PersonDetails personDetails) {

this.personDetails = personDetails;

}

}

PersonDetails class:

@Entity

public class PersonDetails {

private int personDetailId;

private int income;

private String job;

private String zipCode;


@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

public int getPersonDetailId() {

return personDetailId;

}


public void setPersonDetailId(int personDetailId) {

this.personDetailId = personDetailId;

}


public int getIncome() {

return income;

}


public void setIncome(int income) {

this.income = income;

}


public String getJob() {

return job;

}


public void setJob(String job) {

this.job = job;

}


public String getZipCode() {

return zipCode;

}


public void setZipCode(String zipCode) {

this.zipCode = zipCode;

}


}

Main class:


public class MainApp {

public static void main(String[] args) {
SessionFactory sessionFactory = (SessionFactory) new Configuration().configure("Hibernate.cfg.xml")
.addAnnotatedClass(Person.class).addAnnotatedClass(PersonDetails.class).buildSessionFactory();

Session session = sessionFactory.getCurrentSession();
try {
Transaction tx = session.beginTransaction();
PersonDetails pdetails = new PersonDetails();
pdetails.setIncome(1000);
pdetails.setJob("engineer");
pdetails.setZipCode("1206");
Person p1 = new Person();
p1.setName("sahabur");
p1.setPersonDetails(pdetails);
session.save(p1);

tx.commit();
}

finally {
session.close();

}

}

}

hibernate.cfg.xml:

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- JDBC Database connection settings -->
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/person_tracker?useSSL=false&amp;serverTimezone=UTC</property>
        <property name="connection.username">root</property>
        <property name="connection.password">lok094026man</property>

        <!-- JDBC connection pool settings ... using built-in test pool -->
        <property name="connection.pool_size">5</property>

        <!-- Select our SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Echo the SQL to stdout -->
        <property name="show_sql">true</property>

<!-- Set the current session context -->
<property name="current_session_context_class">thread</property>
 
    </session-factory>

</hibernate-configuration>




Wednesday, May 26, 2021

How tomcat server handles multiple request?

Ans:A HTTP request is sent by the browser over internet connection and when request comes to tomcat(servlet container) it allocates a thread from thread pool to serve based on URL mapping of servlet. Because tomcat has the URL mapping of all the servlet . thread from thread pool initialize object of servlet ,calls its service method, generate and send response .then thread goes back to the pool ,ready to serve another request from the client.



Saturday, May 22, 2021

How client send request and get response from server in servlet?



What is HTTP Request and HTTP Response?

Ans: both are the interface and implementation provided by servlet server.ex:tomcat.when user want to send any request to a servlet ,he send HttpRequest and can attach data if want. user can send response like html from server to client using HttpResponse.


Fluent interface pattern

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