0% found this document useful (0 votes)
4 views7 pages

Practical 8 To 11

The document outlines practical exercises for an Advance Java Technology course, focusing on Hibernate and Spring Framework applications. It includes code examples for creating a Customer entity with Hibernate, as well as dependency injection using setter and constructor methods in Spring. Additionally, it provides configuration files for Hibernate and Spring, demonstrating how to set up and utilize these technologies in Java applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Practical 8 To 11

The document outlines practical exercises for an Advance Java Technology course, focusing on Hibernate and Spring Framework applications. It includes code examples for creating a Customer entity with Hibernate, as well as dependency injection using setter and constructor methods in Spring. Additionally, it provides configuration files for Hibernate and Spring, demonstrating how to set up and utilize these technologies in Java applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

FACULTYOFENGINEERINGANDTECHNOLOGY

DepartmentofComputerEngineering
Advance Java Technology (01CE0308)

Practical 8
Hibernate application.

Customer.java (pojo class)

package Lab_8;

import java.io.Serializable;
import javax.persistence.*;

@Entity
@Table(name = "customer1") // Make sure this matches the table in your database
public class Customer implements Serializable {
@Id
@Column(name = "cid")
private int id;

@Column(name = "name")
private String name;

@Column(name = "address")
private String address;

@Column(name = "contact")
private String contact;

// Getters and setters


public int getId() {
return id;
}

public void setId(int id) {


this.id = id;
}

public String getname() {


return name;
}

public void setname(String name) {


this.name = name;
}

public String getaddress() {


return address;
}

public void setaddress(String address) {


this.address = address;
}

92301703105
Batch – 3EC2 - B
FACULTYOFENGINEERINGANDTECHNOLOGY
DepartmentofComputerEngineering
Advance Java Technology (01CE0308)

public String getcontact() {


return contact;
}

public void setcontact(String contact) {


this.contact = contact;
}

@Override
public String toString() {
return "customer{" +
"id=" + id +
", name='" + name + '\'' +
", address='" + address + '\'' +
", contact='" + contact + '\'' +
'}';
}
}

Main.java (Main class)


package Lab_8;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;

public class Main {


public static void main(String[] args) {
try {
// Load the Hibernate configuration file
Configuration confi = new Configuration().configure("hibernate.cfg.xml");

// Build the SessionFactory and open a session


SessionFactory factory = confi.buildSessionFactory();
Session session = factory.openSession();

// Begin a transaction
Transaction tx = session.beginTransaction();

// Create a Customer object and set its properties


Customer c1 = new Customer();
c1.setId(105); // Set a unique ID
c1.setname("Tisha");
c1.setcontact("9524467");
c1.setaddress("Junagadh");

// Save the Customer object to the database


session.save(c1);
tx.commit();
System.out.println("Customer record successfully saved.");

// Fetch and display a Customer object by ID


System.out.println("Fetching Customer record.");
Customer c2 = (Customer) session.get(Customer.class, 105); // Fetching the same customer by ID
92301703105
Batch – 3EC2 - B
FACULTYOFENGINEERINGANDTECHNOLOGY
DepartmentofComputerEngineering
Advance Java Technology (01CE0308)

System.out.println(c2);

// Close the session and factory


session.close();
factory.close();
} catch (HibernateException e) {
System.out.println("Error: " + e.getMessage());
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
customer.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="Lab_8.Customer" table="customer">
<id name="id" column="cid">
<generator class="assigned"/> <!-- 'assigned' because we manually set the ID -->
</id>
<property name="name" column="name"/>
<property name="contact" column="contact"/>
<property name="address" column="address"/>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/customer</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property> <!-- Add your MySQL
password here -->
<property name="hibernate.hbm2ddl.auto">create</property> <!-- Use 'create' to generate
table if not exists -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>

<!-- Mapping the Customer entity -->


<mapping resource="Customer.hbm.xml"/>

92301703105
Batch – 3EC2 - B
FACULTYOFENGINEERINGANDTECHNOLOGY
DepartmentofComputerEngineering
Advance Java Technology (01CE0308)

</session-factory>
</hibernate-configuration>

Output

92301703105
Batch – 3EC2 - B
FACULTYOFENGINEERINGANDTECHNOLOGY
DepartmentofComputerEngineering
Advance Java Technology (01CE0308)

Practical 10
Create a java application to make use of dependency injection using setter method.
Person.java

public class Person {


private String name;

public void setName(String name) {


this.name = name;
}

public void display() {


System.out.println("Name: " + name);
}
}
MainClass.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainClass {


public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Person person = (Person) context.getBean("personBean");
person.display();
}
}
Beans.xml
<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.xsd">

<bean id="personBean" class="Person">


<property name="name" value="" />
</bean>
</beans>

Output

92301703105
Batch – 3EC2 - B
FACULTYOFENGINEERINGANDTECHNOLOGY
DepartmentofComputerEngineering
Advance Java Technology (01CE0308)

Pracatical 11
Create a java application to make use of dependency injection using constructor method.
Parent.java

public class Parent {


private String name;
private String address;
private String contactNumber;

// Constructor for dependency injection


public Parent(String name, String address, String contactNumber) {
this.name = name;
this.address = address;
this.contactNumber = contactNumber;
}

public void displayInfo() {


System.out.println("Name: " + name);
System.out.println("Address: " + address);
System.out.println("Contact Number: " + contactNumber);
}
}
Child.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Child {


public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

Parent parent = (Parent) context.getBean("parentBean");


parent.displayInfo();
}
}
Bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">

<bean id="parentBean" class="Parent">


<constructor-arg value="Tisha"/>
<constructor-arg value="123 Main St, Cityville"/>
<constructor-arg value="1234567890"/>

92301703105
Batch – 3EC2 - B
FACULTYOFENGINEERINGANDTECHNOLOGY
DepartmentofComputerEngineering
Advance Java Technology (01CE0308)

</bean>
</beans>
Output

92301703105
Batch – 3EC2 - B

You might also like