HHH-10294 EntityGraph improvement: For each jpa attribute, generate also a String constant holding the attribute field name

This commit is contained in:
Réda Housni Alaoui 2017-12-18 13:16:08 +01:00 committed by Steve Ebersole
parent b7c0235697
commit 4efac1369a
9 changed files with 62 additions and 8 deletions

View File

@ -31,7 +31,7 @@ import org.hibernate.annotations.CacheConcurrencyStrategy;
@NamedEntityGraph(
name = "Student.Full",
attributeNodes = {
@NamedAttributeNode(value = "courses")
@NamedAttributeNode(value = Student_.COURSES)
}
)
})

View File

@ -111,7 +111,11 @@ public final class ClassWriter {
List<MetaAttribute> members = entity.getMembers();
for ( MetaAttribute metaMember : members ) {
pw.println( " " + metaMember.getDeclarationString() );
pw.println( " " + metaMember.getAttributeDeclarationString() );
}
pw.println();
for ( MetaAttribute metaMember : members ) {
pw.println( " " + metaMember.getAttributeNameDeclarationString() );
}
pw.println();

View File

@ -7,12 +7,15 @@
package org.hibernate.jpamodelgen.annotation;
import java.beans.Introspector;
import java.util.ArrayList;
import java.util.List;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.util.Elements;
import org.hibernate.jpamodelgen.model.MetaAttribute;
import org.hibernate.jpamodelgen.model.MetaEntity;
import org.hibernate.jpamodelgen.util.StringUtil;
/**
* Captures all information about an annotated persistent attribute.
@ -33,7 +36,8 @@ public abstract class AnnotationMetaAttribute implements MetaAttribute {
this.type = type;
}
public String getDeclarationString() {
@Override
public String getAttributeDeclarationString() {
return new StringBuilder().append( "public static volatile " )
.append( parent.importType( getMetaType() ) )
.append( "<" )
@ -46,6 +50,20 @@ public abstract class AnnotationMetaAttribute implements MetaAttribute {
.toString();
}
@Override
public String getAttributeNameDeclarationString(){
return new StringBuilder().append("public static final ")
.append(parent.importType(String.class.getName()))
.append(" ")
.append(StringUtil.getUpperUnderscoreCaseFromLowerCamelCase(getPropertyName()))
.append(" = ")
.append("\"")
.append(getPropertyName())
.append("\"")
.append(";")
.toString();
}
public String getPropertyName() {
Elements elementsUtil = parent.getContext().getElementUtils();
if ( element.getKind() == ElementKind.FIELD ) {

View File

@ -23,7 +23,8 @@ public class AnnotationMetaMap extends AnnotationMetaCollection {
this.keyType = keyType;
}
public String getDeclarationString() {
@Override
public String getAttributeDeclarationString() {
return "public static volatile " + getHostingEntity().importType( getMetaType() )
+ "<" + getHostingEntity().importType( getHostingEntity().getQualifiedName() )
+ ", " + getHostingEntity().importType( keyType ) + ", "

View File

@ -10,7 +10,9 @@ package org.hibernate.jpamodelgen.model;
* @author Hardy Ferentschik
*/
public interface MetaAttribute {
String getDeclarationString();
String getAttributeDeclarationString();
String getAttributeNameDeclarationString();
String getMetaType();

View File

@ -93,6 +93,10 @@ public final class StringUtil {
}
}
public static String getUpperUnderscoreCaseFromLowerCamelCase(String lowerCamelCaseString){
return lowerCamelCaseString.replaceAll("(.)(\\p{Upper})", "$1_$2").toUpperCase();
}
private static boolean startsWithSeveralUpperCaseLetters(String string) {
return string.length() > 1 &&
Character.isUpperCase( string.charAt( 0 ) ) &&

View File

@ -8,6 +8,10 @@ package org.hibernate.jpamodelgen.xml;
import org.hibernate.jpamodelgen.model.MetaAttribute;
import org.hibernate.jpamodelgen.model.MetaEntity;
import org.hibernate.jpamodelgen.util.StringUtil;
import java.util.ArrayList;
import java.util.List;
/**
* @author Hardy Ferentschik
@ -24,14 +28,28 @@ public abstract class XmlMetaAttribute implements MetaAttribute {
this.type = type;
}
@Override
public String getDeclarationString() {
@Override
public String getAttributeDeclarationString() {
return "public static volatile " + hostingEntity.importType( getMetaType() )
+ "<" + hostingEntity.importType( hostingEntity.getQualifiedName() )
+ ", " + hostingEntity.importType( getTypeDeclaration() )
+ "> " + getPropertyName() + ";";
}
@Override
public String getAttributeNameDeclarationString(){
return new StringBuilder().append("public static final ")
.append(hostingEntity.importType(String.class.getName()))
.append(" ")
.append(StringUtil.getUpperUnderscoreCaseFromLowerCamelCase(getPropertyName()))
.append(" = ")
.append("\"")
.append(getPropertyName())
.append("\"")
.append(";")
.toString();
}
public String getPropertyName() {
return propertyName;
}

View File

@ -20,7 +20,8 @@ public class XmlMetaMap extends XmlMetaCollection {
this.keyType = keyType;
}
public String getDeclarationString() {
@Override
public String getAttributeDeclarationString() {
final MetaEntity hostingEntity = getHostingEntity();
return new StringBuilder().append( "public static volatile " )
.append( hostingEntity.importType( getMetaType() ) )

View File

@ -10,6 +10,7 @@ import org.hibernate.jpamodelgen.test.util.TestForIssue;
import org.hibernate.jpamodelgen.util.StringUtil;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@ -35,4 +36,9 @@ public class StringUtilTest {
public void testHashCodeNotAProperty() {
assertFalse( StringUtil.isProperty( "hashCode", "Integer" ) );
}
@Test
public void testGetUpperUnderscoreCaseFromLowerCamelCase(){
assertEquals("USER_PARENT_NAME", StringUtil.getUpperUnderscoreCaseFromLowerCamelCase("userParentName"));
}
}