HHH-6069 - Escape entity fields name

This commit is contained in:
Lukasz Antoniak 2011-04-15 00:05:00 +02:00 committed by adamw
parent 0d71d35697
commit d7cc102b00
5 changed files with 59 additions and 6 deletions

View File

@ -82,9 +82,10 @@ public class Column implements Selectable, Serializable, Cloneable {
return name;
}
public void setName(String name) {
/* Envers passes 'name' parameter wrapped with '`' signs if quotation required. Set 'quoted' property accordingly. */
if (
name.charAt(0)=='`' ||
Dialect.QUOTE.indexOf( name.charAt(0) ) > -1 //TODO: deprecated, remove eventually
Dialect.QUOTE.indexOf( name.charAt(0) ) > -1
) {
quoted=true;
this.name=name.substring( 1, name.length()-1 );

View File

@ -82,11 +82,11 @@ public class RevisionInfoConfiguration {
Element idProperty = MetadataTools.addNativelyGeneratedId(class_mapping, revisionInfoIdData.getName(),
revisionPropType);
MetadataTools.addColumn(idProperty, "REV", null, 0, 0, null, null, null);
MetadataTools.addColumn(idProperty, "REV", null, 0, 0, null, null, null, false);
Element timestampProperty = MetadataTools.addProperty(class_mapping, revisionInfoTimestampData.getName(),
revisionInfoTimestampType.getName(), true, false);
MetadataTools.addColumn(timestampProperty, "REVTSTMP", null, 0, 0, null, null, null);
MetadataTools.addColumn(timestampProperty, "REVTSTMP", null, 0, 0, null, null, null, false);
return document;
}
@ -99,7 +99,7 @@ public class RevisionInfoConfiguration {
if (revisionPropSqlType != null) {
// Putting a fake name to make Hibernate happy. It will be replaced later anyway.
MetadataTools.addColumn(rev_rel_mapping, "*" , null, 0, 0, revisionPropSqlType, null, null);
MetadataTools.addColumn(rev_rel_mapping, "*" , null, 0, 0, revisionPropSqlType, null, null, false);
}
return rev_rel_mapping;

View File

@ -80,6 +80,9 @@ public class MetadataTools {
}
}
/**
* Column name shall be wrapped with '`' signs if quotation required.
*/
public static Element addOrModifyColumn(Element parent, String name) {
Element column_mapping = parent.element("column");
@ -94,11 +97,21 @@ public class MetadataTools {
return column_mapping;
}
/**
* Adds new <code>column</code> element. Method assumes that the value of <code>name</code> attribute is already
* wrapped with '`' signs if quotation required. It shall be invoked when column name is taken directly from configuration
* file and not from {@link org.hibernate.mapping.PersistentClass} descriptor.
*/
public static Element addColumn(Element parent, String name, Integer length, Integer scale, Integer precision,
String sqlType, String customRead, String customWrite) {
return addColumn(parent, name, length, scale, precision, sqlType, customRead, customWrite, false);
}
public static Element addColumn(Element parent, String name, Integer length, Integer scale, Integer precision,
String sqlType, String customRead, String customWrite, boolean quoted) {
Element column_mapping = parent.addElement("column");
column_mapping.addAttribute("name", name);
column_mapping.addAttribute("name", quoted ? "`" + name + "`" : name);
if (length != null) {
column_mapping.addAttribute("length", length.toString());
}
@ -197,7 +210,7 @@ public class MetadataTools {
*/
public static void addColumn(Element any_mapping, Column column) {
addColumn(any_mapping, column.getName(), column.getLength(), column.getScale(), column.getPrecision(),
column.getSqlType(), column.getCustomRead(), column.getCustomWrite());
column.getSqlType(), column.getCustomRead(), column.getCustomWrite(), column.isQuoted());
}
@SuppressWarnings({"unchecked"})

View File

@ -26,13 +26,17 @@ package org.hibernate.envers.test.integration.naming;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.Priority;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Table;
import org.junit.Test;
import javax.persistence.EntityManager;
import java.util.Arrays;
import java.util.Iterator;
/**
* @author Adam Warski (adam at warski dot org)
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
public class BasicNaming extends AbstractEntityTest {
private Integer id1;
@ -112,4 +116,18 @@ public class BasicNaming extends AbstractEntityTest {
getCfg().getClassMapping("org.hibernate.envers.test.integration.naming.NamingTestEntity1_AUD")
.getTable().getName());
}
@Test
public void testEscapeEntityField() {
Table table = getCfg().getClassMapping("org.hibernate.envers.test.integration.naming.NamingTestEntity1_AUD").getTable();
Iterator<Column> columnIterator = table.getColumnIterator();
while (columnIterator.hasNext()) {
Column column = columnIterator.next();
if ("nte_number#".equals(column.getName())) {
assert column.isQuoted();
return;
}
}
assert false;
}
}

View File

@ -32,6 +32,7 @@ import org.hibernate.envers.Audited;
/**
* @author Adam Warski (adam at warski dot org)
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
@Entity
@Table(name="naming_test_entity_1")
@ -46,6 +47,10 @@ public class NamingTestEntity1 {
@Audited
private String data;
@Column(name = "`nte_number#`")
@Audited
private Integer number;
public NamingTestEntity1() {
}
@ -58,6 +63,12 @@ public class NamingTestEntity1 {
this.data = data;
}
public NamingTestEntity1(Integer id, String data, Integer number) {
this.id = id;
this.data = data;
this.number = number;
}
public Integer getId() {
return id;
}
@ -74,6 +85,14 @@ public class NamingTestEntity1 {
this.data = data;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof NamingTestEntity1)) return false;
@ -81,6 +100,7 @@ public class NamingTestEntity1 {
NamingTestEntity1 that = (NamingTestEntity1) o;
if (data != null ? !data.equals(that.data) : that.data != null) return false;
if (number != null ? !number.equals(that.number) : that.number != null) return false;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
return true;
@ -90,6 +110,7 @@ public class NamingTestEntity1 {
int result;
result = (id != null ? id.hashCode() : 0);
result = 31 * result + (data != null ? data.hashCode() : 0);
result = 31 * result + (number != null ? number.hashCode() : 0);
return result;
}
}