使用jersey与maven和hibernate的项目

使用jersey与maven和hibernate的项目,hibernate,maven,Hibernate,Maven,我正在创建一个简单的项目来了解更多关于jersey,hibernate-jpa-2.0-API。 我不确定是否需要在这个组合中加入Spring。我创建了一个模型类,并通过Jersey公开了一个端点。现在我想找到从模型中检索数据的方法 我能想到的一个可能的选择是创建一个服务(使用spring),它将通过entityManager获取数据。这需要hibernate内核。是否有其他方法可以检索数据,例如使用hibernate jpa而不使用spring 我看到这一点也有点晚了,但是没有hibernat

我正在创建一个简单的项目来了解更多关于
jersey
hibernate-jpa-2.0-API
。 我不确定是否需要在这个组合中加入Spring。我创建了一个模型类,并通过
Jersey
公开了一个端点。现在我想找到从模型中检索数据的方法

我能想到的一个可能的选择是创建一个服务(使用spring),它将通过
entityManager
获取数据。这需要hibernate内核。是否有其他方法可以检索数据,例如使用
hibernate jpa
而不使用
spring

我看到这一点也有点晚了,但是没有
hibernate配置文件
,还有其他方法可以做到这一点吗

谢谢

我添加了以下文件来尝试使用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();
}

}
我还创建了一个具有以下特性的DAO

public 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();
}
}
公共类SimpleModelDAO{
私有会话getCurrentSession(){
返回HibernateUtil.getSessionFactory().getCurrentSession();
}
公共SimpleModelDAO(){}
公共列表getResults(){
//TODO自动生成的构造函数存根
会话s=getCurrentSession();
事务tx=s.beginTransaction();
Criteria cr=s.createCriteria(SimpleModel.class);
//如果testtype不是5,则它是常规段导出
返回cr.list();
}
}

但是,现在列表返回0(我认为是因为实体没有与hibernate绑定)。有什么方法可以把它绑起来吗?

使用弹簧,它更容易,也更快。只需转到并选择

  • Jpa
  • 马文项目
下载并打开项目

要实现REST,请阅读本教程:

如果您确实需要运动衫而不是默认的弹簧支撑,请阅读:

或者看看集成Jersey的官方方法:

添加c.addAnnotatedClass(SimpleModel.class);实际上帮助解决了这个问题。