我正在使用netbeans 6.9和Hibernate而我正在尝试在数据库中插入一些值(postgres)但是我得到以下异常:
INFO: Not binding factory to JNDI, no JNDI name configured Exception in thread "main" org.hibernate.MappingException: Unknown entity: SELECTPACK.beanClass at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:550) at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1338) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:98) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187) at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:3
和我的hbm.xml文件在下面给出
<class name="SELECTPACK.beanClass" table="login">
<id column="id" name="id">
<generator class="increment"/>
</id>
<property name="username" column="username"/>
<property name="password" column="password"/>
</class>
我的cfg.xml文件在下面给出
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://192.168.1.100:54321/postgres</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">dbserver</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="SELECTPACK/select.hbm.xml"/>
</session-factory>
</hibernate-configuration>
我的pojo课程
package SELECTPACK;
public class beanClass {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
听到的是我的主要课程
package SELECTPACK;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class selectMainClass {
public static void main(String... args) {
SessionFactory sessionFact = new Configuration().configure().buildSessionFactory();
Session ses = sessionFact.openSession();
Transaction tx = ses.beginTransaction();
beanClass bean = new beanClass();
bean.setPassword("hi....");
bean.setUsername("hi....");
ses.save(bean);
tx.commit();
/*String query = "select * from login";
Query qry = ses.createQuery(query);
ArrayList list = (ArrayList) qry.list();
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}*/
ses.close();
}
}
我不明白问题是什么。
答案 0 :(得分:3)
实体SELECTPACK.beanClass
未被识别为有效实体,并且日志中应存在先前的堆栈跟踪(创建SessionFactory
时),显示实际错误。寻找先前的痕迹。但可能的原因包括:
除此之外,我还有一些小的评论(不是问题):
selectpack
); BeanClass
); selectpack.BeanClass
映射文件中映射类selectpack/BeanClass.hbm.xml
(以便于维护); native
生成器。这个类似的东西:
package selectpack;
public class BeanClass {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
对于映射文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="selectpack.BeanClass" table="login">
<id column="id" name="id">
<generator class="native"/>
</id>
<property name="username" column="username"/>
<property name="password" column="password"/>
</class>
</hibernate-mapping>
当然,请相应地更新hibernate.cfg.xml
:
<mapping resource="selectpack/BeanClass.hbm.xml"/>
但正如我所说,这些言论与问题没有直接关系。