Класс STUDENTS

package org.example.entity;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@Table(name = "STUDENTS")
@NamedQueries(value = { @NamedQuery(name = "STUDENTS.getAll", query = "SELECT s FROM STUDENTS s"),
		@NamedQuery(name = "STUDENTS.getNameById", query = "SELECT s.name FROM STUDENTS s WHERE s.ID = :ID")

})

public class STUDENTS implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long ID;

	@Column
	private String name;

	@Column
	private String hobby;

	@Override
	public String toString() {
		return "STUDENTS [ID=" + ID + ", name=" + name + ", hobby=" + hobby + "]";
	}

	public String getName() {
		return name;
	}

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

	public String getHobby() {
		return hobby;
	}

	public void setHobby(String hobby) {
		this.hobby = hobby;
	}

	public long getID() {
		return ID;
	}

}


Класс CRUD

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;

import org.example.entity.STUDENTS;

public class StudentsCrud {

	public EntityManager em = Persistence.createEntityManagerFactory("JPA").createEntityManager();

	public STUDENTS add(STUDENTS stud) {
		em.getTransaction().begin();
		STUDENTS studDB = em.merge(stud);
		em.getTransaction().commit();
		System.out.println("Add record " + studDB.toString());
		return studDB;
	}

	public STUDENTS get(long id) {
		if (em.find(STUDENTS.class, id) == null) {
			System.out.println("Record with ID = " + id + " is not found ");
			return null;
		} else {
			System.out.println("Record with ID = " + id + " is found ");
		}

		return em.find(STUDENTS.class, id);
	}	
	
	public List<STUDENTS> getAll() {
		System.out.println("--- All students ---");
		return em.createNamedQuery("STUDENTS.getAll", STUDENTS.class).getResultList();
	}
	
	public String getNameByID(long id) {
		try {
			return em.createNamedQuery("STUDENTS.getNameById", String.class).setParameter("ID", id).getSingleResult();
		} catch(NoResultException e){
			return null;
		}
	}
	
	
	public void delete(long id) {
		em.getTransaction().begin();
		if (get(id) != null) {
			em.remove(get(id));
			System.out.println("Delete record by ID = " + id);
		}
		em.getTransaction().commit();
	}
	
	

	public long getLastId() {
		long maxId = em.createQuery("SELECT MAX(s.ID) FROM STUDENTS s", Long.class).getSingleResult();
		System.out.println("Max ID = " + maxId);
		return maxId;
	}

	public long getCountRecords() {
		long countRecords =  em.createQuery("SELECT COUNT(s.name) FROM STUDENTS s", Long.class).getSingleResult();
		System.out.println("Count records  = " + countRecords);		
		return countRecords;
	}

}


Главный класс:

import java.util.List;

import org.example.crud.StudentsCrud;
import org.example.entity.STUDENTS;

public class MainClass {

	public static void main(String[] args) {

		STUDENTS s = new STUDENTS();

		s.setName("Petrov");
		s.setHobby("swimming");

		StudentsCrud sc = new StudentsCrud();
		sc.add(s);
		
		

		sc.getLastId();
		sc.getCountRecords();

		List<STUDENTS> students = sc.getAll();

		for (STUDENTS ss : students) {
			System.out.println(ss);
		}

		sc.getNameByID(1);
		sc.delete(1);
		sc.getNameByID(1);
		
		
			
	}
}

Реклама