|
| 1 | +package org.baeldung.persistence.model; |
| 2 | + |
| 3 | +import javax.persistence.Column; |
| 4 | +import javax.persistence.Entity; |
| 5 | +import javax.persistence.GeneratedValue; |
| 6 | +import javax.persistence.GenerationType; |
| 7 | +import javax.persistence.Id; |
| 8 | + |
| 9 | +@Entity |
| 10 | +public class Book { |
| 11 | + @Id |
| 12 | + @GeneratedValue(strategy = GenerationType.AUTO) |
| 13 | + private long id; |
| 14 | + |
| 15 | + @Column(nullable = false) |
| 16 | + private String title; |
| 17 | + |
| 18 | + @Column(nullable = false) |
| 19 | + private String isbn; |
| 20 | + |
| 21 | + @Column(nullable = false) |
| 22 | + private String author; |
| 23 | + |
| 24 | + // |
| 25 | + |
| 26 | + public Book() { |
| 27 | + super(); |
| 28 | + } |
| 29 | + |
| 30 | + public Book(String title, String isbn, String author) { |
| 31 | + super(); |
| 32 | + this.title = title; |
| 33 | + this.isbn = isbn; |
| 34 | + this.author = author; |
| 35 | + } |
| 36 | + |
| 37 | + // |
| 38 | + |
| 39 | + public long getId() { |
| 40 | + return id; |
| 41 | + } |
| 42 | + |
| 43 | + public void setId(long id) { |
| 44 | + this.id = id; |
| 45 | + } |
| 46 | + |
| 47 | + public String getTitle() { |
| 48 | + return title; |
| 49 | + } |
| 50 | + |
| 51 | + public void setTitle(String title) { |
| 52 | + this.title = title; |
| 53 | + } |
| 54 | + |
| 55 | + public String getIsbn() { |
| 56 | + return isbn; |
| 57 | + } |
| 58 | + |
| 59 | + public void setIsbn(String isbn) { |
| 60 | + this.isbn = isbn; |
| 61 | + } |
| 62 | + |
| 63 | + public String getAuthor() { |
| 64 | + return author; |
| 65 | + } |
| 66 | + |
| 67 | + public void setAuthor(String author) { |
| 68 | + this.author = author; |
| 69 | + } |
| 70 | + |
| 71 | + // |
| 72 | + |
| 73 | + @Override |
| 74 | + public int hashCode() { |
| 75 | + final int prime = 31; |
| 76 | + int result = 1; |
| 77 | + result = (prime * result) + ((author == null) ? 0 : author.hashCode()); |
| 78 | + result = (prime * result) + (int) (id ^ (id >>> 32)); |
| 79 | + result = (prime * result) + ((isbn == null) ? 0 : isbn.hashCode()); |
| 80 | + result = (prime * result) + ((title == null) ? 0 : title.hashCode()); |
| 81 | + return result; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public boolean equals(Object obj) { |
| 86 | + if (this == obj) { |
| 87 | + return true; |
| 88 | + } |
| 89 | + if (obj == null) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + if (getClass() != obj.getClass()) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + final Book other = (Book) obj; |
| 96 | + if (author == null) { |
| 97 | + if (other.author != null) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + } else if (!author.equals(other.author)) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + if (id != other.id) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + if (isbn == null) { |
| 107 | + if (other.isbn != null) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + } else if (!isbn.equals(other.isbn)) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + if (title == null) { |
| 114 | + if (other.title != null) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + } else if (!title.equals(other.title)) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public String toString() { |
| 125 | + final StringBuilder builder = new StringBuilder(); |
| 126 | + builder.append("Book [id=") |
| 127 | + .append(id) |
| 128 | + .append(", title=") |
| 129 | + .append(title) |
| 130 | + .append(", isbn=") |
| 131 | + .append(isbn) |
| 132 | + .append(", author=") |
| 133 | + .append(author) |
| 134 | + .append("]"); |
| 135 | + return builder.toString(); |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments