Showing posts with label JPQL. Show all posts
Showing posts with label JPQL. Show all posts

Saturday, 23 May 2015

Tags

The tags are hooked up. Please find a video of the end result below:



Each blog post can have n many tags. Tags are embedded into the 'Post' class.
    @ElementCollection
    @CollectionTable(name="POST_TAGS", joinColumns = @JoinColumn(name="POST_ID"))
    @Column(name="TAGS")
    private List<tag> tags = new ArrayList<>();

I experimented with tags as entities in order to query them in a more JPA way. However, after several hours of making it complicated I decided to go with embedding them. After all: Why complicate the entities for the sake of a JPQL query?

Friday, 15 May 2015

A Cleaner Entity Count

Using TypedQuery<T> I got a cleaner entity count with JPA's JPQL.

Here is the cleaner way:

    public int countAllMessages(){
        return em.createQuery("SELECT count(m) FROM Message m", Number.class).getSingleResult().intValue();
    }

Instead of:

    public int countAllMessages(){
        Query q = em.createQuery("SELECT count(m) FROM Message m");
        Number result = (Number) q.getSingleResult();
        return result.intValue();
    }

Where q.getSingleResult() is cast to Number.