diff --git a/db/mysql-with-inserts.sql b/db/mysql-with-inserts.sql index a35a9527a562f1131d8191831126d9608f7dd436..9444668b00ca9bed9fbcf4551d6f8628cecd38d0 100644 --- a/db/mysql-with-inserts.sql +++ b/db/mysql-with-inserts.sql @@ -1,3 +1,4 @@ +DROP SCHEMA IF EXISTS `daaexample` ; CREATE DATABASE `daaexample`; CREATE TABLE `daaexample`.`people` ( @@ -14,6 +15,16 @@ CREATE TABLE `daaexample`.`users` ( PRIMARY KEY (`login`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +CREATE TABLE `daaexample`.`pet` ( + `id` int NOT NULL AUTO_INCREMENT, + `name` varchar(50) NOT NULL, + `breed` varchar(50) NOT NULL, + `idOwner` int NOT NULL, + PRIMARY KEY (`id`), + FOREIGN KEY (`idOwner`) REFERENCES `people`(`id`) + +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + GRANT ALL ON `daaexample`.* TO 'daa'@'localhost' IDENTIFIED BY 'daa'; INSERT INTO `daaexample`.`people` (`id`,`name`,`surname`) VALUES (0,'Antón','Pérez'); @@ -30,3 +41,7 @@ INSERT INTO `daaexample`.`users` (`login`,`password`,`role`) VALUES ('admin', '713bfda78870bf9d1b261f565286f85e97ee614efe5f0faf7c34e7ca4f65baca','ADMIN'); INSERT INTO `daaexample`.`users` (`login`,`password`,`role`) VALUES ('normal', '7bf24d6ca2242430343ab7e3efb89559a47784eea1123be989c1b2fb2ef66e83','USER'); + +INSERT INTO `daaexample`.`pet` (`id`,`name`,`breed`,`idOwner`) VALUES (0,'Rex','Pastor Aleman',1); +INSERT INTO `daaexample`.`pet` (`id`,`name`,`breed`,`idOwner`) VALUES (0,'Idefix','Terrier',2); +INSERT INTO `daaexample`.`pet` (`id`,`name`,`breed`,`idOwner`) VALUES (0,'Snoopy','Beagle',3); \ No newline at end of file diff --git a/db/mysql.sql b/db/mysql.sql index ed7cd936f0e30ae075eef4ed0260ba9526e61245..c1026070d981f998470333092918ac964d4b8af9 100644 --- a/db/mysql.sql +++ b/db/mysql.sql @@ -1,3 +1,4 @@ +DROP SCHEMA IF EXISTS `daaexample` ; CREATE DATABASE `daaexample`; CREATE TABLE `daaexample`.`people` ( @@ -14,4 +15,14 @@ CREATE TABLE `daaexample`.`users` ( PRIMARY KEY (`login`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +CREATE TABLE `daaexample`.`pet` ( + `id` int NOT NULL AUTO_INCREMENT, + `name` varchar(50) NOT NULL, + `breed` varchar(50) NOT NULL, + `idOwner` int NOT NULL, + PRIMARY KEY (`id`), + FOREIGN KEY (`idOwner`) REFERENCES `people`(`id`) + +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + GRANT ALL ON `daaexample`.* TO 'daa'@'localhost' IDENTIFIED BY 'daa'; diff --git a/src/main/java/es/uvigo/esei/daa/entities/Pet.java b/src/main/java/es/uvigo/esei/daa/entities/Pet.java new file mode 100644 index 0000000000000000000000000000000000000000..f806a6821b849d2bffaa4a7d3c350e65ad76614e --- /dev/null +++ b/src/main/java/es/uvigo/esei/daa/entities/Pet.java @@ -0,0 +1,131 @@ +package es.uvigo.esei.daa.entities; + +import static java.util.Objects.requireNonNull; + +/** + * An entity that represents a pet. + * + * @author Miguel Ferreiro Diaz + */ +public class Pet { + private int id; + private String name; + private String breed; + private int idOwner; + + // Constructor needed for the JSON conversion + Pet() {} + + /** + * Constructs a new instance of {@link Person}. + * + * @param id identifier of the pet. + * @param name name of the pet. + * @param breed breed of the pet. + * @param idOwner owner of the pet + */ + public Pet(int id, String name, String breed, int idOwner) { + this.id = id; + this.setName(name); + this.setBreed(breed); + this.setIdOwner(idOwner); + } + + /** + * Returns the identifier of the pet. + * + * @return the identifier of the pet. + */ + public int getId() { + return id; + } + /** + * Returns the name of the pet. + * + * @return the name of the pet. + */ + public String getName() { + return name; + } + /** + * Set the name of this pet. + * + * @param name the new name of the pet. + * @throws NullPointerException if the {@code name} is {@code null}. + */ + public void setName(String name) { + this.name = requireNonNull(name, "Name can't be null"); + } + /** + * Returns the breed of the pet. + * + * @return the breed of the pet. + */ + public String getBreed() { + return breed; + } + /** + * Set the breed of this pet. + * + * @param breed the new breed of the person. + * @throws NullPointerException if the {@code breed} is {@code null}. + */ + public void setBreed(String breed) { + this.breed = requireNonNull(breed, "Name can't be null"); + } + + /** + * Returns the owner of the pet + * + * @return the owner of the pet + */ + public int getIdOwner() { + return idOwner; + } + /** + * Set the owner of the pet + * + * @param idOwner the new owner of the pet + * @throws NullPointerException if the {@code idOwner} is {@code null}. + */ + public void setIdOwner(int idOwner) { + this.idOwner = requireNonNull(idOwner, "Name can't be null"); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((breed == null) ? 0 : breed.hashCode()); + result = prime * result + id; + result = prime * result + idOwner; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Pet other = (Pet) obj; + if (breed == null) { + if (other.breed != null) + return false; + } else if (!breed.equals(other.breed)) + return false; + if (id != other.id) + return false; + if (idOwner != other.idOwner) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } +}