0% found this document useful (0 votes)
12 views

Hibernate Demo

The document describes how to configure Hibernate for use with a MySQL database. It includes the POM file dependencies, hibernate configuration file, entity class, and an example application class to demonstrate basic CRUD operations using Hibernate.

Uploaded by

Sagar Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Hibernate Demo

The document describes how to configure Hibernate for use with a MySQL database. It includes the POM file dependencies, hibernate configuration file, entity class, and an example application class to demonstrate basic CRUD operations using Hibernate.

Uploaded by

Sagar Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

from mvnrepository

1. pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.app</groupId>
<artifactId>HibernateDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>HibernateDemo</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->


<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.5.4.Final</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->


<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.2.0</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

2. hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>

<!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>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property
name="connection.url">jdbc:mysql://192.168.100.80:3306/group038</property>
<property name="connection.username">group038</property>
<property name="connection.password">welcome</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<mapping class = "com.app.Student" />
</session-factory>
</hibernate-configuration>

3. Student.java

package com.app;

import javax.persistence.*;

@Entity
@Table(name="student123")

public class Student {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

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

@Column
private String lastName;

@Column
private int age;

public Student(int id, String firstName, String lastName, int age) {


super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public Student() {
// TODO Auto-generated constructor stub
}

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public String getFirstName() {


return firstName;
}

public void setFirstName(String firstName) {


this.firstName = firstName;
}

public String getLastName() {


return lastName;
}

public void setLastName(String lastName) {


this.lastName = lastName;
}

public int getAge() {


return age;
}

public void setAge(int age) {


this.age = age;
}

@Override
public String toString() {
return "Student [id=" + id + ", firstName=" + firstName + ", lastName="
+ lastName + ", age=" + age + "]";
}

4. App.java

package com.app;

import java.util.List;

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

public class App


{
/**
* @param args
*/
public static void main( String[] args )
{ //import org.hibernate.SessionFactory; import
org.hibernate.cfg.Configuration;
SessionFactory factory = new Configuration().
configure("hibernate.cfg.xml").
addAnnotatedClass(Student.class).
buildSessionFactory();

// create session
Session session = factory.openSession();

// begin the transaction


session.beginTransaction();

//4. perform operations


// 4.1 create table
Student s = new Student();
/* s.setFirstName("Vikas");
s.setLastName("Borade");
s.setAge(55);
session.save(s);
*/

/*
// 4.2 get data from student id
System.out.println("get data from student id");
int id =4;
Student s1 =session.get(Student.class, id);
System.out.println(s1);
*/

/*
// 4.3 update student
System.out.println("update student");
int id2 =1;
Student s2 =session.get(Student.class, id2);
s2.setFirstName("Rajan");
s2.setLastName("Pal");
*/

/*
//4.4 delete student
System.out.println("delete student ");
int id3 =4;
Student s3 =session.get(Student.class, id3);
session.delete(s3);
*/

/*
//4.5 to get list of all student
System.out.println("all student list");
Query query = session.createQuery("from Student"); // import
org.hibernate.query.Query;
List<Student> l=query.list(); // import
java.util.List;

for(Student s11:l)
{
System.out.println(s11);
}
*/

/*
// HQL = Hibernate Query Language
System.out.println("only patil lastname");
List<Student> l1 = session.createQuery("from Student s where s.lastName =
'borade' ").getResultList();

for(Student s11:l1)
{
System.out.println(s11);
}

*/
System.out.println("Object Persisted");

session.getTransaction().commit();
session.close();
factory.close();

}
}

You might also like