无法插入,更新和删除以与spring-data-jpa一起使用

时间:2018-12-27 20:57:01

标签: spring-mvc spring-boot spring-data-jpa crud

我正在尝试创建一个执行使用Spring Boot 2编写的基本CRUD操作的Web服务。select操作可以正常工作,但是insert,delete和update操作无效,因为它们的查询未生成和执行。< / p>

我浏览了不同的示例,但无法解决任何问题。我主要担心的是,甚至没有为插入,删除或更新操作触发查询的事实。

学生实体

@Entity
@Table(name = "student")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Student {

@Id
@NotNull
@Column(name = "id")
private int id;

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

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

public Student(int id, String name, String course) {
    this.id = id;
    this.name = name;
    this.course = course;
}

public Student(){}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getCourse() {
    return course;
}

public void setCourse(String course) {
    this.course = course;
}

@Override
public String toString() {
    return "Student{ id=" + id + ", name='" + name + '\'' + ", course='" + course + '\'' + '}';
}
}

StudentDaoImpl

@Repository
@Transactional
public class StudentDaoImpl implements StudentDao {

@Autowired
private EntityManagerFactory entityManagerFactory;

@Override
public List<Student> fetchAllStudents() {
    Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
    CriteriaBuilder cb = session.getCriteriaBuilder();
    CriteriaQuery<Student> cq = cb.createQuery(Student.class);
    Root<Student> root = cq.from(Student.class);
    CriteriaQuery<Student> all = cq.select(root);
    List<Student> solution = session.createQuery(all).getResultList();
    session.close();
    return solution;
}

@Override
public Student deleteStudent(Integer id) {
    Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
    Student student = session.load(Student.class, id);
    if (student != null){
        session.delete(student);
        session.close();
    }
    return student;
}

@Override
public Student fetchForId(Integer id){
    Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
    Student student = session.load(Student.class, id);
    session.close();
    return student;
}

@Override
public Student insertStudent(Student student) {
    Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
    session.save(student);
    session.close();
    return student;
}

@Override
public Student updateStudent(Student student) {
    Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
    Student studentCheck = session.load(Student.class, student.getId());
    if (studentCheck != null) {
        session.saveOrUpdate(student);
        session.close();
    }
    return student;
}
}

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=

spring.jpa.database = MYSQL
spring.jpa.show-sql = true

spring.jpa.hibernate.ddl-auto = update
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

编辑

用EntityManager(+ Persistent Context Annotation)代替EntityManagerFactory为我工作。但是我仍然没有弄清楚为什么持久性对EntityManager有用。

2 个答案:

答案 0 :(得分:0)

如果它不是很重要,则可以使用NativeQuery及其executeUpdate API来完成:

String query = "insert into student values(1,?)";

em.createNativeQuery(query)
   .setParameter(1, "Tom")
   .executeUpdate();

答案 1 :(得分:0)

我想建议这个仓库

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;


@Repository
public interface StudentRepository extends JpaRepository<Student, Integer> {

}

可能您必须将Student的ID从int更改为Integer。

该存储库具有用于检索,更新,创建和删除的方法。

假设您要在服务中使用此存储库,则可以这样操作:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class StudentService {

    @Autowired
    private StudentRepository studentRepository;

    ......

}