CRUD Operations example Using Hibernate
Hibernate an open source Java persistence framework project. Perform powerful object relational mapping and query databases using HQL and SQL. Read more .
- Java 1.8
- Maven 3.2.
- Hibernate 5.x
- Mysql 5
- Eclipse
- Add Hibernate dependency and MySQL dependency in pom.xml
- Create Hibernate Configuration file
- Create Entity Class
- Create Hibernate Utility Class
- execute CRUD methods
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>5.3.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
The hibernate.cfg.xml file should be in root directory of the classpath of your project. If you using Maven then make sure it should be like src > resources > hibernate.cfg.xml.
@Entity
@Table(name="BOOK")
public class Book implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String title;
private String author;
private double price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
public void saveBook(String title,String author,double price){
}
public List<Book> getBooks(){
}
public int updateBook(Book b){
}
public int deleteBook(int id) {
}