Java Framework
Java Framework
Class: TYCS C
Roll no: 9179
PRACTICAL 1
AIM : Demonstration to create First Hibernate Application using Maven Project
Define the stepwise demonstration to create a new application in Eclipse which should be
HIbernate application by using Maven Project
Steps:
Step 3. This should open a page where you can choose the location of the file to be saved ,
choose your files location and click on Next .
Step 4. On the next page archetype will be pre selected by the software , click on the Next
Button.
Step 5. On the next step you have to set up Group ID as “[Link]” and Artifact ID as “your
name” . Here your package name will automatically become a combination of Group ID &
Artifact ID .
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
Step 6. Click on Finish , this should begin the process of creating your maven project. Step 7.
Console will show a message for which conformation has to be given for your project by
entering “Y” character and Enter key . project will be created and will appear in the project
explorer.
Step 8. Project folder will have the following predefined folders in it.
● src/main/java this folder contains packages created for current project
● Inside the package it will have a predefined utility class
● Maven dependency folder contains all the packages and jar files required for the current
project.
● [Link] This contains the dependencies tag which allows the developers to add new
required dependencies.
PRACTICAL 2
AIM : Demonstrates how to establish a connection with a database using Hibernate
SessionFactory.
Source code:
[Link] :
<groupId>[Link]</groupId>
<artifactId>vansh9179</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>vansh9179</name>
<properties>
<[Link]>UTF-8</[Link]>
<[Link]>17</[Link]>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>junit-bom</artifactId>
<version>5.11.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to
parent pom) -->
<plugins>
<!-- clean lifecycle, see
[Link] -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see
[Link]
ng -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.4.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.2</version>
</plugin>
<!-- site lifecycle, see
[Link] -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.12.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.6.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
[Link] :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
"[Link]
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">[Link]</property>
<property name="[Link]">jdbc:mysql://localhost:3306/9179</property>
<property name="[Link]">root</property>
<property name="[Link]">12345</property>
<property name="dialect">[Link].MySQL8Dialect</property>
<property name="[Link]">update</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
[Link] :
package [Link].m65;
import [Link];
import [Link];
import [Link];
public class AppTest {
public static void main(String[] args) {
[Link]("Main method started");
SessionFactory factory = new Configuration().configure("[Link]")
.buildSessionFactory();
// Open session
Session session = [Link]();
[Link]("Session started");
// Close session and factory
[Link]();
[Link]("Session closed");
[Link]();
[Link]("SessionFactory closed");
}
}
OUTPUT :
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
PRACTICAL 3
AIM : Design simple application using hibernate to show the implementation of XML
Mapping. (Create Artist Class with Artist_Id as PK ,Artist_Name , Artist_Genre)
Source code:
[Link]
public Artist() {}
public Artist(String artistName, String artistGenre) {
[Link] = artistName;
[Link] = artistGenre;
}
public int getArtistId() {
return artistId;
}
private void setArtistId(int artistId) {
[Link] = artistId;
}
public String getArtistName() {
return artistName;
}
public void setArtistName(String artistName) {
[Link] = artistName;
}
public String getArtistGenre() {
return artistGenre;
}
public void setArtistGenre(String artistGenre) {
[Link] = artistGenre;
}
}
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
[Link]
[Link]
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">[Link]</property>
<property
name="[Link]">jdbc:mysql://localhost:3306/artist9179?useSSL=false&serverTimez
one=UTC</property>
<property name="[Link]">root</property>
<property name="[Link]">12345</property>
<property name="connection.pool_size">10</property>
<property name="dialect">[Link].MySQL8Dialect</property>
<property name="[Link]">update</property>
<property name="show_sql">true</property>
<mapping resource="[Link]"/>
</session-factory>
</hibernate-configuration>
[Link]
import [Link];
import [Link];
import [Link];
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
try {
Artist artist = new Artist("Sabrina Carpenter", "Pop");
Artist artist1 = new Artist("The Weeknd", "Rock");
[Link]();
[Link](artist);
[Link](artist1);
[Link]().commit();
[Link]("Artist saved successfully!");
} finally {
[Link]();
}
}
}
[Link]
<groupId>[Link]</groupId>
<artifactId>vansh9179</artifactId>
<version>0.0.1-SNAPSHOT</version>
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
<name>vansh9179</name>
<!-- FIXME change it to the project's website -->
<url>[Link]
<properties> <[Link]>UTF-8</[Link]>
<[Link]>17</[Link]>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>junit-bom</artifactId>
<version>5.11.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<groupId>[Link]</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.2.0</version>
</dependency>
<!-- Optionally: parameterized tests support -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
OUTPUT:
PRACTICAL 4 Name: Vansh Singh
Class: TYCS C
Roll no: 9179
Source code:
[Link]
import [Link].*;
@Entity
@Table(name = "Artist")
public class Artist4 {
@Id
@GeneratedValue(strategy = [Link])
private int id;
@Column(name = "name", nullable = false, length = 50)
private String name;
public Artist4() {}
[Link]
import [Link];
import [Link];
import [Link];
try {
Artist4 artist = new Artist4("Sabrina Carpenter", "Manchild");
Artist4 artist1 = new Artist4("The Weeknd", "Starboy");
[Link]();
[Link](artist);
[Link](artist1);
[Link]().commit();
[Link]("Artist saved successfully!");
} finally {
[Link]();
}
}
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
[Link]
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">[Link]</property>
<property
name="[Link]">jdbc:mysql://localhost:3306/artist9179?useSSL=false&serverTimez
one=UTC</property>
<property name="[Link]">root</property>
<property name="[Link]">12345</property>
<property name="connection.pool_size">10</property>
<property name="dialect">[Link].MySQL8Dialect</property>
<property name="[Link]">update</property>
<property name="show_sql">true</property>
<mapping class="Artist4"/>
</session-factory>
</hibernate-configuration>
[Link]
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>vansh9179</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>vansh9179</name>
<properties>
<[Link]>UTF-8</[Link]>
<[Link]>17</[Link]>
</properties>
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
OUTPUT :
[Link]
import [Link].*;
@Entity
@Table(name = "Admission9179")
public class Admission {
@Id
@GeneratedValue(strategy = [Link])
private int admissionId;
public Admission() {}
[Link]
import [Link];
import [Link];
import [Link];
// Create session
Session session = [Link]();
try {
Admission admission = new Admission("Meenakshi", "Kharghar");
Admission admission1 = new Admission("Vaishnav", "Dombivali");
[Link]();
[Link](admission);
[Link](admission1);
[Link]().commit();
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
[Link]
<hibernate-configuration>
<session-factory> <property
name="connection.driver_class">[Link]</property> <property
name="[Link]">jdbc:mysql://localhost:3306/admission1?useSSL=false&serverTim
ezone=UTC</property>
<property name="[Link]">root</property>
<property name="[Link]">12345</property>
<property name="connection.pool_size">10</property>
<property name="dialect">[Link].MySQL8Dialect</property>
<property name="[Link]">update</property>
<property name="show_sql">true</property>
<mapping class="Admission"/>
</session-factory>
</hibernate-configuration>
[Link]
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
<artifactId>vansh9179</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>vansh9179</name>
<properties>
<[Link]>UTF-8</[Link]>
<[Link]>17</[Link]>
</properties>
OUTPUT :
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
PRACTICAL - 5
Source Code :
[Link]
import [Link].*;
@Entity
@Table(name = "Artist9143")
public class Artist {
@Id
@GeneratedValue(strategy = [Link])
private int id;
public Artist() {}
@Override
public String toString() {
return "Artist [id=" + id + ", name=" + name + ", albumName=" +
albumName + "]"; } }
[Link]
<hibernate-configuration>
<session-factory>
<property
name="connection.driver_class">[Link]</property>
<property
name="[Link]">jdbc:mysql://localhost:3306/artist?useSSL=false&server
Timezone= UTC</property>
<property name="[Link]">root</property>
<property name="[Link]">12345</property>
<property name="connection.pool_size">10</property>
<property
name="dialect">[Link].MySQL8Dialect</property>
<property name="[Link]">update</property>
<property name="show_sql">true</property>
<mapping class="Artist"/>
</session-factory>
</hibernate-configuration>
[Link]
import [Link];
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
import [Link];
import [Link];
// 1. Transient state
Artist artist = new Artist("The Weeknd", "Timeless");
[Link]("1. Transient state: Created artist object: " + artist);
Session session = [Link]();
[Link]();
// 2. Persistent state
[Link](artist);
[Link]("2. Persistent state: Artist saved, assigned ID = " + [Link]());
[Link]().commit();
[Link]();
[Link]("Transaction committed and session closed. Artist is now detached.");
// 3. Detached state
[Link]("The Weeknd Detached");
[Link]("3. Detached state: Modified detached artist
name to: " + [Link]());
session = [Link]();
[Link]();
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
// 5. Removed state
[Link](artist);
[Link]
<groupId>[Link]</groupId>
<artifactId>anoushka9143</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>anoushka9143</name>
<!-- FIXME change it to the project's website -->
<url>[Link]
<properties>
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
<[Link]>UTF-8</[Link]
Encoding>
<[Link]>17</[Link]>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>junit-bom</artifactId>
<version>5.11.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<artifactId>mysql-connector-j</artifactId>
<version>8.2.0</version>
</dependency>
OUTPUT :
2. Create a hibernate application to demonstrate the hibernate lifecycle using xml
[Link]
public Admission() {}
@Override
public String toString() {
return "admission{id=" + admissionid + ", studentName='" +
studentName + "'}"; } }
[Link]
<hibernate-mapping>
<class name="Admission" table="admission9143">
<id name="admissionId" column="id">
<generator class="identity"/>
</id>
<property name="studentName" column="student_name"/>
</class>
</hibernate-mapping>
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
// 3. Detached state
[Link]("3. Detached state: " + [Link]());
[Link]();
[Link]();
[Link]("Transaction committed and session closed after
deletion."); [Link]();
[Link]("SessionFactory closed.");
}
}
[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>anoushka9143</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>anoushka9143</name>
<!-- FIXME change it to the project's website -->
<url>[Link]
<properties>
<[Link]>UTF-8</[Link]
Encoding>
<[Link]>17</[Link]>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>junit-bom</artifactId>
<version>5.11.0</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
OUTPUT :
Source Code :
[Link] :
package [Link].anoushka9143;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Entity
@Table(name = "Artist9143")
public class Artist9143 {
@Id
@GeneratedValue(strategy = [Link])
private int id;
public Artist9143) {}
@Override
public String toString() {
return "Artist [id=" + id + ", name=" + artistName + ", albumName=" +
albumName + "]"; }
}
[Link] :
package [Link].anoushka9143;
import [Link];
import [Link];
import [Link];
import [Link];
// 1. Transient state
Artist9143 artist = new Artist9143();
[Link](artistName);
[Link](albumName);
// 2. Persistent state
[Link](artist);
[Link]("2. Persistent state: Artist saved, assigned ID = " + [Link]());
[Link]().commit();
[Link]();
[Link]("Transaction committed and session closed. Artist is now detached.");
// 3. Detached state
[Link](artistName + " Detached");
[Link](albumName + " Final");
[Link]("3. Detached state: Modified detached
artist: Name = " + [Link]() + ", Album = " +
[Link]());
session = [Link]();
[Link]();
// 5. Removed state
[Link](artist);
[Link]("5. Removed state: Artist marked for deletion.");
[Link]().commit();
[Link]();
[Link]("Transaction committed and session closed after deletion.");
[Link]();
[Link]("SessionFactory closed.");
[Link]();
}
}
[Link] :
<!-- Hibernate Core -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>hibernate-core</artifactId>
<version>[Link]</version>
</dependency>
<artifactId>[Link]-api</artifactId>
<version>2.2</version>
</dependency>
[Link] :
<hibernate-configuration>
<session-factory>
<property
name="connection.driver_class">[Link]</property
> <property
name="[Link]">jdbc:mysql://localhost:3306/9143?useSSL=false&serverTimezon
e=UTC</p roperty>
<property name="[Link]">root</property>
<property name="[Link]">12345</property>
<property name="connection.pool_size">10</property>
<property name="dialect">[Link].MySQL8Dialect</property>
<property name="[Link]">update</property>
<property name="show_sql">true</property>
<mapping class="[Link].anoushka9143.Artist9143"/>
</session-factory>
</hibernate-configuration>
OUTPUT :
2. Create a hibernate application to save the Admission class details entered by
user (Id,
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
Source Code :
[Link] :
package [Link].anoushka9143;
@Override
public String toString() {
return "Admission9143{id=" + id + ", studentName='" +
studentName + "'}"; }
}
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
[Link] :
package [Link].anoushka9143;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]();
[Link]();
}
}
[Link] :
PRACTICAL - 7
1. Create a hibernate application to demonstrate Table per class mapping using annotation
Source Code :
[Link]
package [Link].anoushka9143;
import [Link].*;
@Entity
@Table(name = "student9143")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Id
@GeneratedValue(strategy = [Link])
@Column(name = "name")
public Student9143() {}
[Link] = name;
[Link]
package [Link].anoushka9143;
import [Link].*;
@Entity
@DiscriminatorValue("Undergraduate")
@Column(name = "year")
private int year;
public UndergraduateStudent9143() {}
super(name);
[Link] = year;
[Link]
package [Link].anoushka9143;
import [Link].*;
@Entity
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
@DiscriminatorValue("Graduate")
@Column(name = "research_topic")
super(name);
[Link] = researchTopic;
[Link]
package [Link].anoushka9143;
import [Link];
import [Link];
import [Link];
import [Link];
.configure("[Link]")
.addAnnotatedClass([Link])
.addAnnotatedClass([Link])
.addAnnotatedClass([Link])
.buildSessionFactory();
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
try {
[Link]();
[Link](ugStudent);
[Link]()); [Link](gradStudent);
[Link]()); [Link]().commit();
[Link]();
} finally {
[Link]();
[Link]();
}
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
[Link]
<hibernate-configuration>
<session-factory>
<mapping class="[Link].anoushka9143.Student9143"/><mapping
class="[Link].anoushka9143UndergraduateStudent9143"/>
<mapping class="[Link].anoushka9143.GraduateStudent9143"/>
</session-factory>
</hibernate-configuration>
OUTPUT :
2. Create a hibernate application to demonstrateTable Per Concrete class
mapping using annotation.
Source Code :
[Link]
package [Link].anoushka9143;
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
import [Link].*;
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Student9143 {
@Id
@GeneratedValue(strategy = [Link])
private int id;
public Student9143() {}
[Link]
package [Link].anoushka9143;
import [Link].*;
@Entity
@Table
public class UndergraduateStudent9143 extends Student9143 {
@Column(name = "year")
private int year;
public UndergraduateStudent9143() {}
[Link]
package [Link].anoushka9143;
import [Link].*;
@Entity
@Table
public class GraduateStudent9143 extends Student9143 {
@Column(name = "research_topic")
private String researchTopic;
public GraduateStudent9143() {}
[Link]
<hibernate-configuration>
<session-factory>
<mapping
class="[Link].anoushka9143.UndergraduateStudent9143"/>
<mapping
class="[Link].anoushka9143.GraduateStudent9143"/>
</session-factory>
</hibernate-configuration>
[Link]
package [Link].anoushka9143;
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
import [Link];
import [Link];
import [Link];
import [Link];
try {
// Undergraduate Student
[Link]("Enter Undergraduate Student Name: ");String ugName =
[Link](); [Link]("Enter Year: ");
int year = [Link]();
[Link](); // consume newline
// Graduate Student 1
[Link]("Enter Graduate Student 1 Name: ");
String gradName1 = [Link]();
[Link]("Enter Research Topic for Graduate Student 1: ");
String topic1 = [Link]();
// Graduate Student 2
[Link]("Enter Graduate Student 2 Name: ");
String gradName2 = [Link]();
[Link]("Enter Research Topic for Graduate Student 2: ");
String topic2 = [Link]();
[Link](ugStudent);
[Link]("Inserted Undergraduate Student: " +
[Link]()); [Link](gradStudent1);
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
OUTPUT :
SOURCE CODE :
1. [Link]
package [Link].anoushka9143;
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
import [Link].*;
@Entity
@Inheritance(strategy = [Link])
@Table(name = "employee9143")
public class Employee9143 {
@Id
@GeneratedValue(strategy = [Link])
private int id;
@Column
private String name;
public Employee9143() {}
2. [Link]
package [Link].anoushka9143;
import [Link].*;
@Entity
@Table(name = "regular_employee9143")
@PrimaryKeyJoinColumn(name = "emp_id")
public class RegularEmployee9143 extends Employee9143 {
public RegularEmployee9143() {}
3. [Link]
package [Link].anoushka9143;
import [Link].*;
@Entity
@Table(name = "contract_employee9143")
@PrimaryKeyJoinColumn(name = "emp_id")
public class ContractEmployee9143 extends Employee9143 {
public ContractEmployee9143() {}
<hibernate-configuration>
<session-factory>
<mapping class="[Link].anoushka9143.Employee9143"/>
<mapping class="[Link].anoushka9143.RegularEmployee9143"/>
<mapping class="[Link].anoushka9143.ContractEmployee9143"/>
</session-factory>
</hibernate-configuration>
5. [Link]
package [Link].anoushka9143;
import [Link];
import [Link];
import [Link];
import [Link];
try {
[Link]("Enter Regular Employee Name: ");
String regName = [Link]();
[Link]("Enter Salary: ");
float salary = [Link]();
[Link]("Enter Bonus: ");
int bonus = [Link]();
[Link](); // clear buffer
[Link]();
[Link](regEmp);
[Link]("Inserted Regular Employee: " + [Link]());
[Link](conEmp);
[Link]("Inserted Contract Employee: " + [Link]());
[Link]().commit();
[Link]("All employees inserted successfully!");
} finally {
[Link]();
[Link]();
}
}
}
OUTPUT:
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
PRACTICAL - 8
Aim : List and Map Collection Mapping by Annotation
@Entity
@Table(name = "department9143")
public class Department9143 {
@Id
@GeneratedValue(strategy = [Link])
private int id;
public Department9143() {}
[Link]
package [Link].anoushka9143;
import [Link].*;
@Entity
@Table(name = "student9143")
public class Student9143 {
@Id
@GeneratedValue(strategy = [Link])
private int id;
private String name;
@ManyToOne
@JoinColumn(name = "department_id")
private Department9143 department;
public Student9143() {}
[Link]
package [Link].anoushka9143;
import [Link];
import [Link];
import [Link];
import [Link];
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
import [Link];
[Link](s1);
[Link](s2);
[Link](department);
[Link]();
[Link]();
[Link]();
[Link]();
}
}
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
[Link]
<hibernate-configuration>
<session-factory>
<mapping class="[Link].anoushka9143.Department9143"/>
<mapping class="[Link].anoushka9143.Student9143"/>
</session-factory>
</hibernate-configuration>
OUTPUT :
[Link]
package [Link].anoushka9143;
import [Link].*;
@Entity
@Table(name = "department9143")
public class Department9143 {
@Id
@GeneratedValue(strategy = [Link])
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
public Department9143() {}
public Department9143(String name) {
[Link] = name;
}
public int getId() { return id; }
[Link]
package [Link].anoushka9143;
import [Link].*;
@Entity
@Table(name = "student9143")
public class Student9143 {
@Id
@GeneratedValue(strategy = [Link])
private int id;
@ManyToOne(cascade = [Link])
@JoinColumn(name = "department_id")
private Department9143 department;
public Student9143() {}
[Link]
package [Link].anoushka9143;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Transaction tx = [Link]();
[Link](s1);
[Link](s2);
[Link]();
[Link]("\nInserted Department:");
[Link]("Department ID: " + [Link]());
[Link]("Department Name: " + [Link]());
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
[Link]("Students:");
[Link](" - Student ID: " + [Link]() + ", Name: " + [Link]() +
", Department ID: " + ([Link]() != null ? [Link]().getId() :
null)); [Link](" - Student ID: " + [Link]() + ", Name: " +
[Link]() + ", Department ID: " + ([Link]() != null ?
[Link]().getId() : null));
[Link]();
[Link]();
[Link]();
}
}
[Link]
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
<session-factory>
<!-- JDBC connection properties -->
<property
name="connection.driver_class">[Link]</property
> <property
name="[Link]">jdbc:mysql://localhost:3306/m9143?useSSL=false&serverTimez
one=UTC< /property>
<property name="[Link]">root</property>
<property name="[Link]">12345</property>
<property name="connection.pool_size">10</property>
<property name="dialect">[Link].MySQL8Dialect</property>
<property name="[Link]">update</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping class="[Link].anoushka9143.Department9143"/>
<mapping class="[Link].anoushka9143.Student9143"/>
</session-factory>
</hibernate-configuration>
OUTPUT :
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
3. Demonstrate the Map collection One-To-One Mapping
[Link]
package [Link].anoushka9143;
import [Link].*;
@Entity
@Table(name = "user9143")
public class User9143 {
@Id
@GeneratedValue(strategy = [Link])
public User9143() {}
[Link]
package [Link].anoushka9143;
import [Link].*;
import [Link];
import [Link];
@Entity
@Table(name = "user_preferences9143")
@Id
@GeneratedValue(strategy = [Link])
private int id;
@ElementCollection
@CollectionTable(
name = "user_preferences_map9143",
joinColumns = @JoinColumn(name = "user_preferences_id")
)
@MapKeyColumn(name = "preference_type")
@Column(name = "preference_value")
private Map<String, String> preferences = new HashMap<>();
public UserPreferences9143() {}
[Link]
package [Link].anoushka9143;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link]([Link]);
[Link](preferences);
[Link](user);
[Link]();
[Link]();
[Link]();
[Link]();
[Link]
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
<session-factory>
<mapping class="[Link].anoushka9143.User9143"/>
<mapping class="[Link].anoushka9143.UserPreferences9143"/>
</session-factory>
</hibernate-configuration>
OUTPUT :
import [Link];
import [Link];
import [Link];
import [Link];
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
import [Link];
[Link](project);
[Link]();
[Link]();
[Link]();
[Link]();
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
[Link]
package [Link].anoushka9143;
import [Link].*;
@Entity
@Table(name = "employee9143")
public class Employee9143 {
@Id
@GeneratedValue(strategy = [Link])
private Integer id;
public Employee9143() {}
[Link]
package [Link].anoushka9143;
import [Link].*;
import [Link];
import [Link];
@Entity
@Table(name = "project")
public class Project9143 {
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
@Id
@GeneratedValue(strategy = [Link])
private Integer id;
@ElementCollection
@CollectionTable(
name = "project_employee_role",
joinColumns = @JoinColumn(name = "project_id")
)
@MapKeyJoinColumn(name = "employee_id")
@Column(name = "role")
private Map<Employee9143, String> employeeRoles = new
[Link]
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
<session-factory>
<!-- JDBC connection properties -->
<mapping class="[Link].anoushka9143.Employee9143"/>
<mapping class="[Link].anoushka9143.Project9143"/>
</session-factory>
</hibernate-configuration>
OUTPUT :
Name: Vansh Singh
Class: TYCS C
Roll no: 9179
Vansh Singh
TYCS ‘C’
9179
Practical 9
Aim: Retrieve data by using HQL.a Student and Department table
with a Many-to-One mapping.
[Link] all students.
Code:
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class App {
public static void main(String[] args) {
SessionFactory factory = new
Configuration().configure().buildSessionFactory(); Session session =
[Link]();
[Link]();
String hql = "FROM Student";
List<Student> students = [Link](hql, [Link]).list(); for
(Student s : students) {
[Link]("ID : " + [Link]());
[Link]("Name : " + [Link]());
[Link]("Age : " + [Link]());
[Link]("Email : " + [Link]());
[Link]("Course : " + [Link]());
[Link]("----------------------------------");
}
[Link]().commit();
[Link]();
[Link]();
}
}
[Link]
package [Link].practical9;
import [Link].*;
@Entity
public class Department {
@Id
@GeneratedValue(strategy = [Link])
private int id;
private String name;
public Department() {}
public Department(String name) {
[Link] = name;
}
public int getId() {
VanshSingh
TYCS ‘C’
9179
return id;
}
public void setId(int id){
[Link]= id;
}
public String getName(){
return name;
}
public void setName(String name){
[Link]= name;
}
}
[Link]
package [Link].practical9;
import [Link].*;
@Entity
public class Student{
@Id
@GeneratedValue(strategy= [Link])
private int id;
private String name;
private int age;
@ManyToOne
@JoinColumn(name= "department_id")
private Department department;
public Student() {}
public Student(String name, int age, Department department){
[Link]= name;
[Link]= age;
[Link]= department;
}
public int getId(){
return id;
}
public void setId(int id){
[Link]= id;
}
public String getName(){
return name;
}
public void setName(String name){
[Link]= name;
}
public int getAge(){
return age;
}
public void setAge(int age){
[Link]= age;
Vansh Singh
TYCS ‘C’
9179
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
[Link] = department;
}
}
[Link])
<mapping class="Department"/>
<mapping class="Student"/>
Output:
2.
Get students from a specific department name (e.g., "Computer
Science") Code;
[Link]
package [Link].practical9;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class App {
public static void main(String[] args) {
SessionFactory factory = new Configuration()
.configure("[Link]")
.addAnnotatedClass([Link])
.addAnnotatedClass([Link])
.buildSessionFactory();
Session session = [Link]();
[Link]();
Query<Student> query = [Link](
"FROM Student s WHERE [Link] = :deptName",
[Link]); [Link]("deptName", "CS");
List<Student> students = [Link]();
for (Student s : students) {
[Link]("Name : " + [Link]());
Vansh Singh
TYCS ‘C’
9179
[Link]("Age : " + [Link]());
[Link]("Department: " + [Link]().getName());
[Link]("----------------------------------");
}
[Link]().commit();
[Link]();
[Link]();
}
}
Output:
Vansh Singh
TYCS ‘C’
9179
Vansh Singh
TYCS ‘C’
9179
8. Delete a student by ID
[Link]
package [Link].practical9;
import [Link];
import [Link]; // Make sure this is
imported import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class App {
public static void main(String[] args) {
SessionFactory factory = new Configuration()
.configure("[Link]")
.addAnnotatedClass([Link])
.addAnnotatedClass([Link])
.buildSessionFactory();
Session session = [Link]();
Transaction tx = [Link]();
Query query = [Link]("DELETE FROM Student s WHERE [Link] =
:id"); [Link]("id", 3);
int rows = [Link]();
[Link]();
[Link]();
[Link]();
[Link]("Deleted Rows: " + rows);
}
}
Output:
Vansh Singh
TYCS ‘C’
9179
import [Link];
import [Link];
public class App {
public static void main(String[] args) {
SessionFactory factory = new Configuration()
.configure("[Link]")
.addAnnotatedClass([Link])
.addAnnotatedClass([Link])
.buildSessionFactory();
Session session = [Link]();
Transaction tx = [Link]();
Query<String> query = [Link](
"SELECT [Link] FROM Student s JOIN [Link] d WHERE [Link] =
:deptName",
[Link]
);
[Link]("deptName", "Science");
List<String> studentNames = [Link]();
for (String s : studentNames) {
[Link]("Student: " + s);
}
[Link]();
[Link]();
[Link]();
}
}
Output:
Vansh Singh
TYCS “C’
9179
PRACTICAL-10
AIM : Implementation of HCQL with Restrictions class ,Order class
name email salary address
1 n alice@[Link] 55000.00 New York
Common Code:
[Link] package
[Link].muskan9046;
import [Link].*;
@Entity
@Table(name = "Employee") public
class Employee {
@Id private int id;
private String name;
private String email;
private Double salary;
private String address;
public Employee() {
}
public Employee(int id, String name, String email, Double salary, String
address) { [Link] = id; [Link] = name; [Link] = email; [Link] =
salary; [Link] = address;
} public int
getId() { return
id;
</hibernate-configuration>
1. Find all employees whose salary is greater than x(x= user input)
Code: [Link]
package [Link].muskan9046; import
[Link]; import
[Link]; import
[Link]; import
[Link]; import
[Link]; import
[Link]; import
[Link]; import [Link]; public
class App {
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration()
.configure("[Link]")
.addAnnotatedClass([Link])
.buildSessionFactory();
Session session = [Link]();
try (Scanner scanner = new Scanner([Link])) {
[Link]("Enter salary x: "); double x =
[Link]();
[Link]();
CriteriaBuilder builder = [Link]();
CriteriaQuery<Employee> query = [Link]([Link]);
Root<Employee> root = [Link]([Link]);
[Link](root).where([Link]([Link]("salary"), x));
List<Employee> employees = [Link](query).getResultList();
[Link]().commit(); if ([Link]()) {
[Link]("No employees found with salary greater than " + x); }
else { [Link]("Employees with salary greater than " + x +
":"); for
(Employee emp : employees) {
[Link]("ID: " + [Link]());
[Link]("Name: " + [Link]());
[Link]("Email: " + ([Link]() != null ? [Link]() :
"NULL")); [Link]("Salary: " + [Link]());
[Link]("Address: " + [Link]());
[Link]("------------------------------");
Vansh Singh
TYCS “C’
9179
}
}
} catch (Exception e) {
[Link](); }
finally {
[Link]();
[Link]();
}
}
}
OUTPUT:
.buildSessionFactory();
Session session = [Link]();
try {
[Link]();
Criteria criteria = [Link]([Link]);
[Link]([Link]("address", [Link]("New York",
"Chicago"))); List<Employee> employees = [Link]();
[Link]().commit(); if ([Link]()) {
[Link]("No employees found living in New York or Chicago.");
} else {
[Link]("Employees living in New York or Chicago:"); for
(Employee emp : employees) {
[Link]("ID: " + [Link]());
[Link]("Name: " + [Link]());
[Link]("Email: " + ([Link]() != null ? [Link]() :
"NULL"));
[Link]("Salary: " + [Link]());
[Link]("Address: " + [Link]());
[Link]("------------------------------");
[Link]();
[Link]();
}
} }
} catch (Exception e) {
[Link](); }
}
Vansh Singh
TYCS “C’
9179
OUTPUT:
3) Get employees whose salary is between x and y.(x and y take it from user)
[Link]
package [Link]. muskan9046;
import [Link]; import
[Link]; import
[Link]; import
[Link]; import
[Link]; import
[Link]; import [Link];
public class App {
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration()
Vansh Singh
TYCS “C’
9179
.configure("[Link]")
.addAnnotatedClass([Link])
.buildSessionFactory();
Session session = [Link]();
try (Scanner scanner = new Scanner([Link])) {
[Link]("Enter lower salary limit (x): ");
double x = [Link]();
[Link]("Enter upper salary limit (y): ");
double y = [Link]();
[Link]();
Criteria criteria = [Link]([Link]);
[Link]([Link]("salary", x, y));
List<Employee> employees = [Link]();
[Link]().commit(); if
([Link]()) {
[Link]("No employees found with salary between " + x + " and " + y); } else
{
[Link]("Employees with salary between " + x + " and " + y + ":"); for
(Employee emp : employees) {
[Link]("ID: " + [Link]());
[Link]("Name: " + [Link]());
[Link]("Email: " + ([Link]() != null ? [Link]() :
"NULL"));
[Link]("Salary: " + [Link]());
[Link]("Address: " + [Link]());
[Link]("------------------------------");
[Link]();
[Link]();
}
}
}
OUTPUT:
Vansh Singh
TYCS “C’
9179
4) Get employees who either live in 'Austin' or have a salary greater than 70,000.
[Link]
package [Link]. muskan9046;
import [Link]; import
[Link]; import
[Link]; import
[Link]; import
[Link]; import
[Link]; import [Link];
public class App {
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration()
.configure("[Link]")
.addAnnotatedClass([Link])
.buildSessionFactory();
Session session = [Link]();
try {
[Link]();
Criteria criteria = [Link]([Link]);
[Link]([Link](
[Link]("address", "Austin"),
[Link]("salary", 70000.0)
));
List<Employee> employees = [Link]();
[Link]().commit(); if
([Link]()) {
[Link]("No employees found who live in Austin or have salary greater than
70000.");
} else {
[Link]("Employees who live in Austin or have salary greater than 70000:"); for
(Employee emp : employees) {
[Link]("ID: " + [Link]());
[Link]("Name: " + [Link]());
[Link]("Email: " + ([Link]() != null ? [Link]() :
"NULL"));
[Link]("Salary: " + [Link]());
[Link]("Address: " + [Link]());
[Link]("------------------------------");
Vansh
Singh
TYCS
“C’
9179
}
}
[Link](); [Link](); }
OUTPUT:
5. Fetch employees with salary > 40,000 and sort them by address ascending.
[Link]
package [Link]. muskan9046;
import [Link]; import
[Link]; import
[Link]; import
[Link]; import
[Link]; import
[Link]; public class App {
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration()
.configure("[Link]")
.addAnnotatedClass([Link])
Vansh Singh
TYCS “C’
9179
.buildSessionFactory();
Session session = [Link]();
try {
[Link]();
Criteria criteria = [Link]([Link]);
[Link]([Link]("salary", 40000.0));
[Link]([Link]("address"));
List<Employee> employees = [Link]();
[Link]().commit(); if ([Link]()) {
[Link]("No employees found with salary greater than 40,000."); } else
{
[Link]("Employees with salary greater than 40,000 sorted by address:"); for
(Employee emp : employees) {
[Link]("ID: " + [Link]());
[Link]("Name: " + [Link]());
[Link]("Email: " + ([Link]() != null ? [Link]() :
"NULL"));
[Link]("Salary: " + [Link]());
[Link]("Address: " + [Link]());
[Link]("------------------------------");
}
}
} catch (Exception e) {
[Link]();
} finally {
[Link]();
[Link]();
}
}
}
OUTPUT:
Vansh singh
TYCS ‘C’
9179
PRACTICAL NO 11
Aim:Implementation of NamedQuery with hibernate
Artist table :
id name genre country debutYear
Q1. Write a Hibernate program using @NamedQuery to fetch all artists from a
country given by the user
❖ Source code:
● [Link]
package [Link]._pract11;
import [Link].*;
import [Link];
import [Link];
import [Link];
@NamedQueries(
{
@NamedQuery(
name = "findArtistByCountry",
query = "from Artist a where [Link] = :country"
)
Vansh singh
TYCS ‘C’
9179
}
)
@Entity
@Table(name="artists")
public class Artist {
@Id
@GeneratedValue(strategy = [Link])
private int id;
@Column(name = "name")
private String name;
@Column(name= "genre")
private String genre;
@Column(name= "country")
private String country;
@Column(name= "debutYear")
private int debutYear;
public Artist() {}
public Artist(String name, String genre, String country,int debutYear) {}
public int getId() {
return id;
}
public void setId(int id) {
[Link] = id;
}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
[Link] = genre;
}
public String getCountry() {
return country;
}
Vansh singh
TYCS ‘C’
9179
● [Link]
● [Link]’
package [Link]._pract11;
import [Link].*;
import [Link].*;
import [Link];
import [Link].*;
import [Link];
public class App
Vansh singh
TYCS ‘C’
9179
{
public static void main( String[] args )
{
Output:
Vansh singh
TYCS ‘C’
9179
Q2. Write a Hibernate program using @NamedQuery to fetch artists based on both genre
and debutYear given by the user
e.g. Fetch artists with genre = 'Pop' and debutYear > 2010.
❖ Source code:
● [Link]
package [Link].pract11_9020;
import [Link].*;
import [Link];
import [Link];
import [Link];
@NamedQueries(
{
@NamedQuery(
name = "findArtistByGenreAndYear",
query = "FROM Artist a WHERE [Link] = :genre AND [Link] = :year" )})
@Entity
@Table(name="artists")
public class Artist {
@Id
@GeneratedValue(strategy = [Link])
private int id;
@Column(name = "name")
private String name;
@Column(name= "genre")
private String genre;
@Column(name= "country")
private String country;
Vansh singh
TYCS ‘C’
9179
@Column(name= "debutYear")
private int debutYear;
public Artist() {}
public Artist(String name, String genre, String country,int debutYear) {}
public int getId() {
return id;
}
public void setId(int id) {
[Link] = id;
}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
[Link] = genre;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
[Link] = country;
}
public int getDebutYear() {
return debutYear;
}
public void setDebutYear(int debutYear) {
[Link] = debutYear;
}}
● [Link]
package [Link].pract11_9020;
import [Link].*;
Vansh singh
TYCS ‘C’
9179
import [Link].*;
import [Link];
import [Link].*;
import [Link];
public class App {
public static void main(String[] args) {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session sn = [Link]();
Scanner sk = new Scanner([Link]);
[Link]("Enter the Genre: ");
String x = [Link]();
[Link]("Enter the Debut Year: ");
int y = [Link]();
Query<Artist> query = [Link]("findArtistByGenreAndYear",
[Link]);
[Link]("genre", x);
[Link]("year", y);
List<Artist> art=[Link]();
if ([Link]()) {
[Link]("\nNo artists found for the specified genre and year."); } else {
[Link]("\n--- Found Artists ---");
for (Artist a : art) {
[Link]("ID:" + [Link]());
[Link]("Name:" + [Link]());
[Link]("Country:" + [Link]());
[Link]("Genre:" + [Link]());
[Link]("Debut Year:" + [Link]());
[Link]("--- Finish ---");
}
[Link]();
}
}
}
Output:
Vansh singh
TYCS ‘C’
9179
● [Link]
]]>
</query>
</hibernate-mapping>
● [Link]
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
<session-factory>
<property name="[Link].driver_class">[Link]</property>
<property
name="[Link]">jdbc:mysql://localhost:3306/megha_9020?useSSL=true&
;requireSSL=false&verifyServerCertificate=false</property>
<property name="[Link]">root</property>
<property name="[Link]">root</property>
<property name="connection.pool_size">8</property>
<property name="[Link]">[Link].MySQL8Dialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="[Link]">update</property>
<mapping resource="[Link]"/>
</session-factory>
</hibernate-configuration>
● [Link]
package [Link].pract11_9020;
import [Link].*;
import [Link];
import [Link].*;
import [Link];
public class App {
public static void main(String[] args) {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session sn = [Link]();
Query<Artist> query = [Link]("findAllArtistsSortedByYear", [Link]);
List<Artist> art=[Link]();
if ([Link]()) {
[Link]("\nNo artists found for the specified genre and year."); } else {
Vansh singh
TYCS ‘C’
9179
Output:
Vansh singh
TYCS ‘C’
9179
Vansh singh
TYCS ‘C’
9179
Q4. Write a NamedQuery in XML to fetch only the name column of all artists
❖ Source code
● [Link]
</hibernate-mapping>
● [Link]
package [Link].pract11_9020;
import [Link].*;
import [Link].*;
import [Link];
import [Link].*;
import [Link];
public class App {
public static void main(String[] args) {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session sn = [Link]();
[Link]();
}
}
}
Vansh singh
TYCS ‘C’
9179
Output:
Q5. Write a NamedQuery in XML to count the total number of artists from a given
country.
❖ Source code:
● [Link]
● [Link]
package [Link].pract11_9020;
import [Link].*;
import [Link].*;
import [Link];
import [Link].*;
import [Link];
public class App {
public static void main(String[] args) {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session sn = [Link]();
Scanner sk = new Scanner([Link]);
[Link]("Enter the country to count artists from: ");
String countryInput = [Link]();
TypedQuery<Long> query = [Link]("countArtistsByCountry",
[Link]);
[Link]("country", countryInput);
Long artistCount = [Link]();
[Link]("\nTotal number of artists from " + countryInput + ": " + artistCount);
[Link]();
}
}
Output:
Vansh singh
TYCS ‘C’
9179