我正在创建一个简单的项目,以了解有关jersey
,hibernate-jpa-2.0-API
的更多信息。
我不确定是否需要将Spring添加到这个组合中。我创建了一个模型类,并通过Jersey
公开了一个端点。现在我想找到从模型中检索数据的方法。
我能想到的一个可能的选择是创建一个服务(使用spring),它将通过entityManager
获取数据。这需要hibernate核心。有没有其他方法可以检索数据,例如使用hibernate-jpa
而不使用spring
?
我也看到这个article有点晚了,但如果没有hibernate config file
,还有替代方法吗?
感谢。
我添加了以下文件,尝试使用hibernate进行实验。
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
Configuration c = new Configuration();
// Create the SessionFactory from hibernate.cfg.xml
return c.configure().buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
我还创建了一个带有以下
的DAOpublic class SimpleModelDAO {
private Session getCurrentSession() {
return HibernateUtil.getSessionFactory().getCurrentSession();
}
public SimpleModelDAO() {}
public List<SimpleModel> getResults() {
// TODO Auto-generated constructor stub
Session s = getCurrentSession();
Transaction tx = s.beginTransaction();
Criteria cr = s.createCriteria(SimpleModel.class);
// If testtype is not 5 then it is regular segment export
return cr.list();
}
}
但是,现在列表返回0(我认为它因为实体没有与hibernate绑定)。有没有办法解决这个问题?
答案 0 :(得分:0)
使用Spring,更容易和更快。只需转到http://start.spring.io/并选择
即可下载并打开项目。
要实现REST,请阅读本教程:http://spring.io/guides/gs/rest-service/
如果你真的需要泽西而不是默认的SPring Rest,那么请阅读: http://geowarin.github.io/spring-boot/jersey/2014/01/31/a-simple-spring-boot-and-jersey-application.html
或者看看整合泽西岛的官方方式:https://github.com/dsyer/spring-boot-jersey