Java JPA


Definition

The Java ORM standard for storing, accessing, and managing Java objects in a relational database

Usage Scenario

To abstract the database communication layer away from the developer, allowing to easily switch the database store vendor

About

Definition of the Storage Object

Annotation on the Class

@Entity
@Table(name = "group", uniqueConstraints={@UniqueConstraint(columnNames={"groupname"})})

Annotation on the Fields

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "group_seq_gen")
@SequenceGenerator(name = "group_seq_gen", sequenceName = "group_id_seq", allocationSize = 1)

@Column(name = "name", length = 64, insertable=true, updatable=false, unique=true)
private String groupname;

 

Object Store Logic

Inject EntityManager for use

@PersistenceContext(unitName = "group-service-ds")
private EntityManager entityManager;

Create / Update / Delete

public Group create(Group group) {
  entityManager.persist(group);
  return group;
}

public void update(Group group) {
  entityManager.merge(group);
}

public void delete(Group group) {
  entityManager.remove(group);
}


Read

Even though, the database is abstracted, the logic of how to selective read is not, the knowledge of database logic is still needed.  Getting distinct, grouping, from, select, maxresults, and where statements are all apart of the library.  

public Optional<Group> read(String userId) {

 List<Predicate> predicates = new ArrayList<>();

 CriteriaBuilder cb = entityManager.getCriteriaBuilder();

 CriteriaQuery<Group> criteriaQuery = cb.createQuery(Group.class);

 Root<Group> group = criteriaQuery.from(Group.class);

  criteriaQuery.select(group)

                       .distinct(true);

  predicates.add(cb.equal(group.get(Group_.userId), userId));

  criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()]));

  TypedQuery<Group> query = entityManager.createQuery(criteriaQuery)

                                                                                   .setMaxResults(100);

  List<Group> groups = query.getResultList();

  if (groups.isEmpty()) {

    return Optional.empty();

  } else {

    return Optional.of(groups.get(0));

  }

}

Persistence XML

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.0"

  xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence

        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">


  <persistence-unit name="group-service-ds">

    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <jta-data-source>java:jboss/datasources/group-service-ds</jta-data-source>

    <class>edu.psu.swe.services.group.model.Group</class>

    <properties>

      <property name="hibernate.hbm2ddl.auto" value="validate" />

      <property name="hibernate.show_sql" value="false" />

    </properties>

  </persistence-unit>


</persistence>



Comments

Popular posts from this blog

ColdFusion in an Enterprise Environment - Part 1 - Understanding how to use SubVersion (SVN)

coldfusion builder 2 extension not displaying browse button on type=projectdir

Being Thread Safe in Coldbox/Coldfusion