View Javadoc

1   // Copyright 2008 Thiago H. de Paula Figueiredo
2   //
3   // Licensed under the Apache License, Version 2.0 (the "License");
4   // you may not use this file except in compliance with the License.
5   // You may obtain a copy of the License at
6   //
7   //     http://www.apache.org/licenses/LICENSE-2.0
8   //
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  
15  package br.com.arsmachina.dao.hibernate.dialect;
16  
17  import org.hibernate.dialect.PostgreSQLDialect;
18  import org.hibernate.id.IdentityGenerator;
19  import org.hibernate.id.SequenceGenerator;
20  
21  /**
22   * {@link PostgreSQLDialect} that defines {@link IdentityGenerator} as the default id value
23   * generator instead of {@link SequenceGenerator}. In addition, it fixes the "wrong sequence name
24   * in backticks-scaped table name bug".
25   * 
26   * @author Thiago H. de Paula Figueiredo
27   */
28  public class IdentityPostresqlDialect extends PostgreSQLDialect {
29  
30  	/**
31  	 * Returns {@link IdentityGenerator} instead of {@link SequenceGenerator}.
32  	 * 
33  	 * @see org.hibernate.dialect.PostgreSQLDialect#getNativeIdentifierGeneratorClass()
34  	 */
35  	@SuppressWarnings("unchecked")
36  	@Override
37  	public Class getNativeIdentifierGeneratorClass() {
38  		return IdentityGenerator.class;
39  	}
40  
41  	/**
42  	 * Removes the backtickes from the <code>sequenceName</code> and then returns
43  	 * <code>super.getSequenceNextValString(sequenceName)</code>.
44  	 * @see org.hibernate.dialect.PostgreSQLDialect#getSequenceNextValString(java.lang.String)
45  	 */
46  	@Override
47  	public String getIdentitySelectString(String table, String column, int type) {
48  		table = table.replace("\"", "");
49  		return new StringBuffer().append("select currval('")
50  			.append(table)
51  			.append('_')
52  			.append(column)
53  			.append("_seq')")
54  			.toString();
55  	}
56  
57  }