From 1ca4e6f20d6365dd94be9ce00f9fde2bbf16dc32 Mon Sep 17 00:00:00 2001 From: Konstantinos Vitouladitis Date: Mon, 9 Dec 2019 20:50:05 +0200 Subject: [PATCH 1/2] new final Dump --- Dump20191209.sql | 170 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 Dump20191209.sql diff --git a/Dump20191209.sql b/Dump20191209.sql new file mode 100644 index 0000000..1649d13 --- /dev/null +++ b/Dump20191209.sql @@ -0,0 +1,170 @@ +-- MySQL dump 10.13 Distrib 8.0.18, for Win64 (x86_64) +-- +-- Host: 127.0.0.1 Database: efkadb +-- ------------------------------------------------------ +-- Server version 8.0.18 + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!50503 SET NAMES utf8 */; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +-- +-- Table structure for table `appointment` +-- + +DROP TABLE IF EXISTS `appointment`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!50503 SET character_set_client = utf8mb4 */; +CREATE TABLE `appointment` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `citizen_id` int(10) unsigned NOT NULL, + `doctor_id` int(10) unsigned NOT NULL, + `datetime` datetime NOT NULL, + `description` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci, + `notes` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci, + PRIMARY KEY (`id`), + KEY `cit_app_idx` (`citizen_id`), + KEY `doc_app_idx` (`doctor_id`), + CONSTRAINT `cit_app` FOREIGN KEY (`citizen_id`) REFERENCES `citizen` (`id`), + CONSTRAINT `doc_app` FOREIGN KEY (`doctor_id`) REFERENCES `doctor` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `appointment` +-- + +LOCK TABLES `appointment` WRITE; +/*!40000 ALTER TABLE `appointment` DISABLE KEYS */; +/*!40000 ALTER TABLE `appointment` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `citizen` +-- + +DROP TABLE IF EXISTS `citizen`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!50503 SET character_set_client = utf8mb4 */; +CREATE TABLE `citizen` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `user_id` int(10) unsigned NOT NULL, + `AMKA` int(11) unsigned NOT NULL, + `first_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `last_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `email` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `phone_number` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `AMKA_UNIQUE` (`AMKA`), + UNIQUE KEY `user_id_UNIQUE` (`user_id`), + CONSTRAINT `cit_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `citizen` +-- + +LOCK TABLES `citizen` WRITE; +/*!40000 ALTER TABLE `citizen` DISABLE KEYS */; +/*!40000 ALTER TABLE `citizen` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `doctor` +-- + +DROP TABLE IF EXISTS `doctor`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!50503 SET character_set_client = utf8mb4 */; +CREATE TABLE `doctor` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `user_id` int(10) unsigned NOT NULL, + `first_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `last_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `specialty_id` int(10) unsigned DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `user_id_UNIQUE` (`user_id`), + KEY `fk_spec_idx` (`specialty_id`), + CONSTRAINT `doc_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`), + CONSTRAINT `fk_spec` FOREIGN KEY (`specialty_id`) REFERENCES `specialty` (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `doctor` +-- + +LOCK TABLES `doctor` WRITE; +/*!40000 ALTER TABLE `doctor` DISABLE KEYS */; +/*!40000 ALTER TABLE `doctor` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `specialty` +-- + +DROP TABLE IF EXISTS `specialty`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!50503 SET character_set_client = utf8mb4 */; +CREATE TABLE `specialty` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `specialty` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `specialty` +-- + +LOCK TABLES `specialty` WRITE; +/*!40000 ALTER TABLE `specialty` DISABLE KEYS */; +INSERT INTO `specialty` VALUES (7,'Ksematiasma'),(8,'Ksematiasma'),(9,'Ksematiasma'),(10,'Ksematiasma'),(11,'Ksematiasma'),(12,'Ksematiasma'),(13,'Ksematiasma'),(14,'Ksematiasma'),(15,'Ksematiasma'); +/*!40000 ALTER TABLE `specialty` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `user` +-- + +DROP TABLE IF EXISTS `user`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!50503 SET character_set_client = utf8mb4 */; +CREATE TABLE `user` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `type` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL, + `username` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, + `pwd` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `username_UNIQUE` (`username`) +) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `user` +-- + +LOCK TABLES `user` WRITE; +/*!40000 ALTER TABLE `user` DISABLE KEYS */; +INSERT INTO `user` VALUES (14,'d','username23j','password'),(16,'d','usernamess23j','password'),(17,'d','usernamess423j','password'),(18,'d','usernamwsess423j','password'),(19,'d','usernamwsejjss423j','password'),(21,'d','usernsedededmwsejjss423j','password'),(22,'d','usdedmwsejjss423j','password'),(24,'d','usdess423j','password'),(26,'d','usdesfdffs423j','password'); +/*!40000 ALTER TABLE `user` ENABLE KEYS */; +UNLOCK TABLES; +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +-- Dump completed on 2019-12-09 18:39:33 From 3a5bcf0568ae5a9301bf9ad701ad1b4799401c36 Mon Sep 17 00:00:00 2001 From: Konstantinos Vitouladitis Date: Tue, 10 Dec 2019 17:51:23 +0200 Subject: [PATCH 2/2] JPA Working --- .../sample/Entities/Appointment.java | 66 ++++++----- .../regeneration/sample/Entities/Citizen.java | 110 +++++++++++------- .../regeneration/sample/Entities/Doctor.java | 98 +++++++++++----- .../sample/Entities/Specialty.java | 42 +++++-- .../regeneration/sample/Entities/User.java | 90 +++++++++----- .../Repositories/CitizenRepository.java | 7 ++ .../sample/Repositories/DoctorRepository.java | 7 ++ .../Repositories/SpecialtyRepository.java | 7 ++ .../sample/Repositories/UserRepository.java | 7 ++ .../sample/SampleApplication.java | 50 ++++++-- .../src/main/resources/application.properties | 4 +- 11 files changed, 332 insertions(+), 156 deletions(-) create mode 100644 sample/src/main/java/org/regeneration/sample/Repositories/CitizenRepository.java create mode 100644 sample/src/main/java/org/regeneration/sample/Repositories/DoctorRepository.java create mode 100644 sample/src/main/java/org/regeneration/sample/Repositories/SpecialtyRepository.java create mode 100644 sample/src/main/java/org/regeneration/sample/Repositories/UserRepository.java diff --git a/sample/src/main/java/org/regeneration/sample/Entities/Appointment.java b/sample/src/main/java/org/regeneration/sample/Entities/Appointment.java index 18e6bdc..2674e61 100644 --- a/sample/src/main/java/org/regeneration/sample/Entities/Appointment.java +++ b/sample/src/main/java/org/regeneration/sample/Entities/Appointment.java @@ -6,17 +6,33 @@ @Entity public class Appointment { + @Id + @Column(name = "id", nullable = false) private int id; + + @Column(name = "citizen_id", nullable = false, insertable = false, updatable = false) private int citizenId; + + @Column(name = "doctor_id", nullable = false, insertable = false, updatable = false) private int doctorId; - private Timestamp appointmentDatetime; + + @Column(name = "datetime", nullable = false) + private Timestamp datetime; + + @Column(name = "description", nullable = true, columnDefinition = "TEXT") private String description; + + @Column(name = "notes", nullable = true, columnDefinition = "TEXT") private String notes; - private Citizen citizenByCitizenId; - private Doctor doctorByDoctorId; - @Id - @Column(name = "id", nullable = false) + @ManyToOne + @JoinColumn(name = "citizen_id", referencedColumnName = "id", nullable = false) + private Citizen citizen; + + @ManyToOne + @JoinColumn(name = "doctor_id", referencedColumnName = "id", nullable = false) + private Doctor doctor; + public int getId() { return id; } @@ -25,8 +41,6 @@ public void setId(int id) { this.id = id; } - @Basic - @Column(name = "citizen_id", nullable = false, insertable = false, updatable = false) public int getCitizenId() { return citizenId; } @@ -35,8 +49,6 @@ public void setCitizenId(int citizenId) { this.citizenId = citizenId; } - @Basic - @Column(name = "doctor_id", nullable = false, insertable = false, updatable = false) public int getDoctorId() { return doctorId; } @@ -45,18 +57,14 @@ public void setDoctorId(int doctorId) { this.doctorId = doctorId; } - @Basic - @Column(name = "appointment_datetime", nullable = false) - public Timestamp getAppointmentDatetime() { - return appointmentDatetime; + public Timestamp getDatetime() { + return datetime; } - public void setAppointmentDatetime(Timestamp appointmentDatetime) { - this.appointmentDatetime = appointmentDatetime; + public void setDatetime(Timestamp datetime) { + this.datetime = datetime; } - @Basic - @Column(name = "description", nullable = true) public String getDescription() { return description; } @@ -65,8 +73,6 @@ public void setDescription(String description) { this.description = description; } - @Basic - @Column(name = "notes", nullable = true) public String getNotes() { return notes; } @@ -83,33 +89,29 @@ public boolean equals(Object o) { return id == that.id && citizenId == that.citizenId && doctorId == that.doctorId && - Objects.equals(appointmentDatetime, that.appointmentDatetime) && + Objects.equals(datetime, that.datetime) && Objects.equals(description, that.description) && Objects.equals(notes, that.notes); } @Override public int hashCode() { - return Objects.hash(id, citizenId, doctorId, appointmentDatetime, description, notes); + return Objects.hash(id, citizenId, doctorId, datetime, description, notes); } - @ManyToOne - @JoinColumn(name = "citizen_id", referencedColumnName = "id", nullable = false) - public Citizen getCitizenByCitizenId() { - return citizenByCitizenId; + public Citizen getCitizen() { + return citizen; } - public void setCitizenByCitizenId(Citizen citizenByCitizenId) { - this.citizenByCitizenId = citizenByCitizenId; + public void setCitizenByCitizenId(Citizen citizen) { + this.citizen = citizen; } - @ManyToOne - @JoinColumn(name = "doctor_id", referencedColumnName = "id", nullable = false) public Doctor getDoctorByDoctorId() { - return doctorByDoctorId; + return doctor; } - public void setDoctorByDoctorId(Doctor doctorByDoctorId) { - this.doctorByDoctorId = doctorByDoctorId; + public void setDoctorByDoctorId(Doctor doctor) { + this.doctor = doctor; } } diff --git a/sample/src/main/java/org/regeneration/sample/Entities/Citizen.java b/sample/src/main/java/org/regeneration/sample/Entities/Citizen.java index ba95667..86de453 100644 --- a/sample/src/main/java/org/regeneration/sample/Entities/Citizen.java +++ b/sample/src/main/java/org/regeneration/sample/Entities/Citizen.java @@ -1,22 +1,57 @@ package org.regeneration.sample.Entities; +import org.springframework.beans.factory.annotation.Autowired; + import javax.persistence.*; import java.util.Collection; +import java.util.Map; import java.util.Objects; @Entity public class Citizen { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) private int id; + + @Column(name = "user_id", nullable = false, insertable = false, updatable = false) + private int userId; + + @Column(name = "amka", nullable = false, unique = true) private int amka; + + @Column(name = "first_name", length = 32) private String firstName; + + @Column(name = "last_name", length = 32) private String lastName; + + @Column(name = "email", nullable = false, length = 50) private String email; - private String phoneNumner; - private Collection appointmentsById; - private User userById; - @Id - @Column(name = "id", nullable = false) + @Column(name = "phone_number", length = 11) + private String phoneNumber; + + @OneToMany(mappedBy = "citizen") + private Collection appointments; + + @OneToOne + @JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false) + private User user; + + public Citizen() { + } + + public Citizen(User user, int amka, String firstName, String lastName, String email, String phoneNumber) { + this.user = user; + this.amka = amka; + this.firstName = firstName; + this.lastName = lastName; + this.email = email; + this.phoneNumber = phoneNumber; + } + + public int getId() { return id; } @@ -25,8 +60,14 @@ public void setId(int id) { this.id = id; } - @Basic - @Column(name = "AMKA", nullable = false) + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } + public int getAmka() { return amka; } @@ -35,8 +76,6 @@ public void setAmka(int amka) { this.amka = amka; } - @Basic - @Column(name = "first_name", nullable = true, length = 32) public String getFirstName() { return firstName; } @@ -45,8 +84,6 @@ public void setFirstName(String firstName) { this.firstName = firstName; } - @Basic - @Column(name = "last_name", nullable = true, length = 32) public String getLastName() { return lastName; } @@ -55,8 +92,6 @@ public void setLastName(String lastName) { this.lastName = lastName; } - @Basic - @Column(name = "email", nullable = false, length = 50) public String getEmail() { return email; } @@ -65,14 +100,28 @@ public void setEmail(String email) { this.email = email; } - @Basic - @Column(name = "phone_numner", nullable = true, length = 11) - public String getPhoneNumner() { - return phoneNumner; + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + public Collection getAppointmentsById() { + return appointments; + } + + public void setAppointmentsById(Collection appointments) { + this.appointments = appointments; } - public void setPhoneNumner(String phoneNumner) { - this.phoneNumner = phoneNumner; + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; } @Override @@ -81,34 +130,17 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; Citizen citizen = (Citizen) o; return id == citizen.id && + userId == citizen.userId && amka == citizen.amka && Objects.equals(firstName, citizen.firstName) && Objects.equals(lastName, citizen.lastName) && Objects.equals(email, citizen.email) && - Objects.equals(phoneNumner, citizen.phoneNumner); + Objects.equals(phoneNumber, citizen.phoneNumber); } @Override public int hashCode() { - return Objects.hash(id, amka, firstName, lastName, email, phoneNumner); - } - - @OneToMany(mappedBy = "citizenByCitizenId") - public Collection getAppointmentsById() { - return appointmentsById; + return Objects.hash(id, userId, amka, firstName, lastName, email, phoneNumber); } - public void setAppointmentsById(Collection appointmentsById) { - this.appointmentsById = appointmentsById; - } - - @OneToOne - @JoinColumn(name = "id", referencedColumnName = "id", nullable = false) - public User getUserById() { - return userById; - } - - public void setUserById(User userById) { - this.userById = userById; - } } diff --git a/sample/src/main/java/org/regeneration/sample/Entities/Doctor.java b/sample/src/main/java/org/regeneration/sample/Entities/Doctor.java index af834ff..6ef7bf1 100644 --- a/sample/src/main/java/org/regeneration/sample/Entities/Doctor.java +++ b/sample/src/main/java/org/regeneration/sample/Entities/Doctor.java @@ -1,21 +1,52 @@ package org.regeneration.sample.Entities; import javax.persistence.*; +import java.util.ArrayList; import java.util.Collection; import java.util.Objects; @Entity public class Doctor { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) private int id; + + @Column(name = "user_id", nullable = false, insertable = false, updatable = false) + private int userId; + + @Column(name = "first_name", length = 32) private String firstName; + + @Column(name = "last_name", length = 32) private String lastName; - private int specialtyId; - private Collection appointmentsById; - private User userById; - private Specialty specialtyBySpecialtyId; - @Id - @Column(name = "id", nullable = false) + @OneToMany(mappedBy = "doctor") + private Collection appointments; + + @OneToOne + @JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false) + private User user; + + @Column(name = "specialty_id", nullable = false, insertable = false, updatable = false) + private int specialityId; + + @ManyToOne + @JoinColumn(name = "specialty_id", referencedColumnName = "id", nullable = false) + private Specialty specialty; + + public Doctor() { + } + + public Doctor(User user, String firstName, String lastName, Specialty specialty) { + this.user = user; + this.userId=user.getId(); + this.firstName = firstName; + this.lastName = lastName; + this.specialty = specialty; + } + + public int getId() { return id; } @@ -24,8 +55,16 @@ public void setId(int id) { this.id = id; } - @Basic - @Column(name = "first_name", nullable = true, length = 32) + + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + public String getFirstName() { return firstName; } @@ -34,8 +73,7 @@ public void setFirstName(String firstName) { this.firstName = firstName; } - @Basic - @Column(name = "last_name", nullable = true, length = 32) + public String getLastName() { return lastName; } @@ -44,14 +82,12 @@ public void setLastName(String lastName) { this.lastName = lastName; } - @Basic - @Column(name = "specialty_id", nullable = false, insertable = false, updatable = false) public int getSpecialtyId() { - return specialtyId; + return specialityId; } public void setSpecialtyId(int specialtyId) { - this.specialtyId = specialtyId; + this.specialityId = specialityId; } @Override @@ -60,42 +96,40 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; Doctor doctor = (Doctor) o; return id == doctor.id && - specialtyId == doctor.specialtyId && + userId == doctor.userId && Objects.equals(firstName, doctor.firstName) && Objects.equals(lastName, doctor.lastName); } @Override public int hashCode() { - return Objects.hash(id, firstName, lastName, specialtyId); + return Objects.hash(id, userId, firstName, lastName); } - @OneToMany(mappedBy = "doctorByDoctorId") public Collection getAppointmentsById() { - return appointmentsById; + return appointments; } - public void setAppointmentsById(Collection appointmentsById) { - this.appointmentsById = appointmentsById; + public void setAppointmentsById(Collection appointments) { + this.appointments = appointments; } - @OneToOne - @JoinColumn(name = "id", referencedColumnName = "id", nullable = false) - public User getUserById() { - return userById; + public User getUser() { + return user; } - public void setUserById(User userById) { - this.userById = userById; + public void setUser(User user) { + this.user = user; } - @ManyToOne - @JoinColumn(name = "specialty_id", referencedColumnName = "id", nullable = false) - public Specialty getSpecialtyBySpecialtyId() { - return specialtyBySpecialtyId; + + public Specialty getSpecialty() { + return specialty; } - public void setSpecialtyBySpecialtyId(Specialty specialtyBySpecialtyId) { - this.specialtyBySpecialtyId = specialtyBySpecialtyId; + public void setSpecialty(Specialty specialty) { + this.specialty.getDoctors().remove(this); + this.specialty = specialty; + this.specialty.getDoctors().add(this); } } diff --git a/sample/src/main/java/org/regeneration/sample/Entities/Specialty.java b/sample/src/main/java/org/regeneration/sample/Entities/Specialty.java index 0ea04a4..bdd141e 100644 --- a/sample/src/main/java/org/regeneration/sample/Entities/Specialty.java +++ b/sample/src/main/java/org/regeneration/sample/Entities/Specialty.java @@ -1,17 +1,34 @@ package org.regeneration.sample.Entities; import javax.persistence.*; +import javax.print.Doc; +import java.util.ArrayList; import java.util.Collection; import java.util.Objects; @Entity public class Specialty { + @Id + @Column(name = "id", nullable = false) + @GeneratedValue(strategy = GenerationType.AUTO) private int id; + + @Column(name = "specialty", nullable = false, length = 50) private String specialty; - private Collection doctorsById; - @Id - @Column(name = "id", nullable = false) + @OneToMany(mappedBy = "specialty") + private Collection doctors; + + public Specialty() { + } + + + public Specialty(String specialty) { + this.specialty=specialty; + doctors = new ArrayList<>(); + } + + public int getId() { return id; } @@ -20,8 +37,6 @@ public void setId(int id) { this.id = id; } - @Basic - @Column(name = "specialty", nullable = false, length = 50) public String getSpecialty() { return specialty; } @@ -30,6 +45,15 @@ public void setSpecialty(String specialty) { this.specialty = specialty; } + public Collection getDoctors() { + return doctors; + } + + public void setDoctors(Collection doctors) { + this.doctors = doctors; + } + + @Override public boolean equals(Object o) { if (this == o) return true; @@ -44,12 +68,6 @@ public int hashCode() { return Objects.hash(id, specialty); } - @OneToMany(mappedBy = "specialtyBySpecialtyId") - public Collection getDoctorsById() { - return doctorsById; - } - public void setDoctorsById(Collection doctorsById) { - this.doctorsById = doctorsById; - } + } diff --git a/sample/src/main/java/org/regeneration/sample/Entities/User.java b/sample/src/main/java/org/regeneration/sample/Entities/User.java index c6da3fa..acf2161 100644 --- a/sample/src/main/java/org/regeneration/sample/Entities/User.java +++ b/sample/src/main/java/org/regeneration/sample/Entities/User.java @@ -1,18 +1,42 @@ package org.regeneration.sample.Entities; +import org.springframework.beans.factory.annotation.Autowired; + import javax.persistence.*; +import java.util.Collection; import java.util.Objects; @Entity public class User { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) private int id; + + @Column(name = "type", nullable = false, columnDefinition = "CHAR(1)") + private String type; + + @Column(name = "username", nullable = false, length = 32, unique = true) private String username; - private String pwd; - private Citizen citizenById; - private Doctor doctorById; - @Id - @Column(name = "id", nullable = false) + @Column(name = "pwd", nullable = false, length = 32) + private String password; + + @OneToOne(mappedBy = "user") + private Citizen citizen; + + @OneToOne(mappedBy = "user") + private Doctor doctor; + + public User() { + } + + public User(String type, String username, String pwd) { + this.type=type; + this.username=username; + this.password=pwd; + } + public int getId() { return id; } @@ -21,8 +45,15 @@ public void setId(int id) { this.id = id; } - @Basic - @Column(name = "username", nullable = false, length = 32) + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getUsername() { return username; } @@ -31,14 +62,28 @@ public void setUsername(String username) { this.username = username; } - @Basic - @Column(name = "pwd", nullable = false, length = 32) public String getPwd() { - return pwd; + return password; } public void setPwd(String pwd) { - this.pwd = pwd; + this.password = pwd; + } + + public Citizen getCitizen() { + return citizen; + } + + public void setCitizen(Citizen citizen) { + this.citizen = citizen; + } + + public Doctor getDoctor() { + return doctor; + } + + public void setDoctor(Doctor doctor) { + this.doctor = doctor; } @Override @@ -47,30 +92,13 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; User user = (User) o; return id == user.id && + Objects.equals(type, user.type) && Objects.equals(username, user.username) && - Objects.equals(pwd, user.pwd); + Objects.equals(password, user.password); } @Override public int hashCode() { - return Objects.hash(id, username, pwd); - } - - @OneToOne(mappedBy = "userById") - public Citizen getCitizenById() { - return citizenById; - } - - public void setCitizenById(Citizen citizenById) { - this.citizenById = citizenById; - } - - @OneToOne(mappedBy = "userById") - public Doctor getDoctorById() { - return doctorById; - } - - public void setDoctorById(Doctor doctorById) { - this.doctorById = doctorById; + return Objects.hash(id, type, username, password); } } diff --git a/sample/src/main/java/org/regeneration/sample/Repositories/CitizenRepository.java b/sample/src/main/java/org/regeneration/sample/Repositories/CitizenRepository.java new file mode 100644 index 0000000..2210c95 --- /dev/null +++ b/sample/src/main/java/org/regeneration/sample/Repositories/CitizenRepository.java @@ -0,0 +1,7 @@ +package org.regeneration.sample.Repositories; + +import org.regeneration.sample.Entities.Citizen; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface CitizenRepository extends JpaRepository { +} \ No newline at end of file diff --git a/sample/src/main/java/org/regeneration/sample/Repositories/DoctorRepository.java b/sample/src/main/java/org/regeneration/sample/Repositories/DoctorRepository.java new file mode 100644 index 0000000..1e08504 --- /dev/null +++ b/sample/src/main/java/org/regeneration/sample/Repositories/DoctorRepository.java @@ -0,0 +1,7 @@ +package org.regeneration.sample.Repositories; + +import org.regeneration.sample.Entities.Doctor; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface DoctorRepository extends JpaRepository { +} diff --git a/sample/src/main/java/org/regeneration/sample/Repositories/SpecialtyRepository.java b/sample/src/main/java/org/regeneration/sample/Repositories/SpecialtyRepository.java new file mode 100644 index 0000000..156a90a --- /dev/null +++ b/sample/src/main/java/org/regeneration/sample/Repositories/SpecialtyRepository.java @@ -0,0 +1,7 @@ +package org.regeneration.sample.Repositories; + +import org.regeneration.sample.Entities.Specialty; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface SpecialtyRepository extends JpaRepository { +} diff --git a/sample/src/main/java/org/regeneration/sample/Repositories/UserRepository.java b/sample/src/main/java/org/regeneration/sample/Repositories/UserRepository.java new file mode 100644 index 0000000..5a50d90 --- /dev/null +++ b/sample/src/main/java/org/regeneration/sample/Repositories/UserRepository.java @@ -0,0 +1,7 @@ +package org.regeneration.sample.Repositories; + +import org.regeneration.sample.Entities.User; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { +} diff --git a/sample/src/main/java/org/regeneration/sample/SampleApplication.java b/sample/src/main/java/org/regeneration/sample/SampleApplication.java index 07a2ec0..02e955d 100644 --- a/sample/src/main/java/org/regeneration/sample/SampleApplication.java +++ b/sample/src/main/java/org/regeneration/sample/SampleApplication.java @@ -1,21 +1,55 @@ package org.regeneration.sample; +import org.regeneration.sample.Entities.Citizen; +import org.regeneration.sample.Entities.Doctor; +import org.regeneration.sample.Entities.Specialty; +import org.regeneration.sample.Entities.User; +import org.regeneration.sample.Repositories.CitizenRepository; +import org.regeneration.sample.Repositories.DoctorRepository; +import org.regeneration.sample.Repositories.SpecialtyRepository; +import org.regeneration.sample.Repositories.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import java.util.ArrayList; +import java.util.List; + @SpringBootApplication -public class SampleApplication { +public class SampleApplication implements CommandLineRunner { + + private final DoctorRepository docRep; + private final CitizenRepository citRep; + private final SpecialtyRepository specRep; + private final UserRepository userRep; + + @Autowired + public SampleApplication(DoctorRepository docRep, CitizenRepository citRep, SpecialtyRepository specRep, UserRepository userRep) { + this.docRep = docRep; + this.citRep = citRep; + this.specRep = specRep; + this.userRep = userRep; + } public static void main(String[] args) { SpringApplication.run(SampleApplication.class, args); - /*Specialty spec = new Specialty(); - spec.setSpecialty("kardio"); - Doctor doctor = new Doctor(); - doctor.setFirstName("fname"); - doctor.setLastName("lname"); - doctor.setSpecialtyId(1);*/ - } + @Override + public void run(String... args) throws Exception { + User user = new User("c", "hh", "password"); + userRep.save(user); + + Citizen cit = new Citizen(user,19955,"kalo","paidi","mail@e.com","Thong"); + citRep.save(cit); + + user.setCitizen(cit); + + userRep.save(user); + + + + } } diff --git a/sample/src/main/resources/application.properties b/sample/src/main/resources/application.properties index b38f994..65cb120 100644 --- a/sample/src/main/resources/application.properties +++ b/sample/src/main/resources/application.properties @@ -1,7 +1,7 @@ -spring.datasource.url=jdbc:mysql://localhost:3306/jpa?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC +spring.datasource.url=jdbc:mysql://localhost:3306/efkadb?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=1234 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect -spring.jpa.hibernate.ddl-auto=create \ No newline at end of file +spring.jpa.hibernate.ddl-auto=update \ No newline at end of file