Find the right side (value) of expression in Java.

A simple example using RegEx: import java.util.regex.Matcher; import java.util.regex.Pattern; Pattern pattern = Pattern.compile(“[\\d\\w\\s’\”]+\\z”); Matcher matcher = pattern.matcher(“value=\”hello my name is bob\””); while (matcher.find()) { System.out.print(“found:'”+matcher.group()+”‘”); } prints… found:’”hello my name is bob”‘ This will work for strings containing apostrophes and quotes too. Any quotes (“) must first be escaped with a \ in the search string. Continue reading Find the right side (value) of expression in Java.

Hibernate and Postgres – a custom UserType to handle arrays

Here is a custom Hibernate UserType that will work to persist Java array types (string[], int[], boolean[], etc..) in a single column. I have seen Oracle implementations, but here is one that uses Java.sql.array and works for Postgres. If you’d like to know more about how this works, Andrew Phillips has written a great explanation of custom Hibernate UserTypes. Enjoy! _______________________ import java.io.Serializable; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; import java.sql.Array; import org.hibernate.*; import org.hibernate.usertype.UserType; /* * Custom UserType implemented to store java array (ie; String[]) types */ public class CustomArrayType implements UserType { public int[] sqlTypes() { … Continue reading Hibernate and Postgres – a custom UserType to handle arrays

RegEx Pattern (Java) for Finding or Matching Operators

Here is a regular expression pattern that can be used to find the operator in a SQL expression. For that matter any F=V type expression. Given a “field (operator) value” expression, this regex pattern will match and find the operator portion in strings such as… field = value, field > value, field >=, field <= value, field value, etc.. Pattern: [=\\]*[^a-zA-Z0-9] Note – The above pattern worked for Java 1.6, and on other languages it maybe possible to remove so of the escape slashes in the “[=\\]” portion to “[=]”. If you would like to find other RegEx patterns, I … Continue reading RegEx Pattern (Java) for Finding or Matching Operators

The “Quick Study”

“One who is able to memorize something easily and quickly or is able to understand and deal with something easily and successfully”.. Have you ever been called a “quick study”? I think a single day lesson is adequate to examine a technology of interest. Perhaps it’s my mild programmer A.D.D, but I don’t have patience for the multi-part series, where each lesson spans days, weeks, or months. Examples include the “Summer of Nhibernate“, “Learn step-by-step in 7 days“. Of course, s/w methodologies are not learned in a day, but specific software platforms and applications make a good “quick study”. The goal of each quick study will be to: 1) … Continue reading The “Quick Study”

Resolving Postgres "returning auto generated keys no supported"

I discovered Jean-Pol Landrain’s, “TriggerAssignedIdentityGenerator” to address the issue of the Hibernate ID Generator not supporting an Id created by the database via a BEFORE INSERT trigger. However the TriggerAssignedIdentityGenerator design for Oracle did not work for the older JDBC PostgreSQL driver which doesn’t support the return of auto-generated keys. The probem was resolved by changes to the ‘prepare’ and ‘executeAndExtract’ methods: protected PreparedStatement prepare(String insertSQL, SessionImplementor session) throws SQLException { return session.getBatcher().prepareStatement(insertSQL + ” RETURNING *”);} protected Serializable executeAndExtract(PreparedStatement insert) throws SQLException { insert.execute(); // get the new id back ResultSet rs = insert.getResultSet(); if (rs.next()) { return new … Continue reading Resolving Postgres "returning auto generated keys no supported"

Hibernate – org.hibernate.HibernateException: Unable to locate current JTA transaction

It started with a “org.hibernate.HibernateException: Could not find datasource” which was resolved by moving the hibernate-cfg.xml to the root folder (above WEB-INF). Once this was resolved my servlet returned a “Unable to locate current JTA transaction” transaction error. The postgres datasource configured via JBoss indicated status DOWN. Error text: org.hibernate.context.JTASessionContext.currentSession(JTASessionContext.java:88) org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:621) occurs when getSessionFactory is attempted. Direct connection to db (w/o JBoss DS) works fine. Continue reading Hibernate – org.hibernate.HibernateException: Unable to locate current JTA transaction