Twitter

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

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

luni, 8 august 2016

Hibernate OGM via Java Persistence API, Spring MVC 4

Hibernate OGM via Java Persistence API, Spring MVC 4

Inventory optimistic locking with retry feature via db-util (Spring Data+Hibernate JPA)

This application is a test of handling an inventory when concurrent updates leads to optimistic locking exceptions via retry feature provided by Vlad Mihalcea in his db-util library  (in Spring: org.springframework.orm.ObjectOptimisticLockingFailureException). For more details, you should read the How to retry a JPA transactions after an OptimisticLockException article.

Test case possible output (inventory contains initially a quantity of 8 t-shirts, there are 10 buyers who order a random number of t-shirts between 1-4, and 10 retries for each buyer optimistic locking exception):

duminică, 7 august 2016

Hibernate OGM via Java Persistence API, RESOURCE_LOCAL under Tomcat

Hibernate OGM via Java Persistence API, RESOURCE_LOCAL under Tomcat

sâmbătă, 6 august 2016

Hibernate OGM via Java Persistence API, JEE application under Payara 4

Hibernate OGM via Java Persistence API, JEE application under Payara 4

Update only the modified fields of an entity via Hibernate and Spring Data (JpaRepository)

Complete example for updating only the modified fields of an entity based on Hibernate,  org.hibernate.annotations.DynamicUpdate.



vineri, 5 august 2016

Hibernate OGM via Hibernate Native API, standalone JTA, JBoss Transactions

Hibernate OGM via Hibernate Native API, standalone JTA, JBoss Transactions

Hibernate OGM via Hibernate Native API, JTA environment, built-in JTA platform

Hibernate OGM via Hibernate Native API, JTA environment, built-in JTA platform

Hibernate OGM via Hibernate Native API, JTA environment, EJB/CMT (Container Managed Transaction)

Hibernate OGM via Hibernate Native API, JTA environment, EJB/CMT (Container Managed Transaction)

Hibernate OGM via Hibernate Native API, JTA environment, EJB/BMT (Bean Managed Transaction)

Hibernate OGM via Hibernate Native API, JTA environment, EJB/BMT (Bean Managed Transaction)

Hibernate OGM via Hibernate Native API, non-JTA, JDBC transaction demarcation

Hibernate OGM via Hibernate Native API, non-JTA, JDBC transaction demarcation

JOINS via Hibernate JPA, PostgreSQL and Spring MVC

Let's suppose the following one-to-many relationship:


INNER JOIN
SELECT columns FROM TABLE_A a INNER JOIN TABLE_B b ON a.pk=b.pk

·         All players from all tournaments (players that participate in tournaments)

JPQL
SELECT a.playername, b.tournamentname
FROM Tournament b INNER JOIN b.players a

Hibernate (from JPQL)
Query:["
select     players1_.player_name as col_0_0_,
                tournament0_.tournament_name as col_1_0_
from tournaments tournament0_
inner join players players1_ on tournament0_.id=players1_.tournament_id
"],
Params:[()]

Native
SELECT a.player_name, b.tournament_name
FROM players a INNER JOIN tournaments b ON b.id = a.tournament_id

Output:
Row: [Rafael Nadal, Roland Garros]
Row: [Roger Federer, Roland Garros]
Row: [David Ferer, Roland Garros]
Row: [Kei Nishikori, BCR ]
Row: [Marin Cilic, BCR ]
Row: [Tommy Haas, Wimbledon]
Row: [Andy Murray, Wimbledon]

·         All tournaments for all players (tournaments that have players)

JPQL
SELECT a.playername, b.tournamentname
FROM Player a INNER JOIN a.tournament b

Hibernate (from JPQL)
Query:["
select     player0_.player_name as col_0_0_,
                 tournament1_.tournament_name as col_1_0_
 from players player0_
inner join tournaments tournament1_ on player0_.tournament_id=tournament1_.id
"],
 Params:[()]

Native
SELECT a.player_name, b.tournament_name
FROM tournaments b INNER JOIN players a ON b.id = a.tournament_id

Output:
Row: [Rafael Nadal, Roland Garros]
Row: [Roger Federer, Roland Garros]
Row: [David Ferer, Roland Garros]
Row: [Kei Nishikori, BCR ]
Row: [Marin Cilic, BCR ]
Row: [Tommy Haas, Wimbledon]
Row: [Andy Murray, Wimbledon]

·         All players that participate to Wimbledon tournament and have their rank smaller or equal to 5

JPQL
SELECT a.playername, a.rank
FROM Tournament b INNER JOIN b.players a
WHERE b.tournamentname = :tournament AND a.rank <= :rank

Hibernate (from JPQL)
Query:["
select     players1_.player_name as col_0_0_,
                players1_.player_rank as col_1_0_
from tournaments tournament0_
inner join players players1_ on tournament0_.id=players1_.tournament_id
where tournament0_.tournament_name=? and players1_.player_rank<=?
"],
 Params:[(1=Wimbledon,2=5)]

Native
SELECT a.player_name, a.player_rank
FROM players a INNER JOIN tournaments b ON b.id=a.tournament_id
WHERE b.tournament_name = :tournament AND a.player_rank <= :rank

Output:
Row: [Andy Murray, 3]

·         All tournaments that have players with rank smaller or equal to 5

JPQL
SELECT b.id, b.tournamentname
FROM Player a INNER JOIN a.tournament b
WHERE a.rank <= :rank

Hibernate (from JPQL)
Query:["
select     tournament1_.id as col_0_0_,
                tournament1_.tournament_name as col_1_0_
from players player0_
inner join tournaments tournament1_ on player0_.tournament_id=tournament1_.id
 where player0_.player_rank<=?
"],
 Params:[(1=5)]

Native
SELECT b.id, b.tournament_name
FROM tournaments b INNER JOIN players a ON b.id = a.tournament_id
WHERE a.player_rank <= :rank

Output:
Row: [1, Roland Garros]
Row: [1, Roland Garros]
Row: [2, Wimbledon]

LEFT JOIN
SELECT columns FROM TABLE_A a LEFT JOIN TABLE_B b ON a.pk=b.pk

·         All players even if they are not in tournaments

JPQL
SELECT a.playername, b.tournamentname
FROM Player a LEFT JOIN a.tournament b

Hibernate (from JPQL)
Query:["
select     player0_.player_name as col_0_0_,
                 tournament1_.tournament_name as col_1_0_
from players player0_
left outer join tournaments tournament1_ on player0_.tournament_id=tournament1_.id
"],
 Params:[()]

Native
SELECT a.player_name, b.tournament_name
FROM players a LEFT JOIN tournaments b ON b.id = a.tournament_id

Output:
Row: [Rafael Nadal, Roland Garros]
Row: [Roger Federer, Roland Garros]
Row: [David Ferer, Roland Garros]
Row: [Kei Nishikori, BCR ]
Row: [Marin Cilic, BCR ]
Row: [Tommy Haas, Wimbledon]
Row: [Andy Murray, Wimbledon]
Row: [Richard Gasquet, null]
Row: [Steve Johnson, null]

·         All tournaments even if they don't have players

JPQL
SELECT a.playername, b.tournamentname
FROM Tournament b LEFT JOIN b.players a

Hibernate (from JPQL)
Query:["
select     players1_.player_name as col_0_0_,
                tournament0_.tournament_name as col_1_0_
from tournaments tournament0_
left outer join players players1_ on tournament0_.id=players1_.tournament_id
"],
 Params:[()]

Native
SELECT a.player_name, b.tournament_name
FROM tournaments b LEFT JOIN players a ON b.id = a.tournament_id

Output:
Row: [Rafael Nadal, Roland Garros]
Row: [Roger Federer, Roland Garros]
Row: [David Ferer, Roland Garros]
Row: [Kei Nishikori, BCR ]
Row: [Marin Cilic, BCR ]
Row: [Tommy Haas, Wimbledon]
Row: [Andy Murray, Wimbledon]
Row: [null, Madrid Open]
Row: [null, Australian Open]
Row: [null, US Open]

RIGHT JOIN
SELECT columns FROM TABLE_A a RIGHT JOIN TABLE_B b ON a.pk=b.pk

·         All tournaments even if they don't have players

JPQL
SELECT a.playername, b.tournamentname
FROM Player a RIGHT JOIN a.tournament b

Hibernate (from JPQL)
Query:["
select     player0_.player_name as col_0_0_,
                tournament1_.tournament_name as col_1_0_
from players player0_
right outer join tournaments tournament1_ on player0_.tournament_id=tournament1_.id
"],
 Params:[()]

Native
SELECT a.player_name, b.tournament_name
FROM players a RIGHT JOIN tournaments b ON b.id = a.tournament_id

Output:
Row: [Rafael Nadal, Roland Garros]
Row: [Roger Federer, Roland Garros]
Row: [David Ferer, Roland Garros]
Row: [Kei Nishikori, BCR ]
Row: [Marin Cilic, BCR ]
Row: [Tommy Haas, Wimbledon]
Row: [Andy Murray, Wimbledon]
Row: [null, Madrid Open]
Row: [null, Australian Open]
Row: [null, US Open]

·         All players even if they are not in tournaments

JPQL
SELECT a.playername, b.tournamentname
FROM Tournament b RIGHT JOIN b.players a

Hibernate (from JPQL)
Query:["
select     players1_.player_name as col_0_0_,
                 tournament0_.tournament_name as col_1_0_
from tournaments tournament0_
right outer join players players1_ on tournament0_.id=players1_.tournament_id
"],
 Params:[()]

Native
SELECT a.player_name, b.tournament_name
FROM tournaments b RIGHT JOIN players a ON b.id = a.tournament_id

Output:
Row: [Rafael Nadal, Roland Garros]
Row: [Roger Federer, Roland Garros]
Row: [David Ferer, Roland Garros]
Row: [Kei Nishikori, BCR ]
Row: [Marin Cilic, BCR ]
Row: [Tommy Haas, Wimbledon]
Row: [Andy Murray, Wimbledon]
Row: [Richard Gasquet, null]
Row: [Steve Johnson, null]

FULL JOIN
SELECT columns FROM TABLE_A a FULL OUTER JOIN TABLE_B b ON a.pk=b.pk

·         All players and tournaments

JPQL
SELECT a.playername, b.tournamentname
FROM Player a FULL JOIN a.tournament b

Hibernate (from JPQL)
Query:["
select     player0_.player_name as col_0_0_,
                tournament1_.tournament_name as col_1_0_
from players player0_
full outer join tournaments tournament1_ on player0_.tournament_id=tournament1_.id
"],
 Params:[()]

Native
SELECT a.player_name, b.tournament_name
FROM players a FULL JOIN tournaments b ON b.id = a.tournament_id

Output:
Row: [Rafael Nadal, Roland Garros]
Row: [Roger Federer, Roland Garros]
Row: [David Ferer, Roland Garros]
Row: [Kei Nishikori, BCR ]
Row: [Marin Cilic, BCR ]
Row: [Tommy Haas, Wimbledon]
Row: [Andy Murray, Wimbledon]
Row: [Richard Gasquet, null]
Row: [Steve Johnson, null]
Row: [null, Madrid Open]
Row: [null, Australian Open]
Row: [null, US Open]

·         All tournaments and players

JPQL
SELECT a.playername, b.tournamentname
FROM Tournament b FULL JOIN b.players a

Hibernate (from JPQL)
Query:["
select     players1_.player_name as col_0_0_,
                tournament0_.tournament_name as col_1_0_
from tournaments tournament0_
full outer join players players1_ on tournament0_.id=players1_.tournament_id
"],
 Params:[()]

Native
SELECT a.player_name, b.tournament_name
FROM tournaments b FULL JOIN players a ON b.id = a.tournament_id

Output:
Row: [Rafael Nadal, Roland Garros]
Row: [Roger Federer, Roland Garros]
Row: [David Ferer, Roland Garros]
Row: [Kei Nishikori, BCR ]
Row: [Marin Cilic, BCR ]
Row: [Tommy Haas, Wimbledon]
Row: [Andy Murray, Wimbledon]
Row: [Richard Gasquet, null]
Row: [Steve Johnson, null]
Row: [null, Madrid Open]
Row: [null, Australian Open]
Row: [null, US Open]

LEFT EXCLUDING JOIN
SELECT columns FROM TABLE_A a LEFT JOIN TABLE_B b ON a.pk=b.pk WHERE b.pk IS NULL

·         All players that are not in tournaments

JPQL
SELECT a.playername, b.tournamentname
FROM Player a LEFT JOIN a.tournament b WHERE b.id IS NULL

Hibernate (from JPQL)
Query:["
select     player0_.player_name as col_0_0_,
                tournament1_.tournament_name as col_1_0_
from players player0_
left outer join tournaments tournament1_ on player0_.tournament_id=tournament1_.id
where tournament1_.id is null
"],
 Params:[()]

Native
SELECT a.player_name, b.tournament_name
FROM players a LEFT JOIN tournaments b ON b.id = a.tournament_id WHERE b.id IS NULL

Output:
Row: [Richard Gasquet, null]
Row: [Steve Johnson, null]

·         All tournaments that don't have players

JPQL
SELECT a.playername, b.tournamentname
FROM Tournament b LEFT JOIN b.players a WHERE a.id IS NULL

Hibernate (from JPQL)
Query:["
select     players1_.player_name as col_0_0_,
                tournament0_.tournament_name as col_1_0_
from tournaments tournament0_
left outer join players players1_ on tournament0_.id=players1_.tournament_id
where players1_.id is null
"],
 Params:[()]

Native
SELECT a.player_name, b.tournament_name
FROM tournaments b LEFT JOIN players a ON b.id = a.tournament_id WHERE a.id IS NULL

Output:
Row: [null, Madrid Open]
Row: [null, Australian Open]
Row: [null, US Open]

RIGHT EXCLUDING JOIN
SELECT columns FROM TABLE_A a RIGHT JOIN TABLE_B b ON a.pk=b.pk WHERE a.pk IS NULL

·         All tournaments that don't have players

JPQL
SELECT a.playername, b.tournamentname
FROM Player a RIGHT JOIN a.tournament b WHERE a.id IS NULL

Hibernate (from JPQL)
Query:["
select     player0_.player_name as col_0_0_,
                tournament1_.tournament_name as col_1_0_
from players player0_
right outer join tournaments tournament1_ on player0_.tournament_id=tournament1_.id
where player0_.id is null
"],
 Params:[()]

Native
SELECT a.player_name, b.tournament_name
FROM players a RIGHT JOIN tournaments b ON b.id = a.tournament_id WHERE a.id IS NULL

Output:
Row: [null, Madrid Open]
Row: [null, Australian Open]
Row: [null, US Open]

·         All players that are not in tournaments

JPQL
SELECT a.playername, b.tournamentname
FROM Tournament b RIGHT JOIN b.players a WHERE b.id IS NULL

Hibernate (from JPQL)
Query:["
select     players1_.player_name as col_0_0_,
                 tournament0_.tournament_name as col_1_0_
from tournaments tournament0_
right outer join players players1_ on tournament0_.id=players1_.tournament_id
where tournament0_.id is null
"],
 Params:[()]

Native
SELECT a.player_name, b.tournament_name
FROM tournaments b RIGHT JOIN players a ON b.id = a.tournament_id WHERE b.id IS NULL

Output:
Row: [Richard Gasquet, null]
Row: [Steve Johnson, null]

FULL OUTER EXCLUDING JOIN
SELECT columns FROM TABLE_A a FULL OUTER JOIN TABLE_B b ON a.pk=b.pk WHERE a.pk IS NULL OR b.pk IS NULL

·         All tournaments that don't have players and all players that don't participate in tournaments

JPQL
SELECT a.playername, b.tournamentname
FROM Player a FULL JOIN a.tournament b WHERE a.id IS NULL OR b.id IS NULL

Hibernate (from JPQL)
Query:["
select     player0_.player_name as col_0_0_,
                tournament1_.tournament_name as col_1_0_
from players player0_
full outer join tournaments tournament1_ on player0_.tournament_id=tournament1_.id
where player0_.id is null or tournament1_.id is null
"],
 Params:[()]

Native
SELECT a.player_name, b.tournament_name
FROM players a FULL JOIN tournaments b ON b.id = a.tournament_id WHERE a.id IS NULL OR b.id IS NULL

Output:
Row: [Richard Gasquet, null]
Row: [Steve Johnson, null]
Row: [null, Madrid Open]
Row: [null, Australian Open]
Row: [null, US Open]

·         Or, reverse fetch

JPQL
SELECT a.playername, b.tournamentname
FROM Tournament b FULL JOIN b.players a WHERE a.id IS NULL OR b.id IS NULL

Hibernate (from JPQL)
Query:["
select     players1_.player_name as col_0_0_,
                tournament0_.tournament_name as col_1_0_
from tournaments tournament0_
full outer join players players1_ on tournament0_.id=players1_.tournament_id
where players1_.id is null or tournament0_.id is null
"],
 Params:[()]

Native
SELECT a.player_name, b.tournament_name
FROM tournaments b FULL JOIN players a ON b.id = a.tournament_id WHERE a.id IS NULL OR b.id IS NULL

Output:
Row: [Richard Gasquet, null]
Row: [Steve Johnson, null]
Row: [null, Madrid Open]
Row: [null, Australian Open]
Row: [null, US Open]

The complete application is available here.