Uploaded by arun kumar

hibernate27-05-2023

advertisement
package com.arun.hiber.inher.ex1;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import jakarta.persistence.Query;
public class MainApp {
public static void main(String... args) {
try(Session session=HiberUtil.getSessionFactory().openSession()){
Teacher t1 = new Teacher();
t1.setRollNo(24);
t1.setName("Kmatchi");
t1.setSal(1500);
Transaction txn;
txn = session.beginTransaction();
session.save(t1);
//session.save(s1);
txn.commit();
txn = session.beginTransaction();
String hql = " FROM Teacher";
Query query = session.createQuery(hql);
List results = query.getResultList();
Teacher s1 = (Teacher) results.get(0);
System.out.println(s1.getName()+" : "+s1.getRollNo()+" :
"+s1.getSal());
}
}
}
HiberUtil.java
package com.arun.hiber.inher.ex1;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class HiberUtil {
private static StandardServiceRegistry registry;
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
try {
registry = new
StandardServiceRegistryBuilder().configure("student.cfg.xml").build(
);
MetadataSources sources = new MetadataSources(registry);
Metadata data = sources.getMetadataBuilder().build();
sessionFactory = data.getSessionFactoryBuilder().build();
}catch(Exception ex) {
ex.printStackTrace();
}
return sessionFactory;
}
}
Student.java
package com.arun.hiber.inher.ex1;
import jakarta.persistence.*;
@Entity
@Table(name="Student_info")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="student",discriminatorType=DiscriminatorT
ype.STRING)
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
@Column(name="student_id")
private int id;
private int rollNo;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(int rollNo, String name) {
super();
this.rollNo = rollNo;
this.name = name;
}
public Student() {
}
}
Teacher.java
package com.arun.hiber.inher.ex1;
import jakarta.persistence.*;
@Entity
@Table(name="Teacher_info")
public class Teacher extends Student{
private double sal;
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
}
Student.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/jpahiber</property
>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- JDBC connection pool settings ... using built-in test pool -->
<property name="connection.pool_size">2</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>
<!-- Drop and re-create the database schema on startup -->
<!-- create,create-drop,update,validate -->
<property name="hbm2ddl.auto">create</property>
<!-- dbcp connection pool configuration -->
<mapping class="com.arun.hiber.inher.ex1.Student" />
<mapping class="com.arun.hiber.inher.ex1.Teacher" />
</session-factory>
</hibernate-configuration>
Pom.xml
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java ->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core
-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.7.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<scope>provided</scope>
</dependency>
</dependencies>
Download