实体:
var tujuanUrl = URLComponents(string: "https://api-sandbox.tiket.com/flight_api/all_airport?")!
tujuanUrl.queryItems = [
URLQueryItem(name: "token", value: "4d4bba355bb920fbdc1aa3848769a59ba74ea03c" ),
URLQueryItem(name: "output", value: "json")
]
let request = tujuanUrl.url
let task = URLSession.shared.dataTask(with: request!, completionHandler: { (data, response, error) -> Void in
if let error = error {
print(error)
return
}
// Parse JSON data
if let data = data {
self.kotaTujuan = self.parseJsonData(data: data)
// Reload table view
OperationQueue.main.addOperation({
self.tableView.reloadData()
})
}
})
task.resume()
}
命名策略:
@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;
}
}
JPA元模型:
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();
}
}
我将做类似下面的事情:
@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);
元模型中看到自动生成的任何与表名相关的属性。如何在加入条件中使用元模型表名(我不想使用硬编码字符串)?
PS:我正在使用Spring MVC和命名策略,所有驼峰案例都用下划线分隔。
答案 0 :(得分:2)
听到这个(来自@Neil Stockton)太糟糕了,它不是元模型的一部分,我认为它应该被包含在内。
无论如何,我的代码没有注释,因为我使用隐式命名策略。
所以通过这样做,它不起作用,它得到了NullPointerException:
System.out.println("Table_Name: " + MyAccount.class.getAnnotation(Table.class).name());
最简单的方法是重用我的函数addUnderscores
(将其更改为public
):
System.out.println("Table_Name: " + HibernateNamingStrategy.addUnderscores(MyAccount.class.getSimpleName()));
虽然代码变得更长但我认为它比硬编码字符串更好。
希望这也有助于其他人。