Skip to content

Instantly share code, notes, and snippets.

@dodangquan
Created January 8, 2016 10:14
Show Gist options
  • Save dodangquan/dba5fddba3351501600b to your computer and use it in GitHub Desktop.
Save dodangquan/dba5fddba3351501600b to your computer and use it in GitHub Desktop.
Project BookStore
package com.dangquan.webapp.bookstore.entity;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hibernate.validator.constraints.NotEmpty;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Set;
/**
* Created by dangquan on Dec 29, 2015
* Email: dangquanzt@gmail.com
*/
@Entity
@Table(name = "tbl_book")
public class Book implements Serializable {
private static final long serialVersionUID = 2784896266770837325L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "book_id")
private long id;
@NotNull
@NotEmpty
@Column(name = "title", nullable = false, unique = true)
private String title;
@NotNull
@NotEmpty
@Min(value = 2000)
@Column(name = "year", nullable = false)
private int year;
@Column(name = "isbn", nullable = false, unique = true)
private String isbn;
@Column(name = "price", nullable = false)
private BigDecimal price;
/*
* @formatter:off
*/
@NotNull
@ManyToMany(fetch = FetchType.EAGER )
@JoinTable(name = "tbl_author_book",
joinColumns = { @JoinColumn(name = "book_id") },
inverseJoinColumns = {@JoinColumn(name = "author_id") })
private Set<Author> authors;
/*
* @formatter:on
*/
@ManyToMany(mappedBy = "books", fetch = FetchType.LAZY)
private Set<Category> categories;
@Column(name = "last_update", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date lastUpdate;
@Column(name = "description", nullable = false, unique = true)
private String description;
public Book(Book source) {
this.title = source.getTitle();
this.isbn = source.getIsbn();
this.authors = source.getAuthors();
this.categories = source.getCategories();
this.description = source.getDescription();
this.lastUpdate = source.getLastUpdate();
this.price = source.getPrice();
this.year = source.getYear();
}
public Book() {
super();
}
public Date getLastUpdate() {
return lastUpdate;
}
public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public long getId() {
return id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public Set<Author> getAuthors() {
return authors;
}
public void setAuthors(Set<Author> authors) {
this.authors = authors;
}
public Set<Category> getCategories() {
return categories;
}
public void setCategories(Set<Category> categories) {
this.categories = categories;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof Book)) {
return false;
}
if (this == obj) {
return true;
}
Book other = (Book) obj;
EqualsBuilder equalsBuilder = new EqualsBuilder();
return equalsBuilder.append(this.getTitle(), other.getTitle()).append(this.getAuthors(), other.getAuthors())
.isEquals();
}
@Override
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
builder.append("id", this.getId());
builder.append("title", this.getTitle());
builder.append("authors", this.authors.toArray());
builder.append("isbn", this.getIsbn());
builder.append("year", this.getYear());
builder.append("price", this.getPrice());
return builder.toString();
}
@Override
public int hashCode() {
HashCodeBuilder builder = new HashCodeBuilder();
return builder.append(this.getTitle()).append(this.getAuthors()).hashCode();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment