Twitter

sâmbătă, 17 noiembrie 2018

Java EE 8 -> JSON-B + JAX-RS + JPA Entities

This is an example of annotating a JPA entity via JSON-B (JSR 367). We use the well-known @Column to shape the database columns names and JSON-B suite to shape the JSON returned to the client. We rely on @JsonbProperty, @JsonbTransient, @JsonbDateFormat, @JsonbNumberFormat, but more are available in javax.json.bind.annotation.
The important part here is the the Product entity listed below:

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
import javax.json.bind.annotation.JsonbDateFormat;
import javax.json.bind.annotation.JsonbNumberFormat;
import javax.json.bind.annotation.JsonbProperty;
import javax.json.bind.annotation.JsonbTransient;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

@Entity
@Table(name = "products")
public class Product implements Serializable {
 
    private static final long serialVersionUID = 1L;

    @Id
    @NotNull
    @Column(name = "id")
    @JsonbProperty(nillable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @JsonbProperty("product-name")
    @Column(name = "product_name")
    private String name;

    @JsonbProperty(value = "product-brand", nillable = true)
    @Column(name = "brand")
    private String brand;

    @JsonbTransient
    @Column(name = "random_sku")
    private int sku;

    @JsonbProperty("product-arrival-date")
    @JsonbDateFormat("dd-MM-yyyy")
    @Column(name = "arrival_date")
    private LocalDate arrivalDate;

    @JsonbProperty("product-price")
    @Column(name = "price", precision = 11, scale = 2)
    private BigDecimal price;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
     
    @JsonbNumberFormat(locale = "en_US", value = "#0.0")
    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public int getSku() {
        return sku;
    }

    public void setSku(int sku) {
        this.sku = sku;
    }

    public LocalDate getArrivalDate() {
        return arrivalDate;
    }

    public void setArrivalDate(LocalDate arrivalDate) {
        this.arrivalDate = arrivalDate;
    }
}



Example of executing the application from Google Chrome, ARC extension:
  • add a new product via a JSON

  • load an existing product by id
The complete application is available on GitHub. I tested on Payara 5 and Derby DB (jdbc/sample).

vineri, 16 noiembrie 2018

Starting Java EE 8 on NetBeans 8.2 and Payara 5

This is an example of starting a Java EE 8 project on NetBeans 8.2 and Payara 5. I suppose that you already have installed NetBeans 8.2 and Payara 5:

  • In NetBeans, File | New Project | Maven | Project from Archetype. Click Next.


  • In NetBeans, Search filed type search airhacks.com artifact, javaee8-essentials-archetype. Optionally, configure the archetype and click Next.
  • In NetBeans, set up the project name, location, etc. Click Finish.
  • In NetBeans, if everything works fine, you should obtain a project like below:
  • In NetBeans, Build and Run (don't forget to attach Payara server to the project):
  • In the browser, access: http://localhost:8080/quickstart/resources/ping. If everything works as expected you will see the message from the picture below.

This works because the generated quickstart application exposes a JAX-RS endpoint. Just check the source code of PingResource class.

Done! Source code on GitHub.

joi, 29 septembrie 2016

Using Amazon Simple Workflow Service to rollback and retry a db failed transaction

This simple application take advantage of Amazon Simple Workflow Service (SWF) exponential retry (@ExponentialRetry) to rollback and retry a database failed transaction 5 times at 10 seconds each. After 5 attempts the error is revealed.

Check source code here.

joi, 18 august 2016

Hibernate OGM 5.0 with MongoDB - Listeners

Hibernate OGM 5.0 with MongoDB - Listeners

Quickstart Bitronix JTA


Quickstart Bitronix JTA with one MySQL data source (Spring MVC 4.2.3 Final | Hibernate 5.2.2 Final)-no XML configs
Quickstart Bitronix JTA with two MySQL data sources (Spring MVC 4.2.3 Final | Hibernate 5.2.2 Final)-no XML configs

miercuri, 17 august 2016

marți, 16 august 2016

Hibernate OGM 5.0 with MongoDB - @MappedSuperclass

Hibernate OGM 5.0 with MongoDB - @MappedSuperclass

Constructor Result Mappings Sample

 Constructor Result Mappings was introduced in JPA 2.1. This application is just a sample of using it for joining data from three tables.

vineri, 12 august 2016

Hibernate OGM 5.0 with MongoDB - bidirectional @OneToOne

Hibernate OGM 5.0 with MongoDB - bidirectional @OneToOne

Hibernate OGM 5.0 with MongoDB - using @OneToOne and @MapsId

Hibernate OGM 5.0 with MongoDB - using @OneToOne and @MapsId

Hibernate JPA - Get the transaction isolation level in a readable fashion

Hibernate OGM 5.0 with MongoDB - using identifiers (JPA @Id)

This application is a quickstart for working with Hibernate OGM 5 and MongoDB identifiers (JPA @Id). The application contains:
  • manually (explicitly) set id
  • AUTO strategy
  • IDENTITY strategy
  • SEQUENCE strategy
  • TABLE strategy
  • UUID2 
  • EmbeddedId
  • IdClass
  • Custom generator
The application is available here.

joi, 11 august 2016

Hibernate JPA sync/async transactions JUnit tests for MySQL and PostgreSQL

Inspired from Hibernate Master Class by Vlad Mihalcea (author of  High-Performance Java Persistence book, repo: GitHub) I wrote a simple application that can be use to test transactions for MySQL and PostgreSQL. Basically, the application uses Hibernate JPA and you can easily:

  • select between MySQL and PostgreSQL
  • configure data source via a properties file (samples provided)
  • specify if the data source should be proxied by ttddyy/datasource-proxy
  • specify if you want to use the HikariCP
  • specify the pool size for HikariCP (default to 3, if 0 is set)
  • specify a custom interceptor
  • test simple transactions
  • test sync transactions
  • test sync transactions by scheduling the nested one (unfortunately because JUnit kill child threads, this works by blocking the main thread - not so useful)
  • test async transactions via a fixed (specified size) thread pool
  • execute code before/after the transaction by overriding a built-in functional interface
  • count executed statements via db-util
  • auto-detects your entities (annotated with @Entity) by scanning /target folders
Well, I've done this app for playing with it, but maybe you will find something useful in it also. Here it is. By default, it contains 8 tests for MySQL, and 2 should fail with an javax.persistence.OptimisticLockingException exception.

marți, 9 august 2016

Hibernate OGM via Java Persistence API, standalone JTA, JBoss Transactions

Hibernate OGM via Java Persistence API, standalone JTA, JBoss Transactions