0% found this document useful (0 votes)
11 views3 pages

Day22 Lab

The document defines a Student entity with properties like student ID, name, standard, roll number, and age. Getter and setter methods are created for each property. The document also shows how to retrieve a student entity by ID from the database using Hibernate and print out its properties.

Uploaded by

xeraceb151
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)
11 views3 pages

Day22 Lab

The document defines a Student entity with properties like student ID, name, standard, roll number, and age. Getter and setter methods are created for each property. The document also shows how to retrieve a student entity by ID from the database using Hibernate and print out its properties.

Uploaded by

xeraceb151
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/ 3

Creating Student datatables

package com.EXHibernate.entities;

import javax.persistence.*;
@Entity
@Table(name = "studentlist")
public class Student
{
@Id
private String stdid;
private String stdname;
@Column(name = "Standard")
private String std;
private int rollno;
private int age;

public Student() {
super();
// TODO Auto-generated constructor stub
}

public String getStdid() {


return stdid;
}

public void setStdid(String stdid) {


this.stdid = stdid;
}

public String getStdname() {


return stdname;
}

public void setStdname(String stdname) {


this.stdname = stdname;
}

public String getStd() {


return std;
}

public void setStd(String std) {


this.std = std;
}

public int getRollno() {


return rollno;
}

public void setRollno(int rollno) {


this.rollno = rollno;
}
public int getAge() {
return age;
}

public void setAge(int age) {


this.age = age;
}

Fetching data

package com.example.hibernateStudentManagement;
import org.hibernate.*;
import org.hibernate.cfg.*;
import com.EXHibernate.entities;
public class RetrievingDataUsinggetmethod {

public static void main(String[] args)


{
// create object for configuration
Configuration config=new Configuration().configure("hibernate.cfg.xml");
//create Session Factory
SessionFactory factory=config.buildSessionFactory();
// create a session object
Session session=factory.openSession();
/*-------- Entity Creation----------------------------*/
// Transaction
Transaction transaction=session.beginTransaction();
/*---- To retrieve entity-----*/
Employee emp1=session.get(Employee.class,"emp102");
if(emp1 == null)
{
System.out.println("No data found");
}
else
{
System.out.println(emp1);
System.out.println("Employee id : "+emp1.getEmpid());
System.out.println("Employee Name : "+emp1.getEmpname());
System.out.println("Salary : Rs "+emp1.getSalary());
}
System.out.println("---------------------------------------------");
/*---- To retrieve entity-----*/
Employee emp2=session.get(Employee.class,"emp102");
if(emp2 == null)
{
System.out.println("No data found");
}
else
{
System.out.println(emp2);
System.out.println("Employee id : "+emp2.getEmpid());
System.out.println("Employee Name : "+emp2.getEmpname());
System.out.println("Salary : Rs "+emp2.getSalary());
}
/*----- Commit the transaction-----*/
transaction.commit();

System.out.println("------------------------------------");
/*---- Closing the resources-----*/
session.close();
factory.close();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy