Skip to content
Open

hi #1

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 104 additions & 4 deletions src/main/java/sba/sms/models/Course.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,119 @@

import jakarta.persistence.*;
import lombok.*;
import lombok.experimental.FieldDefaults;

import java.util.LinkedHashSet;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
* Course is a POJO, configured as a persistent class that represents (or maps to) a table
* name 'course' in the database. A Course object contains fields that represent course
* information and a mapping of 'courses' that indicate an inverse or referencing side
* information and a mapping of that indicate an inverse or referencing side
* of the relationship. Implement Lombok annotations to eliminate boilerplate code.
*/

@Entity

public class Course {

}
// Getter Setter methods
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private int id;

@Column(name = "name", length = 50, nullable = false)
private String name;


@Column(name = "instructor", length = 50, nullable = false)
private String instructor;

@ManyToMany(mappedBy = "courses", fetch = FetchType.EAGER, cascade = {CascadeType.DETACH, CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST})
private Set<Student> students = new HashSet<>();

public Course(){

}


public Course(int id, String name, String instructor, Set<Student> students){
this.id = id;
this.name = name;
this.instructor = instructor;
this.students = students;
}

// Required-args constructor
public Course(String name, String instructor){
this.name = name;
this.instructor = instructor;
}

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 getInstructor(){
return instructor;
}

public void setInstructor(String instructor){
this.instructor = instructor;
}

public Set<Student> getStudents(){
return students;
}

public void setStudents(Set<Student> students){
this.students = students;
}

// toString method
@Override
public String toString(){
return "Course{" +
"id=" + id +
", name='" + name + '\'' +
", instructor='" + instructor + '\'' +
'}';

}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Course course = (Course) o;
return id == course.id && Objects.equals(name, course.name) && Objects.equals(instructor, course.instructor) && Objects.equals(students, course.students);
}


// hash code method
@Override
public int hashCode(){
return Objects.hash(id, name ,instructor);
}

//Helper method
public boolean hasStudent(Student student){
return this.students.contains(student);
}



}
102 changes: 99 additions & 3 deletions src/main/java/sba/sms/models/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import jakarta.persistence.*;
import lombok.*;
import lombok.experimental.FieldDefaults;

import java.util.LinkedHashSet;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

Expand All @@ -13,14 +12,111 @@
* Student is a POJO, configured as a persistent class that represents (or maps to) a table
* name 'student' in the database. A Student object contains fields that represent student
* login credentials and a join table containing a registered student's email and course(s)
* data. The Student class can be viewed as the owner of the bi-directional relationship.
* data. The Student class can be viewed as the owner of the relationship.
* Implement Lombok annotations to eliminate boilerplate code.
*/
@NoArgsConstructor



@Table (name = "student")
@Entity
public class Student {
// Getter Setters
@Id
@Column(name = "email", length = 50, nullable = false)
private String email;

@Column(name = "name", length = 50, nullable = false)
private String name;

@Column(name = "password", length = 50, nullable = false)
private String password;

@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH, CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST})
@JoinTable(
name = "student_courses",
joinColumns = @JoinColumn(name = "student_email"),
inverseJoinColumns = @JoinColumn(name = "course_id")
)
private Set<Course> courses = new HashSet<>();


//
public Student(String email, String name, String password, Set<Course> courses){
this.email = email;
this.name = name;
this.password = password;
this.courses = courses;
}

public Student(String email, String name, String password) {
this.email = email;
this.name = name;
this.password = password;
}

public String getEmail(){
return email;
}

public void setEmail(String email){
this.email = email;
}

public String getName(){
return name;
}

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

public String getPassword(){
return password;
}

public void setPassword(String password){
this.password = password;
}

public Set<Course> getCourses(){
return courses;
}

public void setCourses(Set<Course> courses){
this.courses = courses;
}

public static void addCourse(Course courseById) {

}


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

@Override
public boolean equals(Object o){
if(this == o) return true;
if(o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return Objects.equals(email, student.email) &&
Objects.equals(name, student.name) &&
Objects.equals(password, student.password);
}

@Override
public int hashCode(){
return Objects.hash(email, name, password);
}



}
62 changes: 60 additions & 2 deletions src/main/java/sba/sms/services/CourseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import sba.sms.dao.CourseI;
import sba.sms.models.Course;
import sba.sms.utils.HibernateUtil;

import javax.security.auth.login.Configuration;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -16,6 +18,62 @@
* CourseI interface, overrides all abstract service methods and
* provides implementation for each method.
*/
public class CourseService {
public class CourseService implements CourseI{

}
@Override
public void createCourse(Course course) {
Session s = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;

try{
tx = s.beginTransaction();
s.persist(course);
tx.commit();
} catch (HibernateException exception){
if(tx != null) tx.rollback();
} finally {
s.close();
}
}

@Override
public Course getCourseById(int courseId) {
Session s = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
Course course = new Course();

try{
tx = s.beginTransaction();
Query<Course> q = s.createQuery("from Course where id = :id", Course.class);
q.setParameter("id", courseId);
course = q.getSingleResult();
tx.commit();
} catch (HibernateException exception){
if(tx != null) tx.rollback();
exception.printStackTrace();
} finally {
s.close();
}
return course;
}

@Override
public List<Course> getAllCourses() {
Session s = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
List<Course> courseList = new ArrayList<>();

try{
tx = s.beginTransaction();
Query<Course> q = s.createQuery("from Course", Course.class);
courseList = q.getResultList();
tx.commit();
} catch (HibernateException exception){
if(tx != null) tx.rollback();
exception.printStackTrace();
} finally {
s.close();
}
return courseList;
}
}
Loading