Abaixo um exemplo do código para uma classe JavaBean que será persistida (utilizando as anotações da persistência seguindo o padrão de persistência Java – JPA
package gov.tjpr.example.entity.bean;
import gov.tjpr.entity.PersistableEntity;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
/**
* Classe que representa a entidade persistente de um Example Bean
* Esta classe possui as anotações necessárias para que a JPA
* saiba como esta entidade deverá ser persistida no repositório.
* @author Luiz Henrique Grossl
* @version 1.0.0
* @since 03-aug-2006
* @see gov.tjpr.entity.PersistableEntity
*/
@Entity
@Table(name="TB001EXAMPLE_BEAN")
@SequenceGenerator(name="sequence", sequenceName="TB001_SEQUENCE")
//gov.tjpr.hibernate.id.StoredProcedureGenerator
public class ExampleBean implements PersistableEntity<Integer> {
// the date format conversion pattern
public static final DateFormat _DATE_FORMAT =
SimpleDateFormat.getDateInstance(DateFormat.SHORT);
private static final long serialVersionUID = 1L;
// the bean unique identificator
@Id @Column(name="TB001_ID")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sequence")
private Integer id;
// the bean name
@Column(name="TB001_NAME", nullable=false)
private String name;
// the bean value
@Column(name="TB001_VALUE", nullable=false)
private String value;
// the bean creator e-mail
@Column(name="TB001_MAIL", nullable=false)
private String creatorMail;
// the bean edition date
@Column(name="TB001_DT_EDITION", nullable=false)
private Date editionDate = Calendar.getInstance().getTime();
// the bean cretion date
@Column(name="TB001_DT_CREATION", nullable=false)
private Date creationDate = Calendar.getInstance().getTime();
// the bean deploy date
@Column(name="TB001_DT_DEPLOYMENT", nullable=false)
private Date deploymentDate = Calendar.getInstance().getTime();
/**
* get DeploymentDate property value
* @return java.util.Date
*/
public Date getDeploymentDate() {
return this.deploymentDate;
}
/**
* set the deployment date with the last time of the day for comparisons
* of the actual edition date with the deployment date last time of day
* @param deploymentDate
*/
public void setDeploymentDate(Date deploymentDate) {
// set date with last time of the day for comparisons
Calendar calendar = Calendar.getInstance();
calendar.setTime(deploymentDate);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
this.deploymentDate = calendar.getTime();
}
/**
* auxiliary method for parsing the formatted deployment date, if
* date format is wrong get the actual date time
* @param deploymentDateFormatted
* @see SimpleDateFormat
* @see DateFormat.SHORT
*/
public void setDeploymentDateFormatted(String deploymentDateFormatted) {
try{
this.setDeploymentDate(_DATE_FORMAT.parse(deploymentDateFormatted));
}catch(ParseException pex){
// data has wrong format set as actual date;
this.setDeploymentDate(Calendar.getInstance().getTime());
}
}
/**
* get CreationDate property value
* @return java.util.Date
*/
public Date getCreationDate() {
return this.creationDate;
}
/**
* set CreationDate property value
* @param java.util.Date
*/
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
/**
* auxiliary method for parsing the formatted creation date, if
* date format is wrong get the actual date time
* @param creationDateFormatted
* @see SimpleDateFormat
* @see DateFormat.SHORT
*/
public void setCreationDateFormatted(String creationDateFormatted) {
try{
this.setCreationDate(_DATE_FORMAT.parse(creationDateFormatted));
}catch(ParseException pex){
// data has wrong format set as actual date;
this.setCreationDate(Calendar.getInstance().getTime());
}
}
/**
* set EditionDate property value
* @param java.util.Date
*/
public void setEditionDate(Date editionDate) {
this.editionDate = editionDate;
}
/**
* get EditionDate property value
* @return java.util.Date
*/
public Date getEditionDate() {
return this.editionDate;
}
/**
* get id property value
* @return java.lang.Integer
*/
public Integer getId() {
return this.id;
}
/**
* set id property value
* @param java.lang.Integer
*/
public void setId(Integer id) {
this.id = id;
}
/**
* get name property value
* @return java.lang.String
*/
public String getName() {
return this.name;
}
/**
* set name property value
* @param java.lang.String
*/
public void setName(String name) {
this.name = name;
}
/**
* get value property value
* @return java.lang.String
*/
public String getValue() {
return this.value;
}
/**
* set value property value
* @param java.lang.String
*/
public void setValue(String value) {
this.value = value;
}
/**
* get creatorMail property value
* @return java.lang.String
*/
public String getCreatorMail() {
return this.creatorMail;
}
/**
* set creatorMail property value
* @param java.lang.String
*/
public void setCreatorMail(String creatorMail) {
this.creatorMail = creatorMail;
}
@Override
public boolean equals(Object arg0) {
if(arg0 == null){
return false;
}
return new EqualsBuilder()
.append(true, arg0 instanceof PersistableEntity)
.append(this.getId(), ((PersistableEntity)arg0).getId())
.isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder()
.append(this.getId())
.toHashCode();
}
}