0
實體:JPA元模型如何得到表名
@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;
}
}
命名策略:
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元模型:
@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;
}
我要像做如下:
Join<Employee,MyAccount> project = emp.join("my_account", JoinType.LEFT);
我在MyAccount_
元模型中看不到任何與自動生成的表名相關的屬性。我如何在聯接條件中使用元模型表名(我不想使用硬編碼字符串)? PS:我使用Spring MVC和命名策略,所有的駱駝案例都是用下劃線分隔的。
表名是**不是**元模型(靜態或API)的一部分,所以你正在浪費你的時間看向那個方向。表名僅存在於註釋/ XML元數據中,或者通過JPA提供程序自己的內部(非可移植)API –