/java/

JEE JPA Discriminator

2015-09-12 17:04:57

Here you will find simple example of JPA Discriminator together with JAX-RS and Jboss 8.

DataBase In this example we will use PostgreSQL as a Data Base. You have to create new data base "test2".

Jboss configuration - standalone.xml

Next you have to update your standalone.xml


<datasource enabled='"true"' jndi-name='"java:jboss/datasources/ExamplePgDS2"' jta='"true"' pool-name='"ExamplePgDS2"' use-ccm='"true"' use-java-context='"true"'>
<connection-url>jdbc:postgresql://localhost:5432/test2</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<driver>postgresql-jdbc4</driver>
<pool>
<min-pool-size>2</min-pool-size>
<max-pool-size>20</max-pool-size>
<prefill>true</prefill>
</pool>
<security>
<user-name>postgres</user-name>
<password>postgres</password>
</security>
<validation>
<check-valid-connection-sql>SELECT 1</check-valid-connection-sql>
</validation>
</datasource>

If this is your first time when you use postgresql, you have to add postgresql-9.2-1003.jdbc4.jar to /modules/system/layers/base/org/postgresql/main/ in jboss directory. This file you can find on my github.

What next? you have to configure / connect your app with db You can do this by adding persistence.xml to /src/main/resources/META-INF/persistence.xml


<persistence http:="" java.sun.com="" ns="" persistence="" persistence_2_0.xsd&quot;="" version='"2.0"' xml="" xmlns='"http://java.sun.com/xml/ns/persistence"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsi:schemalocation='"'>
<persistence-unit name='"ExamplePgDS"' transaction-type='"JTA"'>
<jta-data-source>java:jboss/datasources/ExamplePgDS2</jta-data-source>
<properties>
<property name='"hibernate.dialect"' value='"org.hibernate.dialect.PostgreSQLDialect"'></property>
<property name='"hibernate.hbm2ddl.auto"' value='"create-drop"'></property>
</properties>
</persistence-unit>
</persistence>

ah, I know, XML hell, ok we can switch to java part

Entities

package pl.btbw.persistent.entities;

import pl.btbw.web.dto.PostDto;

import javax.persistence.*;

@Entity
@Table(name = "post")
@DiscriminatorColumn(name = "distype", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("POST")
@NamedQueries({
        @NamedQuery(name = "Post.all", query = "SELECT p FROM Post p WHERE distype = 'POST'")
})
public class Post {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    protected int id;

    @Column(name = "name")
    protected String name;

    public Post() {
    }

    public Post(String name) {
        this.name = name;
    }

    public PostDto asDto() {
        return new PostDto(id, name);
    }
}
package pl.btbw.persistent.entities;

import pl.btbw.web.dto.ArticleDto;

import javax.persistence.*;

@Entity
@Table(name = "article")
@DiscriminatorValue("ARTICLE")
@NamedQueries({
        @NamedQuery(name = "Article.all", query = "SELECT p FROM Article p WHERE distype = 'ARTICLE'")
})
public class Article extends Post {

    @Column(name = "category")
    private String category;

    public Article() {
    }

    public Article(String name, String category) {
        this.name = name;
        this.category = category;
    }

    public ArticleDto asDto() {
        return new ArticleDto(id, name, category);
    }
}

as you see Article extends Post. Both entities use one table "post"

We have possibility to distinguish them by simple trick with @DiscriminatorColumn and @DiscriminatorValue anotations.

and that is all, our table "post" now have extra column "distype" where for post entity system will add "POST" value, for article entity "ARTICLE" value.

we can use this proper to adjust queries and fetch only what we need

of course full example you can find on github