休眠:javax.naming.NoInitialContextException(component映射通过注释) [英] Hibernate: javax.naming.NoInitialContextException (Component Mapping via Annotations)

查看:117
本文介绍了休眠:javax.naming.NoInitialContextException(component映射通过注释)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Could someone tell me why am I getting the following error?

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)

我得到想要的结果,并获得在个人表格填入数据,但我得到这个错误。

I am getting the desired result and getting the person table populated with the data but I am getting this error.

下面是code对我使用:
Person.java

Here is the code for I am using: 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();            
    }
}

有人能帮助我了解为什么我收到这个错误?

Could someone help me understand why am I getting that error?

感谢。

推荐答案

我猜它是由您指定的事实引起了&LT 名称属性;会话工厂&GT; ,因此,Hibernate尝试绑定的SessionFactory 来下这个名字JNDI,但JNDI是不可用在您的环境。因此,删除名称属性。

I guess it's caused by the fact that you specified name attribute in <session-factory>, therefore Hibernate tries to bind SessionFactory to JNDI under that name, but JNDI is not available in your environment. So, remove name attribute.

这篇关于休眠:javax.naming.NoInitialContextException(component映射通过注释)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆