当我使用数据库执行操作时,我的程序无法工作,我发现此异常:线程“main”中的异常org.hibernate.MappingException:未知实体:Ville
这是所有控制台结果
1 avr. 2016 09:59:44 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [spring.xml] 1 avr.
2016 09:59:44 org.hibernate.cfg.Environment <clinit> INFO: Hibernate 3.2.5 1 avr.
2016 09:59:44 org.hibernate.cfg.Environment <clinit> INFO: hibernate.properties not found 1 avr.
2016 09:59:44 org.hibernate.cfg.Environment buildBytecodeProvider INFO: Bytecode provider name : cglib 1 avr.
2016 09:59:44 org.hibernate.cfg.Environment <clinit> INFO: using JDK 1.4 java.sql.Timestamp handling 1 avr.
2016 09:59:44 org.hibernate.cfg.Configuration configure INFO: configuring from resource: dao/config/hibernate.cfg.xml 1 avr.
2016 09:59:44 org.hibernate.cfg.Configuration getConfigurationInputStream INFO: Configuration resource: dao/config/hibernate.cfg.xml 1 avr.
2016 09:59:44 org.hibernate.cfg.Configuration addResource INFO: Reading mappings from resource : dao/config/ville.hbm.xml 1 avr.
2016 09:59:46 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues INFO: Mapping class: model.Ville -> t_ville 1 avr.
2016 09:59:46 org.hibernate.cfg.Configuration doConfigure INFO: Configured SessionFactory: null 1 avr.
2016 09:59:46 org.hibernate.connection.DriverManagerConnectionProvider configure INFO: Using Hibernate built-in connection pool (not for production use!) 1 avr.
2016 09:59:46 org.hibernate.connection.DriverManagerConnectionProvider configure INFO: Hibernate connection pool size: 20 1 avr.
2016 09:59:46 org.hibernate.connection.DriverManagerConnectionProvider configure INFO: autocommit mode: false 1 avr.
2016 09:59:46 org.hibernate.connection.DriverManagerConnectionProvider configure ATTENTION: no JDBC Driver class was specified by property hibernate.connection.driver_class 1 avr.
2016 09:59:46 org.hibernate.connection.DriverManagerConnectionProvider configure INFO: using driver: null
at URL: jdbc:mysql://localhost:3306/tpproject 1 avr.
2016 09:59:46 org.hibernate.connection.DriverManagerConnectionProvider configure INFO: connection properties: {user=root, password=****, class_driver=com.mysql.jdbc.Driver} 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: RDBMS: MySQL, version: 5.6.17 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.7 ( Revision: ${svn.Revision} ) 1 avr.
2016 09:59:46 org.hibernate.dialect.Dialect <init> INFO: Using dialect: org.hibernate.dialect.MySQLDialect 1 avr.
2016 09:59:46 org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory INFO: Using default transaction strategy (direct JDBC transactions) 1 avr.
2016 09:59:46 org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended) 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: Automatic flush during beforeCompletion(): disabled 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: Automatic session close
at end of transaction: disabled 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: JDBC batch size: 15 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: JDBC batch updates for versioned data: disabled 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: Scrollable result sets: enabled 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: JDBC3 getGeneratedKeys(): enabled 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: Connection release mode: auto 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: Maximum outer join fetch depth: 2 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: Default batch fetch size: 1 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: Generate SQL with comments: disabled 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: Order SQL updates by primary key: disabled 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: Order SQL inserts for batching: disabled 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory 1 avr.
2016 09:59:46 org.hibernate.hql.ast.ASTQueryTranslatorFactory <init> INFO: Using ASTQueryTranslatorFactory 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: Query language substitutions: {} 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: JPA-QL strict compliance: disabled 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: Second-level cache: enabled 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: Query cache: disabled 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory createCacheProvider INFO: Cache provider: org.hibernate.cache.NoCacheProvider 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: Optimize cache for minimal puts: disabled 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: Structured second-level cache entries: disabled 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: Echoing all SQL to stdout 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: Statistics: disabled 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: Deleted entity synthetic identifier rollback: disabled 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: Default entity-mode: pojo 1 avr.
2016 09:59:46 org.hibernate.cfg.SettingsFactory buildSettings INFO: Named query checking : enabled 1 avr.
2016 09:59:46 org.hibernate.impl.SessionFactoryImpl <init> INFO: building session factory 1 avr.
2016 09:59:46 org.hibernate.impl.SessionFactoryObjectFactory addInstance INFO: Not binding factory to JNDI, no JNDI name configured Exception in thread "main" org.hibernate.MappingException: Unknown entity: Ville
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:550)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:68)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:815)
at dao.VilleDaoIplm.select(VilleDaoIplm.java:13)
at metier.prep.main(prep.java:16)
SessionFactoryUtil
package dao;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class SessionFctoryUtil {
private static SessionFactory sessionF;
SessionFctoryUtil(){
Configuration config = new Configuration();
config.configure("dao/config/hibernate.cfg.xml");
sessionF = config.buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
if (sessionF == null)
new SessionFctoryUtil();
return sessionF;
}
}
VilleDaoImpl
package dao;
import org.hibernate.SessionFactory;
import model.Ville;
public class VilleDaoIplm implements IVilleDao {
private final SessionFactory sf = SessionFctoryUtil.getSessionFactory();
@Override
public Ville select(int id) {
// TODO Auto-generated method stub
Ville v = (Ville)sf.openSession().get("Ville", id);
return v;
}
@Override
public void insert(Ville v) {
// TODO Auto-generated method stub
// Transaction trans=((Session) sf).beginTransaction();
try{
sf.getCurrentSession().save(v);
}
catch(org.hibernate.HibernateException he){
sf.openSession().save(v);
}
// trans.commit();
}
@Override
public void update(Ville v) {
// TODO Auto-generated method stub
sf.getCurrentSession().update(v);
}
@Override
public void delete(Ville v) {
// TODO Auto-generated method stub
sf.getCurrentSession().delete(v);
}
}
prep.java
package metier;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import dao.IVilleDao;
public class prep {
public static void main(String[] args) {
Resource r = new ClassPathResource("spring.xml");
BeanFactory beanF = new XmlBeanFactory(r);
IVilleDao d = (IVilleDao) beanF.getBean("dao");
System.out.println(d.select(1).getNom());
}
}
hibernate.cfg.xml中
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.class_driver">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/tpproject</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">20</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="dao/config/ville.hbm.xml" />
</session-factory>
</hibernate-configuration>
ville.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="model.Ville" table="t_ville">
<id name="id" type="int" column="id_v">
<generator class="assigned"/>
</id>
<property name="nom" column="nom_v" type="string"/>
</class>
</hibernate-mapping>
Class Ville
package model;
public class Ville {
private int id;
private String nom;
public Ville() {
super();
}
public Ville(int id, String nom) {
super();
this.id = id;
this.nom = nom;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getNom() {
return nom;
}
}
表t_ville
`CREATE TABLE IF NOT EXISTS `t_ville` ( `id_v` int(11) NOT NULL, `nom_v` varchar(50) DEFAULT NULL, PRIMARY KEY (`id_v`) )`
答案 0 :(得分:0)
您对get()
@Override
public Ville select(int id) {
// TODO Auto-generated method stub
Ville v = (Ville)sf.openSession().get(Ville.class, id);
return v;
}
或者
Ville v = (Ville)sf.openSession().get("model.Ville", id);