我是hibernate-spring tirple的新手..
我只是尝试编写简单的注册簿..我有以下代码:
Student.java
@Entity(name = "STUDENTS")
@NamedQueries({
@NamedQuery(name = "getAllStudent", query = "SELECT k FROM STUDENTS k ORDER BY k.id DESC"),
@NamedQuery(name = "findByName", query = "SELECT k FROM STUDENTS k WHERE k.name LIKE :name")
})
public class Student {
@Column(name = "STUDENTNO", nullable = false)
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "NAME", nullable = false)
private String name;
@Column(name = "SURNAME")
private String surname;
@Column(name = "AGE")
private String age;
// GET ve SET metods
StduentDAO.java
@Repository
@Transactional(readOnly = true)
public class StudentDAO implements IStudentDAO {
@PersistenceContext
EntityManager em;
@Override
public void deleteStudent(Student student) {
Student temp = em.getReference(Student.class, student.getId());
em.remove(temp);
System.out.println("### getting out from studentDAO deleteStudent method ###")
StudentController.java
@Component
@Scope(value = "request")
public class StudentController {
@Autowired
IStudentDAO studentDAO;
List<Student> allStudentList = new ArrayList();
Student student = new Student();
@PostConstruct
private void loadStudents() {
allStudentList = studentDAO.allStudent();
public void deleteStudent() {
studentDAO.deleteStudent(student);
System.out.println("### getting out from StudentController deleteStudent method ### ");
}
当我运行deleteStudent()
代码时,我得到了:
“###退出studentDAO deleteStudent方法###”
“###从StudentController中删除deleteStudent方法###”
我在输出上看到这些但没有从数据库中删除..我搜索了一下,我发现这“每个权利经理的方法都打开了自己的会话。”这就是为什么它说我应该像上面那样编写我的StudentDAO的deleteStudent方法..
我想我错过了关于交易的一些事情,但我还没有认识到..
我该怎么办?
谢谢..
答案 0 :(得分:0)
@Transactional注释在您的DBMS上创建一个事务。
如果使用(readOnly = true),则会阻止对DB的操作(如INSERT / UPDATE / DELETE)。
删除readOnly = true,这样你的删除方法就可以了。