Hibernate Demo
Hibernate Demo
Hibernate Demo
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>
<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"?>
<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")
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name="fname")
private String firstName;
@Column
private String lastName;
@Column
private int 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;
// create session
Session session = factory.openSession();
/*
// 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();
}
}