Hibernate CRUD

package com.luv2code.hibernate.demo;

import java.util.List;

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

import com.luv2code.hibernate.demo.entity.Student;

public class ReadStudentDemo {

	public static void main(String[] args) {
		//create session factory
		SessionFactory factory = new Configuration()
				.configure("hibernate.cfg.xml")
				.addAnnotatedClass(Student.class)
				.buildSessionFactory();
		//create session		
		Session session = factory.getCurrentSession();
		try {		
			//start a transaction
			session.beginTransaction();			
			// query students
			List<Student> theStudents = session.createQuery("from Student").getResultList();
			//display students
			displayStudents(theStudents);
			System.out.println("-----------------");	
			// query studnets : lastName = 'Duck'
			theStudents = session.createQuery("from Student s where s.lastName = 'Duck'").list();
			//display students		
			displayStudents(theStudents);
			//query students : lataName = ''
//			System.out.println("-----------------");	
//			theStudents = session.createQuery("from Student s where "+"s.lastName='zx' OR s.firstName ='Jiang'").getResultList();
//			displayStudents(theStudents);
			//query studets where LIKE 'XXX'    students' email ends with XXX
//			theStudents = session.createQuery("from Student s where "+
//					"s.email LIKE 'Vin@steve'").getResultList();
			//commit transaction
			session.getTransaction().commit();
		}
		finally {
			factory.close();
		}
	}
	private static void displayStudents(List<Student> theStudents) {
		for(Student tempStudent : theStudents) {
			System.out.println(tempStudent);
		}
	}
}

Hibernate: select student0_.id as id1_0_, student0_.email as email2_0_, student0_.first_Name as first_Na3_0_, student0_.last_Name as last_Nam4_0_ from student student0_
Student [id=1, firstName=Daff, lastName=Duck, email=Vin@luv2code]
-----------------
Hibernate: select student0_.id as id1_0_, student0_.email as email2_0_, student0_.first_Name as first_Na3_0_, student0_.last_Name as last_Nam4_0_ from student student0_ where student0_.last_Name='Duck'
Student [id=1, firstName=Daff, lastName=Duck, email=Vin@luv2code]
Done
Update数据
public class UpdateStudentDemo {

	public static void main(String[] args) {
		//create session factory
		SessionFactory factory = new Configuration()
				.configure("hibernate.cfg.xml")
				.addAnnotatedClass(Student.class)
				.buildSessionFactory();
		//create session
		
		Session session = factory.getCurrentSession();
		try {
		
			int studentId = 1;
			//start a transaction 更新id为1的lastName
			session = factory.getCurrentSession();
			session.beginTransaction();
			
			System.out.println("\nGetting student with id: "+studentId);
			Student myStudent = session.get(Student.class, studentId);
			
			System.out.println("Updating student...");	
			myStudent.setFirstName("LALA");
			
			//更新所有email
			session.getTransaction().commit();
			
			session = factory.getCurrentSession();
			session.beginTransaction();
			
			session.createQuery("update Student set email='JJJ@QQ'")
			.executeUpdate();
			session.getTransaction().commit();
	
		}
		finally {
			factory.close();
		}
		
	}
	private static void displayStudents(List<Student> theStudents) {
		for(Student tempStudent : theStudents) {
			System.out.println(tempStudent);
		}
	}

}

在这里插入图片描述

删除行数据
			int studentId = 2;
			//start a transaction
			session = factory.getCurrentSession();
			session.beginTransaction();
			
			System.out.println("\nGetting student with id: "+studentId);
			Student myStudent = session.get(Student.class, studentId);
			
			session.delete(myStudent);
			//session.createQuery("delete from Student where id = 2").executeUpdate();
			
			session.getTransaction().commit();
If you are using Hibernate 5.2 or higher, then the Query list() method has been deprecated.

In your code you should make the following update:
Replace
session.createQuery(“from Student”).list()
With
session.createQuery(“from Student”).getResultList()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值