I am new to Java web developing and I am having a lot of trouble working with Hibernate. I looked at a lot of examples online on how to do this and so far I don't have any luck getting any of it to work. I notice some pattern they use online, a lot of them go like bellow.
@Autowired
private SessionFactory sessionFactory;
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
// do something with session
session.getTransaction().commit();
However, whenever I try to do that in my project, I got an error saying
Field sessionFactory in com.bT.practice.WebMySQLAspects.dao.StudentDAOImpl required a bean of type 'org.hibernate.SessionFactory' that could not be found.
I am really confused about this and I couldn't find a good example on hibernate website on how to do this. I use http://start.spring.io/
to bootstrap my app. Bellow is my code.
ENTITY
package com.bT.practice.WebMySQLAspects.entity;
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="student")
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 + "]";
}
}
DAO IMPLEMENTATION
package com.bT.practice.WebMySQLAspects.dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.bT.practice.WebMySQLAspects.entity.Student;
@Repository
public class StudentDAOImpl implements StudentDAO {
@Autowired
private SessionFactory sessionFactory;
@Override
public List<Student> getStudents() {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
List<Student> students = session.createQuery("from Student order by lastName").list();
session.getTransaction().commit();
return students;
}
}
SERVICE IMPLEMENTATION
package com.bT.practice.WebMySQLAspects.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bT.practice.WebMySQLAspects.dao.StudentDAO;
import com.bT.practice.WebMySQLAspects.entity.Student;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDAO studentDAO;
@Override
@Transactional
public List<Student> getStudents() {
return studentDAO.getStudents();
}
}
CONTROLLER
package com.bT.practice.WebMySQLAspects.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.bT.practice.WebMySQLAspects.entity.Student;
import com.bT.practice.WebMySQLAspects.service.StudentService;
@RestController
@RequestMapping("/api")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/students/show")
public List<Student> getStudents() {
List<Student> students = studentService.getStudents();
return students;
}
}
application.properties
spring.datasource.driverClssName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false
spring.datasource.username=username
spring.datasource.password=password
Since hibernate has existed for nearly two decades now, many tutorials are horribly outdated. You'll have a far easier time if you use tutorials released in the last few years.
Originally hibernate could be accessed only in a hibernate specific way, using Sessions
obtained from a SessionFactory
. In 2006, the Java Persistence API standard created a general way of accessing object relational mappers in Java through EntityManagers
obtained from an EntityManagerFactory
. Its design was heavily influenced by the Hibernate team, and became the preferred way to access hibernate with the release of Hibernate 3.2 in the fall of 2006.
That is, the tutorials you found have been obsolete for over a decade. To see how it's done now, check out
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.