package kr.pe.kwonnam.hibernate4memcached.example.entity;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
/**
*
*/
@Entity
@Table(name = "books")
@NamedQuery(name = "Book.byEdition",
query = "from Book where edition=:edition",
hints = {
@QueryHint(name = "org.hibernate.cacheable", value = "true"),
@QueryHint(name = "org.hibernate.cacheRegion", value = "book-by-named-edition")
}
)
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = "books")
public class Book implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "book_id")
private long id;
@Column(name = "title", length = 128, nullable = false)
private String title;
@Column(name = "description", length = 1024, nullable = false)
private String description;
//
// @Column(name="test", nullable = true)
// private String test = "hello";
@Column(name = "edition", nullable = false)
private int edition = 1;
@Lob
@Column(name="contents", nullable = true, length = 60000)
private String contents;
@ManyToMany
@JoinTable(name = "book_authors", joinColumns = @JoinColumn(name = "book_id", referencedColumnName = "book_id"),
inverseJoinColumns = @JoinColumn(name = "author_id", referencedColumnName = "author_id"))
@Cache(region = "books_authors", usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public Collection<Author> authors;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "published_at", nullable = false)
private Date publishedAt;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getEdition() {
return edition;
}
public void setEdition(int edition) {
this.edition = edition;
}
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
public Collection<Author> getAuthors() {
return authors;
}
public void setAuthors(Collection<Author> authors) {
this.authors = authors;
}
public Date getPublishedAt() {
return publishedAt;
}
public void setPublishedAt(Date publishedAt) {
this.publishedAt = publishedAt;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
if (id != book.id) return false;
return true;
}
@Override
public int hashCode() {
return (int) (id ^ (id >>> 32));
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", title='" + title + '\'' +
", description='" + description + '\'' +
", edition=" + edition +
", authors=" + authors +
", publishedAt=" + publishedAt +
'}';
}
}