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

时间:2011-06-09 12:52:59

标签: hibernate annotations

有人能告诉我为什么会收到以下错误?

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 表,但是我收到了此错误。

以下是我使用的代码: 的 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;
    }
}

Address.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>  

Client.java

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();            
    }
}

有人可以帮我理解为什么我会收到这个错误吗?

感谢。

3 个答案:

答案 0 :(得分:35)

我想这是因为您在name中指定了<session-factory>属性,因此Hibernate尝试将SessionFactory绑定到该名称下的JNDI,但JNDI在您的环境中不可用。因此,请删除name属性。

答案 1 :(得分:7)

只要删除name属性,即使它的会话 - 工厂映射为空[hibernate.cfc.xml]

答案 2 :(得分:1)

要纠正错误,必须删除SessionFactory节点中的 name 属性。

enter image description here