Google Endpoints 是否可以通过 Cloud SQL 与 Hibernate 一起使用?
有人可以为它发布一个简短的代码示例吗?
我尝试这样做并在生成客户端库时遇到了一般错误
语言班:
@Entity
@Table(name = "LANGUAGES")
public class Language {
private String languageIsoCode;
private String languageName;
public Language() {}
@Id
@Column(name = "LANGUAGE_ISO_CODE", unique = true, nullable = false, length = 8)
public String getLanguageIsoCode() {
return this.languageIsoCode;
}
public void setLanguageIsoCode(String languageIsoCode) {
this.languageIsoCode = languageIsoCode;
}
@Column(name = "LANGUAGE_NAME", nullable = false, length = 160)
public String getLanguageName() {
return this.languageName;
}
public void setLanguageName(String languageName) {
this.languageName = languageName;
}
}
语言端点类:
@Api
public class LanguageEndpoint {
@ApiMethod
public String getLanguage(){
EntityManager mgr = getEntityManager();
Query q = mgr.createQuery("from Language");
Language l = (Language) q.getResultList().get(0);
return l.getLanguageName();
}
private static EntityManager getEntityManager() {
return EMF.hibernateGet().createEntityManager();
}
}
和 EMF 类:
public final class EMF {
private static final EntityManagerFactory emfInstance = Persistence
.createEntityManagerFactory("transactions-optional");
private static final EntityManagerFactory emfHibernateInstance = Persistence
.createEntityManagerFactory("com.google.cloudsql.jpa");
private EMF() {
}
public static EntityManagerFactory get() {
return emfInstance;
}
public static EntityManagerFactory hibernateGet() {
return emfHibernateInstance;
}
}