001 // Copyright 2008 Thiago H. de Paula Figueiredo
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package br.com.arsmachina.dao.hibernate.dialect;
016
017 import org.hibernate.dialect.PostgreSQLDialect;
018 import org.hibernate.id.IdentityGenerator;
019 import org.hibernate.id.SequenceGenerator;
020
021 /**
022 * {@link PostgreSQLDialect} that defines {@link IdentityGenerator} as the default id value
023 * generator instead of {@link SequenceGenerator}. In addition, it fixes the "wrong sequence name
024 * in backticks-scaped table name bug".
025 *
026 * @author Thiago H. de Paula Figueiredo
027 */
028 public class IdentityPostresqlDialect extends PostgreSQLDialect {
029
030 /**
031 * Returns {@link IdentityGenerator} instead of {@link SequenceGenerator}.
032 *
033 * @see org.hibernate.dialect.PostgreSQLDialect#getNativeIdentifierGeneratorClass()
034 */
035 @SuppressWarnings("unchecked")
036 @Override
037 public Class getNativeIdentifierGeneratorClass() {
038 return IdentityGenerator.class;
039 }
040
041 /**
042 * Removes the backtickes from the <code>sequenceName</code> and then returns
043 * <code>super.getSequenceNextValString(sequenceName)</code>.
044 * @see org.hibernate.dialect.PostgreSQLDialect#getSequenceNextValString(java.lang.String)
045 */
046 @Override
047 public String getIdentitySelectString(String table, String column, int type) {
048 table = table.replace("\"", "");
049 return new StringBuffer().append("select currval('")
050 .append(table)
051 .append('_')
052 .append(column)
053 .append("_seq')")
054 .toString();
055 }
056
057 }