/*
* (c) Copyright 2005-2012 JAXIO, www.jaxio.com
* Source code generated by Celerio, a Jaxio product
* Want to use Celerio within your company? email us at info@jaxio.com
* Follow us on twitter: @springfuse
* Template pack-backend-sd:src/main/java/project/domain/Entity.e.vm.java
*/
package com.company.demo.domain;
import com.google.common.base.Objects;
import javax.xml.bind.annotation.XmlTransient;
import static javax.persistence.CascadeType.ALL;
import static javax.persistence.CascadeType.PERSIST;
import static javax.persistence.EnumType.STRING;
import static javax.persistence.FetchType.LAZY;
import static javax.persistence.TemporalType.TIMESTAMP;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.validation.constraints.Size;
import org.apache.log4j.Logger;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.data.domain.Persistable;
import com.company.demo.domain.Address;
import com.company.demo.domain.Book;
import com.company.demo.domain.Document;
import com.company.demo.domain.Role;
import com.company.demo.domain.enums.Civility;
@Entity
@Table(name = "ACCOUNT")
public class Account implements Persistable<String> {
private static final long serialVersionUID = 1L;
@SuppressWarnings("unused")
private static final Logger log = Logger.getLogger(Account.class);
// Raw attributes
private String id; // pk
private String username; // unique (not null)
private String password; // not null
private String email; // unique (not null)
private Boolean isEnabled;
private Civility civility;
private String firstName;
private String lastName;
private Date birthDate;
private Integer version;
// Technical attributes for query by example
private Integer addressId;
// Many to one
private Address homeAddress; // (addressId)
// One to many
private List<Book> books = new ArrayList<Book>();
private List<Document> documents = new ArrayList<Document>();
// Many to many
private List<Role> roles = new ArrayList<Role>();
// ---------------------------
// Constructors
// ---------------------------
public Account() {
}
public Account(String primaryKey) {
setId(primaryKey);
}
@Override
@Transient
@XmlTransient
public boolean isNew() {
return getId() == null;
}
// -------------------------------
// Role names support
// -------------------------------
/**
* Returns the granted authorities for this user. You may override
* this method to provide your own custom authorities.
*/
@Transient
@XmlTransient
public List<String> getRoleNames() {
List<String> roleNames = new ArrayList<String>();
for (Role role : getRoles()) {
roleNames.add(role.getRoleName());
}
return roleNames;
}
// -------------------------------
// Getter & Setter
// -------------------------------
// -- [id] ------------------------
@Column(name = "ID", length = 32)
@GeneratedValue(generator = "strategy-uuid")
@GenericGenerator(name = "strategy-uuid", strategy = "uuid")
@Id
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
// -- [username] ------------------------
@NotEmpty
@Size(min = 4, max = 255)
@Column(name = "login", nullable = false, unique = true)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
// -- [password] ------------------------
@NotEmpty
@Size(max = 255)
@Column(name = "`PASSWORD`", nullable = false)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
// -- [email] ------------------------
@NotEmpty
@Size(max = 255)
@Email
@Column(nullable = false, unique = true)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
// -- [isEnabled] ------------------------
@Column(name = "is_enabled", length = 1)
public Boolean getIsEnabled() {
return isEnabled;
}
public void setIsEnabled(Boolean isEnabled) {
this.isEnabled = isEnabled;
}
// -- [civility] ------------------------
@Column(length = 2)
@Enumerated(STRING)
public Civility getCivility() {
return civility;
}
public void setCivility(Civility civility) {
this.civility = civility;
}
// -- [firstName] ------------------------
@Size(max = 255)
@Column(name = "FIRST_NAME")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
// -- [lastName] ------------------------
@Size(max = 255)
@Column(name = "LAST_NAME")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
// -- [birthDate] ------------------------
@Column(name = "BIRTH_DATE", length = 23)
@Temporal(TIMESTAMP)
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
// -- [addressId] ------------------------
@Column(name = "address_id", precision = 10, insertable = false, updatable = false)
public Integer getAddressId() {
return addressId;
}
private void setAddressId(Integer addressId) {
this.addressId = addressId;
}
// -- [version] ------------------------
@Column(name = "VERSION", precision = 10)
@Version
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
// --------------------------------------------------------------------
// Many to One support
// --------------------------------------------------------------------
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// many-to-one: Account.addressId ==> Address.id
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@JoinColumn(name = "address_id")
@ManyToOne(cascade = PERSIST, fetch = LAZY)
public Address getHomeAddress() {
return homeAddress;
}
/**
* Set the homeAddress without adding this Account instance on the passed homeAddress
* If you want to preserve referential integrity we recommend to use
* instead the corresponding adder method provided by {@link Address}
*/
public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
// We set the foreign key property so it can be used by Hibernate search by Example facility.
if (homeAddress != null) {
setAddressId(homeAddress.getId());
} else {
setAddressId(null);
}
}
// --------------------------------------------------------------------
// One to Many support
// --------------------------------------------------------------------
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// one to many: account ==> books
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@OneToMany(mappedBy = "account", orphanRemoval = true, cascade = ALL)
public List<Book> getBooks() {
return books;
}
/**
* Set the {@link Book} list.
* It is recommended to use the helper method {@link #addBook(Book)} / {@link #removeBook(Book)}
* if you want to preserve referential integrity at the object level.
*
* @param books the list to set
*/
public void setBooks(List<Book> books) {
this.books = books;
}
/**
* Helper method to add the passed {@link Book} to the books list
* and set this account on the passed book to preserve referential
* integrity at the object level.
*
* @param book the to add
* @return true if the book could be added to the books list, false otherwise
*/
public boolean addBook(Book book) {
if (getBooks().add(book)) {
book.setAccount((Account) this);
return true;
}
return false;
}
/**
* Helper method to determine if the passed {@link Book} is already present in the books list.
*
* @param book the instance to look up.
* @return true if the books list contains the passed book, false otherwise.
*/
public boolean containsBook(Book book) {
return getBooks() != null && getBooks().contains(book);
}
/**
* Helper method to remove the passed {@link Book} from the books list and unset
* this account from the passed book to preserve referential integrity at the object level.
*
* @param book the instance to remove
* @return true if the book could be removed from the books list, false otherwise
*/
public boolean removeBook(Book book) {
if (getBooks().remove(book)) {
book.setAccount(null);
return true;
}
return false;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// one to many: account ==> documents
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@OneToMany(mappedBy = "account", orphanRemoval = true, cascade = ALL)
public List<Document> getDocuments() {
return documents;
}
/**
* Set the {@link Document} list.
* It is recommended to use the helper method {@link #addDocument(Document)} / {@link #removeDocument(Document)}
* if you want to preserve referential integrity at the object level.
*
* @param documents the list to set
*/
public void setDocuments(List<Document> documents) {
this.documents = documents;
}
/**
* Helper method to add the passed {@link Document} to the documents list
* and set this account on the passed document to preserve referential
* integrity at the object level.
*
* @param document the to add
* @return true if the document could be added to the documents list, false otherwise
*/
public boolean addDocument(Document document) {
if (getDocuments().add(document)) {
document.setAccount((Account) this);
return true;
}
return false;
}
/**
* Helper method to determine if the passed {@link Document} is already present in the documents list.
*
* @param document the instance to look up.
* @return true if the documents list contains the passed document, false otherwise.
*/
public boolean containsDocument(Document document) {
return getDocuments() != null && getDocuments().contains(document);
}
/**
* Helper method to remove the passed {@link Document} from the documents list and unset
* this account from the passed document to preserve referential integrity at the object level.
*
* @param document the instance to remove
* @return true if the document could be removed from the documents list, false otherwise
*/
public boolean removeDocument(Document document) {
if (getDocuments().remove(document)) {
document.setAccount(null);
return true;
}
return false;
}
// --------------------------------------------------------------------
// Many to Many
// --------------------------------------------------------------------
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// many-to-many: account ==> roles
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* Returns the roles list.
*/
@JoinTable(name = "ACCOUNT_ROLE", joinColumns = @JoinColumn(name = "ACCOUNT_ID"), inverseJoinColumns = @JoinColumn(name = "role_id"))
@ManyToMany(cascade = PERSIST)
public List<Role> getRoles() {
return roles;
}
/**
* Set the roles list.
* <p>
* It is recommended to use the helper method {@link #addRole(Role)} / {@link #removeRole(Role)}
* if you want to preserve referential integrity at the object level.
*
* @param roles the list of Role
*/
public void setRoles(List<Role> roles) {
this.roles = roles;
}
/**
* Helper method to add the passed {@link Role} to the roles list.
*/
public boolean addRole(Role role) {
return getRoles().add(role);
}
/**
* Helper method to remove the passed {@link Role} from the roles list.
*/
public boolean removeRole(Role role) {
return getRoles().remove(role);
}
/**
* Helper method to determine if the passed {@link Role} is present in the roles list.
*/
public boolean containsRole(Role role) {
return getRoles() != null && getRoles().contains(role);
}
/**
* Set the default values.
*/
public void initDefaultValues() {
}
@Override
public boolean equals(Object other) {
return this == other || (other instanceof Account && hashCode() == other.hashCode());
}
/**
* hashCode implementation using a business key.
*/
@Override
public int hashCode() {
return Objects.hashCode(getUsername());
}
/**
* Construct a readable string representation for this {@link Account} instance.
*/
@Override
public String toString() {
return Objects.toStringHelper(this) //
.add("id", getId()) //
.add("username", getUsername()) //
.add("password", getPassword()) //
.add("email", getEmail()) //
.add("isEnabled", getIsEnabled()) //
.add("civility", getCivility()) //
.add("firstName", getFirstName()) //
.add("lastName", getLastName()) //
.add("birthDate", getBirthDate()) //
.add("addressId", getAddressId()) //
.add("version", getVersion()) //
.toString();
}
}