javax.naming.NoInitialContextException with QueryDSL



我正在尝试使用QueryDSL编写一个简单的查询,但是我的尝试失败了以下例外。

Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

我尝试通过以下方式执行查询。

SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
JPQLQuery query = new HibernateQuery(session);
QClient t = QClient.client;
List<Client> lst = query.from(t).list(t);
System.out.println(lst.size());

和另一种方式。

    EntityManagerFactory emf = 
            Persistence.createEntityManagerFactory("my.package.entities");
    EntityManager em = emf.createEntityManager();
    JPAQuery query = new JPAQuery(em);
    QClient t = QClient.client;
    List<Client> lst = query.from(t).list(t);
    System.out.println(lst.size());

如前所述,这两种方式都失败了。

我正在使用postress db,并且参数在 hibernate.cfg.xml中指定。

我需要设置更多东西才能工作吗?

javax.Naming软件包包括JNDI API。由于它只是API,而不是实现,因此您需要告诉它要使用的JNDI实现。实现通常针对您要与之交谈的服务器。

要指定实现,您可以在构造初始信息时传递属性对象。这些属性指定要使用的实现以及服务器的位置。默认的initialContext构造函数仅在存在系统属性时才有用,但是属性与您手动传递的属性相同。

您需要设置哪些属性,取决于您的服务器。您需要将这些设置捕捉并插入其中。

样本:

      Properties properties = new Properties();
      properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
      properties.put("jnp.socket.Factory", "org.jnp.interfaces.TimedSocketFactory");
      properties.setProperty("java.naming.provider.url", "jnp://" + remoteHost + :1099");
      context = new InitialContext(properties);

寻找这个

问题

最新更新