我是Java新手,剛剛從在線教程開始學習Hibernate。我用Hibernate創建了Java代碼,但之後我一直未能測試運行。 我有MySQL數據庫「測試」中的數據庫表「用戶」。當我嘗試插入一條記錄時,它會給我這個錯誤。無法找到確切的原因..Hibernate映射異常:未知實體
堆棧:
710 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
org.hibernate.MappingException: Unknown entity: in.shivasoft.pojo.Users
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:580)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1365)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:562)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:550)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:546)
at in.shivasoft.test.TestMain.saveRecord(TestMain.java:33)
at in.shivasoft.test.TestMain.main(TestMain.java:19)
my Users.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class entity-name="Users" name="in.shivasoft.pojo.Users" table="Users">
<meta attribute="description">This class is used to save the info about users</meta>
<id column="UserId" name="UserId" type="long"/>
<property column="FName" name="FName" type="string"/>
<property column="LName" name="LName" type="string"/>
<property column="UserTypeId" name="UserTypeId" type="long"/>
<property column="UserName" name="UserName" type="string"/>
<property column="Email" name="Email" type="string"/>
<property column="Pwd" name="Pwd" type="string"/>
<property column="Note" name="Note" type="string"/>
<property column="IsActive" name="IsActive" type="boolean"/>
</class>
</hibernate-mapping>
Hibernate配置(hibernate.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.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">1234</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property**strong text**>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<mapping resource="in/shivasoft/pojo/Users.hbm.xml"/>
</session-factory>
</hibernate-configuration>
ŧ他是Hibernate代碼生成創建的實體類
package in.shivasoft.pojo;
// Generated Sep 27, 2013 11:48:09 AM by Hibernate Tools 4.0.0
/**
* Users generated by hbm2java
*/
public class Users implements java.io.Serializable {
private long UserId;
private String FName;
private String LName;
private long UserTypeId;
private String UserName;
private String Email;
private String Pwd;
private String Note;
private boolean IsActive;
public Users() {
}
public Users(long UserId) {
this.UserId = UserId;
}
public Users(long UserId, String FName, String LName, long UserTypeId,
String UserName, String Email, String Pwd, String Note,
boolean IsActive) {
this.UserId = UserId;
this.FName = FName;
this.LName = LName;
this.UserTypeId = UserTypeId;
this.UserName = UserName;
this.Email = Email;
this.Pwd = Pwd;
this.Note = Note;
this.IsActive = IsActive;
}
public long getUserId() {
return this.UserId;
}
public void setUserId(long UserId) {
this.UserId = UserId;
}
public String getFName() {
return this.FName;
}
public void setFName(String FName) {
this.FName = FName;
}
public String getLName() {
return this.LName;
}
public void setLName(String LName) {
this.LName = LName;
}
public long getUserTypeId() {
return this.UserTypeId;
}
public void setUserTypeId(long UserTypeId) {
this.UserTypeId = UserTypeId;
}
public String getUserName() {
return this.UserName;
}
public void setUserName(String UserName) {
this.UserName = UserName;
}
public String getEmail() {
return this.Email;
}
public void setEmail(String Email) {
this.Email = Email;
}
public String getPwd() {
return this.Pwd;
}
public void setPwd(String Pwd) {
this.Pwd = Pwd;
}
public String getNote() {
return this.Note;
}
public void setNote(String Note) {
this.Note = Note;
}
public boolean isIsActive() {
return this.IsActive;
}
public void setIsActive(boolean IsActive) {
this.IsActive = IsActive;
}
}
這是我TestMain.Java
package in.shivasoft.test;
import in.shivasoft.pojo.Users;
import in.shivasoft.util.HibernateUtil;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
TestMain obj = new TestMain();
obj.saveRecord();
obj.getList();
}
public void saveRecord()
{
Users u = new Users(0, "Jitendra", "Zaa", 1, "jitendra.zaa", "[email protected]", "test", "this is note", true);
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try
{
transaction = session.beginTransaction();
session.save(u);
transaction.commit();
System.out.println("Data Saved");
}catch(Exception e)
{
e.printStackTrace();
}finally{session.close();}
}
public void updateUser(long UserId)
{
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try
{
transaction = session.beginTransaction();
Users u = (Users)session.get(Users.class,UserId);
u.setFName("ShivaSoft");
transaction.commit();
System.out.println("Data Updated");
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
session.close();
}
}
public void getList()
{
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try
{
transaction = session.beginTransaction();
List<Users> uList = List<Users> uList = Session.createCriteria(Users.class).list();
for(Users u : uList)
{
System.out.println("First Name - "+u.getFName());
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
session.close();
}
}
}
這是登錄會話工廠不知道是否有它的問題..
10 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.3.1.GA
13 [main] INFO org.hibernate.cfg.Environment - loaded properties from resource hibernate.properties: {hibernate.bytecode.use_reflection_optimizer=false}
15 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
19 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
61 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
61 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
128 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : in/shivasoft/pojo/Users.hbm.xml
197 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: Users -> users
208 [main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null
256 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
256 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20
256 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
262 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: org.gjt.mm.mysql.Driver at URL: jdbc:mysql://localhost/test
262 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=root, password=****}
499 [main] INFO org.hibernate.cfg.SettingsFactory - RDBMS: MySQL, version: 5.6.14-log
499 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.0 ($Date: 2007-04-05 23:56:05 +0200 (Thu, 05 Apr 2007) $, $Revision: 6376 $)
515 [main] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
518 [main] INFO org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions)
519 [main] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
519 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
519 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
520 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
520 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
520 [main] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
520 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled
520 [main] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto
521 [main] INFO org.hibernate.cfg.SettingsFactory - Maximum outer join fetch depth: 2
521 [main] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
521 [main] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
521 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
521 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
521 [main] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
523 [main] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
523 [main] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
523 [main] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled
523 [main] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
523 [main] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled
523 [main] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
523 [main] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
523 [main] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
527 [main] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled
528 [main] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
528 [main] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
528 [main] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled
568 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
737 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
你的包聲明是否正確,還需要TestMain.java片段來檢查! – aksappy
如果你嘗試選擇記錄,會發生什麼?在保存session.getCriteria(Users.class).list();' – smajlo
嘗試'session.persist (u);'而不是保存 – smajlo