Search code examples
javahibernatejpajpa-2.0jpa-criteria

jpa metamodel how to get table name


Entity:

@Entity
public class MyAccount {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String userId;

    private String password;

    private String email;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }   
}

Naming strategy:

public class HibernateNamingStrategy extends PhysicalNamingStrategyStandardImpl implements Serializable {

    private static final long serialVersionUID = -4772523898875102775L;

    @Override
    public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
        return new Identifier(addUnderscores(name.getText()), name.isQuoted());
    }

    @Override
    public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
        return new Identifier(addUnderscores(name.getText()), name.isQuoted());
    }

    protected static String addUnderscores(String name) {
        final StringBuilder buf = new StringBuilder(name.replace('.', '_'));
        for (int i = 1; i < buf.length() - 1; i++) {
            if (Character.isLowerCase(buf.charAt(i - 1)) && Character.isUpperCase(buf.charAt(i))
                    && Character.isLowerCase(buf.charAt(i + 1))) {
                buf.insert(i++, '_');
            }
        }

        return buf.toString().toLowerCase();
    }
}

JPA metamodel:

@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(MyAccount.class)
public abstract class MyAccount_ {

    public static volatile SingularAttribute<MyAccount, String> password;
    public static volatile SingularAttribute<MyAccount, Integer> id;
    public static volatile SingularAttribute<MyAccount, String> userId;
    public static volatile SingularAttribute<MyAccount, String> email;

}

I am going to do something like below:

Join<Employee,MyAccount> project = emp.join("my_account", JoinType.LEFT);

I don't see any table name related attribute auto-generated in the MyAccount_ metamodel. How can I use metamodel table name in the join criteria (I don't want to use hardcoded string)?

PS: I am using Spring MVC and naming strategy which all camel case is separated by underscore.


Solution

  • It is too bad to hear that (from @Neil Stockton), it is not part of the metamodel, I think it really should be included.

    Anyway, my code doesn't have annotation because I am using implicit naming strategy.

    So by doing this, it is not working, it got NullPointerException:

    System.out.println("Table_Name: " + MyAccount.class.getAnnotation(Table.class).name());
    

    So the simplest way, is to reuse my function addUnderscores (change it to public):

    System.out.println("Table_Name: " + HibernateNamingStrategy.addUnderscores(MyAccount.class.getSimpleName()));
    

    Although code become longer but I think it is better than hardcoded string instead.

    Hope this help others too.