当前位置: 首页 > 面试题库 >

休眠:javax.naming.NoInitialContextException(通过注释进行组件映射)

姚星腾
2023-03-14
问题内容

有人可以告诉我为什么出现以下错误吗?

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
    at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.getNameParser(Unknown Source)
    at org.hibernate.util.NamingHelper.bind(NamingHelper.java:75)
    at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:113)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:348)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
    at com.transbinary.main.Client.main(Client.java:13)

我得到期望的结果,并用数据填充 人员 表,但出现此错误。

这是我正在使用的代码: Person.java

@Entity
public class Person implements Serializable {
    private Integer id;
    private Address homeAddress;

    @Id
    @GeneratedValue
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }

    public Address getHomeAddress() {
        return homeAddress;
    }
    public void setHomeAddress(Address homeAddress) {
        this.homeAddress = homeAddress;
    }
}

地址.java

@Embeddable
public class Address implements Serializable {
    private String city;
    private String Country;

    public Address() {} 
    public Address(String city, String country) {
        super();
        this.city = city;
        Country = country;
    }

    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getCountry() {
        return Country;
    }
    public void setCountry(String country) {
        Country = country;
    }
}

hibernate.cfg.xml

<hibernate-configuration>
 <session-factory name="">
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.password">password</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

  <property name="hibernate.hbm2ddl.auto">create</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.format_sql">true</property>

  <mapping class="com.transbinary.domain.Person"/>

 </session-factory>
</hibernate-configuration>

客户端程序

public class Client {
    public static void main(String[] args) {        
        SessionFactory sessionFactory =  new AnnotationConfiguration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction txn = session.beginTransaction();

        Person person = new Person();
        Address homeAddress = new Address("Mumbai", "India");
        person.setHomeAddress(homeAddress);

        session.save(person);

        txn.commit();
        session.close();            
    }
}

有人可以帮助我了解我为什么会收到该错误吗?

谢谢。


问题答案:

我猜这是由于您在中指定了name属性造成的<session- factory>,因此Hibernate尝试以SessionFactory该名称绑定到JNDI,但是JNDI在您的环境中不可用。因此,删除name属性。



 类似资料:
  • 问题内容: 如何在Grails 3.0.1中使用注释映射域类? 以下步骤对我不起作用。 步骤1 。我使用Grails 3.0.1()创建了一个新应用程序。 第二步 。如“ 使用hibernate注释映射”中所述,我在(也尝试过)中创建了一个新类。 第三步 。然后通过向文件中添加相关条目,向Hibernate sessionFactory注册该类,如下所示: 第四步 。启动应用程序()后,“欢迎使用

  • 问题内容: 我的Java模型上有一个枚举类型,我想映射到数据库中的表。我正在使用休眠注释,但我不知道该怎么做。由于我搜索的答案比较旧,我想知道哪种方法最好? 提前致谢 问题答案: 除了注释,您还需要其他东西吗?例如,以下枚举: 可以这样使用和注释: 您可以使用批注的enum属性指定枚举应如何在数据库中持久化。指定该枚举将作为整数值保留。在这里,设置为将永久保存为0,保留为1,依此类推。 替代方法是

  • 问题内容: 我有一张桌子DEAL和一张桌子DEAL_TYPE。我想映射以下代码: 问题在于数据已经存在于数据库中。而且我很难将类映射到数据库。 数据库看起来像这样: 我知道我可以在交易类型之间使用简单的@OneToMany关系,但是我更喜欢使用枚举。这可能吗? 我几乎可以通过使用EnumType.ORDINAL类型来使其工作。但不幸的是,我的交易类型表中的ID不是连续的,并且不是从1开始。 有什么

  • 问题内容: 休眠注释应该放在哪里? 它是我的实例变量上方的线吗?还是在吸气剂之前?还是在二传手之前?还是真的不重要吗? 非常感谢 问题答案: 您可以将它们放在 野外 或 吸气剂上 。从《 Hibernate注释参考指南》中: 2.2.1。将POJO标记为持久实体 (…) 根据您是否注释字段或方法,Hibernate使用的访问类型将是字段或属性。EJB3规范要求您在将要访问的元素类型上声明注释,即,

  • 问题内容: 有一个结构。我想以这种方式链接这三个实体:公司应包含ID,公司名称和部门列表,每个部门都有一个工人列表,ID和部门名称。每个工人都有名字,身份证。 我试图与一对多和多对一建立联系,但未成功。 公司 部门 工人 我从开始: 它填充公司,但不填充其他表,也没有创建任何联接(映射)错误: 问题答案: 除了Glenn Lane的答案中提到的级联,您还需要了解双向关联是如何工作的。 它们有一个所

  • 问题内容: 如果您正在使用xml映射,那么liquibase是hibernate的 hbm2ddl_auto 属性的完美替代方案。但是我使用的是JPA注释(hibernate注释)。那么可以使用liquibase吗? 问题答案: 是的,Liquibase使用了hibernate的元数据类,无论您使用xml映射还是注释,它们都是相同的。您确实需要一个hibernate配置文件来指向liquibase