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

Curd Exmp

The document contains code for an entity class called Student that defines properties like id, firstName, lastName and email. It uses JPA annotations to map the class to a database table. There is also configuration code that sets up Hibernate to connect to a MySQL database and map the Student entity. The AppTest class shows sample code to save, update and delete Student objects using Hibernate.

Uploaded by

Shubham Pakhale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Curd Exmp

The document contains code for an entity class called Student that defines properties like id, firstName, lastName and email. It uses JPA annotations to map the class to a database table. There is also configuration code that sets up Hibernate to connect to a MySQL database and map the Student entity. The AppTest class shows sample code to save, update and delete Student objects using Hibernate.

Uploaded by

Shubham Pakhale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

package hb1;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "student03")
public class Student {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;

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

@Column(name = "last_name")
private String lastName;

@Column(name = "email")
private String email;

public Student() {

public Student(String firstName, String lastName, String email) {


this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}

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 String getEmail() {


return email;
}

public void setEmail(String email) {


this.email = email;
}

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

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


super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}

Hibernate .cfg

<?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="connection.driver.class">com.mysql.jdbc.Driver</prope
rty>
<property
name="connection.url">jdbc:mysql://localhost:3306/myhibpro<
/property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property
name="dialect">org.hibernate.dialect.MySQL5Dialect</propert
y>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>

<mapping class="hb1.Student" />


</session-factory>

</hibernate-configuration>

App.class

package hb1;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class AppTest {


public static void main(String[] args) {

SessionFactory factory=new Configuration().configure().buildSessionFactory();

System.out.println("1.insert 2.update 3.display 4.delete 5.exit");


System.out.println(factory);

Session session = factory.openSession();


Transaction transaction = session.beginTransaction();
// Student s=new Student("neha","kapoor","[email protected]");
// session.saveOrUpdate(s);

Student student = session.get(Student.class, 1);


// student.setFirstName("nakul");
// session.saveOrUpdate(student);
System.out.println(student);

session.delete(student);

// Student s2=new Student(1,"soham","khan","[email protected]");

transaction.commit();

session.close();

factory.close();
}

You might also like