REST Test – Create JPA Entity

The REST services were based around a resource called AnEntity. The ‘AnEntity’ resources themselves were persisted using JPA.

Select New, Entity Class..., and then use Lombok to tidy up the code and add a simple attribute (called ‘name’) to give:

package com.nigeleke.restin.model;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import lombok.Data;

@Entity
public @Data class AnEntity implements Serializable {

  @Override
  public String toString() {
    return "com.nigeleke.restin.AnEntity[ id=" + id + ", name=" + name + "]";
  }

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  @NotNull
  private String name;

  private static final long serialVersionUID = 1L;
}

This step also create the persistence.xml file using restin as the JNDI name.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="restinPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>restin</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="eclipselink.ddl-generation" value="create-tables"/>
    </properties>
  </persistence-unit>
</persistence>

Note, at this point, Netbeans enables you to set up a database referenced by the JNDI name. This will not be the database used during integration testing.

REST Test - Create Maven Web Application REST Test - Generate REST Service

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.