【问题标题】:javax.naming.NoInitialContextException with QueryDSL带有 QueryDSL 的 javax.naming.NoInitialContextException
【发布时间】:2018-07-22 08:58:33
【问题描述】:

我正在尝试使用 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());

如上所述,这两种方式都失败了,但出现了相同的异常。

我使用的是Postrges DB,参数在hibernate.cfg.xml中指定。

我是否需要进行更多设置才能使其正常工作?

【问题讨论】:

  • 同时添加hibernate.cfg.xml内容

标签: java hibernate jndi jpql querydsl


【解决方案1】:

javax.naming 包包含 JNDI API。由于它只是一个 API,而不是一个实现,因此您需要告诉它要使用哪个 JNDI 实现。这些实现通常特定于您尝试与之通信的服务器。

要指定实现,请在构造 InitialContext 时传入一个 Properties 对象。这些属性指定要使用的实现,以及服务器的位置。默认的 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);

寻找这个

Issue

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-04
    • 2014-02-27
    • 2021-03-22
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 2017-12-18
    相关资源
    最近更新 更多