Hibernate:javax.naming.NoInitialContextException(通过注释进行组件映射)
有人可以告诉我为什么会出现以下错误吗?
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();
}
}
有人可以帮助我理解为什么我是这样吗?得到那个错误?
谢谢。
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.
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?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜这是由于您在
中指定了name
属性引起的,因此 Hibernate 尝试将SessionFactory
绑定到 JNDI 下该名称,但 JNDI 在您的环境中不可用。因此,删除name
属性。I guess it's caused by the fact that you specified
name
attribute in<session-factory>
, therefore Hibernate tries to bindSessionFactory
to JNDI under that name, but JNDI is not available in your environment. So, removename
attribute.只需删除 name 属性,即使它在会话工厂映射中为空 [hibernate.cfc.xml]
Just remove the name attribute even if its empty from your session-factory mapping [hibernate.cfc.xml]
要修复错误,您必须删除 SessionFactory 节点中的 name 属性。
To fix your error, you must delete the name attribute in the SessionFactory node.