Wednesday, May 4, 2016

Hibernate/JPA Entity Model

Hi All,

Thanks again to stay here reading my post.

I would like to help other developers to solve problems about Java and middleware implementations.

If you have any doubts or problems about Java/JEE or JBoss Midleware feel free to contact me (fredericocerqueiraalves@gmail.com). It will be a pleasure to help you.

Now, i would like to show you a model implementation to use hibernate entity beans.

These classes are implemented to support hibernate first or second level cache, audit with envers and native or named queries.

So, let's see...

- Base Model

package br.com.alvesfred.entity;
 

import java.io.Serializable;

/**
 * Base Model Interface
 *
 * @author alvesfred
 *
 */
public interface BaseModel<ID> extends Serializable {

    /**
     * Set ID definition
     *
     * @param id
     */
    void setId(ID id);
   
    /**
     * Get ID
     *
     * @return
     */
    ID getId();
}


- Entity Base Model

package br.com.alvesfred.entity;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

/**
 * Model for Entities
 *
 * @author alvesfred
 *
 * @param <ID>
 */
@MappedSuperclass
public abstract class BaseModelEntity<ID> implements BaseModel<ID> {

    /**
     * serial
     */
    private static final long serialVersionUID = 3958601406206249649L;

    //@Id
    //@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "default_id_generator")
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "hilo_sequence_generator")
    @GenericGenerator(
            name       = "hilo_sequence_generator",
            strategy   = "org.hibernate.id.enhanced.SequenceStyleGenerator",
            parameters = { // default parameters
                    @Parameter(name = "sequence_name",  value = "sq_id"),
                    @Parameter(name = "initial_value",  value = "1"),
                    @Parameter(name = "increment_size", value = "1"),
                    @Parameter(name = "optimizer",      value = "hilo")
    })
    protected ID id;

    public BaseModelEntity() {
    }

    public BaseModelEntity(ID id) {
        setId(id);
    }

    public ID getId() {
        return id;
    }

    public void setId(ID id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        HashCodeBuilder hcb = new HashCodeBuilder();
        hcb.append(this.id);

        return hcb.toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || obj.getClass().isAssignableFrom(BaseModelEntity.class)) {
            return false;
        }

        @SuppressWarnings("unchecked")
        BaseModelEntity<ID> other = (BaseModelEntity<ID>) obj;

        EqualsBuilder eb = new EqualsBuilder();
        eb.append(this.getId(), other.getId());
       
        return eb.isEquals();
    }
   
}


- Version (Entity Base Model)

 package br.com.alvesfred.entity;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;

/**
 * Lock version entity - base model
 *
 * @author alvesfred
 *
 * @param <ID>
 * @param <VERSION>
 */
@MappedSuperclass
public abstract class BaseModelVersionEntity<ID> extends BaseModelEntity<ID> {

    /**
     * serial
     */
    private static final long serialVersionUID = 9205887194329558055L;

    //@Generated(GenerationTime.NEVER)
    @Version
    @Column(name = "versionId", nullable = false)
    private Long version;

    public Long getVersion() {
        return (this.version != null) ? this.version : this.hashCode();
    }

    public void setVersion(Long versao) {
        this.version = versao;
    }
}


- Finally Entity Class

 package br.com.alvesfred.entity;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.ColumnResult;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.QueryHint;
import javax.persistence.SequenceGenerator;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.SqlResultSetMappings;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.envers.Audited;

@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL) // using JTA by container
@Entity
@Audited
@Table(name="person")
@SequenceGenerator(name = "hilo_sequence_generator", sequenceName = "seq_id_person", allocationSize = 1)
@AttributeOverrides({
    @AttributeOverride(name="id", column=@Column(name = "person_id", unique = true, nullable = false, scale = 0)),
})

@NamedQueries({
    @NamedQuery(
            name  = PersonEntity.LIST_BY_ID_NAME,
            query = "select new PersonEntity(p.id) "
                  + "from PersonEntity p "
                  + "where p.name = :name",
            hints = {
                    @QueryHint (name="org.hibernate.cacheable", value="true"),
            }
    ),
})

@NamedNativeQueries(
        value = {
            @NamedNativeQuery(
                name  = PersonEntity.LIST_NATIVE_BY_NAME,
                query = PersonEntity.SQL_NATIVE_BY_NAME,
                resultSetMapping = PersonEntity.MAPPING_NATIVE_BY_NAME),
})

@SqlResultSetMappings(
        value = {
            @SqlResultSetMapping(      
                    name    = PersonEntity.MAPPING_NATIVE_BY_NAME,
                    columns = {
                        @ColumnResult(name="size")}),
        }
)
public class PersonEntity extends BaseModelVersionEntity<Long> {

    private static final long serialVersionUID = -1355055022303449688L;

    // Named Queries
    public static final String LIST_BY_ID_NAME = "PersonEntity.listByIdName";

    // Native Named Queries
    public static final String LIST_NATIVE_BY_NAME = "PersonEntity.nativeListByIdName";

    // SQL
    protected static final String SQL_NATIVE_BY_NAME =
            " select count(*) as size "
          + " from person "
          + " where name = :name ";

    // MAPPING
    public static final String MAPPING_NATIVE_BY_NAME = "PersonEntity.mappingListByIdName";

    @Lob
    @Column(name = "jsonInfo", nullable = false)
    private String jsonInfo; // a complete file text information, using json jackson parser
   
    @Column(name = "name", nullable = false)
    private String name;

    @Column(name = "age", nullable = true)
    private int age;

}
 

That's it!
Thanks a lot and feel free to contact me for any help.

See you soon...

No comments:

Post a Comment