我在java.below中正在做hibernate示例是使用的代码
的hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/serverdata</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<mapping resource="table.hbm.xml"/>
</session-factory>
</hibernate-configuration>
table.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="tabledata" table="tabledata">
<id name="id" column="id" type="ïnteger">
<generator class="assigned"></generator>
</id>
<property name="Name" column="Name" type="string"></property>
</class>
</hibernate-mapping>
inserttable.java
public class Inserttable {
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration cfg = new Configuration();
cfg.configure("Hibernate.cfg.xml");
SessionFactory sf = cfg.buildSessionFactory();
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
tabledata tab = new tabledata();
tab.setId(1);
tab.setName("mack");
s.save(tab);
s.flush();
tx.commit();
s.close();}
}
我正在使用mysql数据库,所以我使用的是mysql-connector,下面是使用的以下jar
mysql-connector-java-5.1.38-bin.jar
antlr-2.7.6.jar
cglib-2.2.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
Hibernate3.jar
Hibernate-jpa-2.0-api-1.0.0.Final.jar
Hibernate-Testing.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
ehcache-1.2.3.jar
slf4j-jdk14-1.5.8.jar
slf4j-api-1.5.8.jar
commons-logging-1.0.4.jar
它显示以下错误
Dec 26, 2015 6:32:31 PM org.hibernate.annotations.common.Version <clinit>
INFO: Hibernate Commons Annotations 3.2.0.Final
Dec 26, 2015 6:32:31 PM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.6.4.Final
Dec 26, 2015 6:32:31 PM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Dec 26, 2015 6:32:31 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : javassist
Dec 26, 2015 6:32:31 PM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Dec 26, 2015 6:32:31 PM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: Hibernate.cfg.xml
Dec 26, 2015 6:32:31 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: Hibernate.cfg.xml
Dec 26, 2015 6:32:31 PM org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : firsthb/table.hbm.xml
Dec 26, 2015 6:32:31 PM org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
Dec 26, 2015 6:32:31 PM org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: tabledata -> tabledata
Dec 26, 2015 6:32:31 PM org.hibernate.cfg.Configuration applyHibernateValidatorLegacyConstraintsOnDDL
INFO: Hibernate Validator not found: ignoring
Dec 26, 2015 6:32:31 PM org.hibernate.cfg.search.HibernateSearchEventListenerRegister enableHibernateSearch
INFO: Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
Dec 26, 2015 6:32:31 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
Dec 26, 2015 6:32:31 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 20
Dec 26, 2015 6:32:31 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: false
Dec 26, 2015 6:32:31 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/serverdata
Dec 26, 2015 6:32:31 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=root, password=****, autocommit=false}
Sat Dec 26 18:32:31 IST 2015 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Dec 26, 2015 6:32:32 PM org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.MySQLDialect
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Database ->
name : MySQL
version : 5.7.10-log
major : 5
minor : 7
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Driver ->
name : MySQL Connector Java
version : mysql-connector-java-5.1.38 ( Revision: fe541c166cec739c74cc727c5da96c1028b4834a )
major : 5
minor : 1
Dec 26, 2015 6:32:32 PM org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
INFO: Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
Dec 26, 2015 6:32:32 PM org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic flush during beforeCompletion(): disabled
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic session close at end of transaction: disabled
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch size: 15
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch updates for versioned data: disabled
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable result sets: enabled
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3 getGeneratedKeys(): enabled
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection release mode: auto
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Maximum outer join fetch depth: 2
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default batch fetch size: 1
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL with comments: enabled
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL updates by primary key: disabled
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL inserts for batching: disabled
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
Dec 26, 2015 6:32:32 PM org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
INFO: Using ASTQueryTranslatorFactory
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JPA-QL strict compliance: disabled
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level cache: enabled
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache: disabled
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory createRegionFactory
INFO: Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: disabled
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured second-level cache entries: disabled
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Echoing all SQL to stdout
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics: disabled
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted entity synthetic identifier rollback: disabled
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default entity-mode: pojo
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Named query checking : enabled
Dec 26, 2015 6:32:32 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Check Nullability in Core (should be disabled when Bean Validation is on): enabled
Dec 26, 2015 6:32:32 PM org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
Dec 26, 2015 6:32:32 PM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [materialized_clob] overrides previous : org.hibernate.type.MaterializedClobType@133e16fd
Dec 26, 2015 6:32:32 PM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [clob] overrides previous : org.hibernate.type.ClobType@51b279c9
Dec 26, 2015 6:32:32 PM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [java.sql.Clob] overrides previous : org.hibernate.type.ClobType@51b279c9
Dec 26, 2015 6:32:32 PM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [wrapper_characters_clob] overrides previous : org.hibernate.type.CharacterArrayClobType@1ad282e0
Dec 26, 2015 6:32:32 PM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [materialized_blob] overrides previous : org.hibernate.type.MaterializedBlobType@7f416310
Dec 26, 2015 6:32:32 PM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [wrapper_materialized_blob] overrides previous : org.hibernate.type.WrappedMaterializedBlobType@1cab0bfb
Dec 26, 2015 6:32:32 PM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [characters_clob] overrides previous : org.hibernate.type.PrimitiveCharacterArrayClobType@5e955596
Dec 26, 2015 6:32:32 PM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [blob] overrides previous : org.hibernate.type.BlobType@50de0926
Dec 26, 2015 6:32:32 PM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [java.sql.Blob] overrides previous : org.hibernate.type.BlobType@50de0926
Exception in thread "main" org.hibernate.MappingException: entity class not found: tabledata
at org.hibernate.mapping.PersistentClass.getMappedClass(PersistentClass.java:125)
at org.hibernate.tuple.PropertyFactory.getGetter(PropertyFactory.java:191)
at org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:67)
at org.hibernate.tuple.entity.EntityMetamodel.<init> (EntityMetamodel.java:135)
at org.hibernate.persister.entity.AbstractEntityPersister.<init> (AbstractEntityPersister.java:485)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init> (SingleTableEntityPersister.java:133)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:286)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)
at firsthb.Inserttable.main(Inserttable.java:16)
Caused by: java.lang.ClassNotFoundException: tabledata
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:192)
at org.hibernate.mapping.PersistentClass.getMappedClass(PersistentClass.java:122)
... 9 more
请帮助我知道我的代码中的问题是什么,或者是关于添加jar文件的问题。我知道,任何帮助都表示赞赏。
编辑:在此之后也替换为tabledata.hbm.xml。我认为这将是hibernate学习初学者的好例子。
答案 0 :(得分:1)
您的文件夹符合正确的约定
资源文件应位于src / main / resources
下java files under src/main/java
虽然它不是必须遵循的,但是你应该自动消除一些问题,然后你就会转向惯例而不是配置。否则你可以在下面执行,因为hfg文件将能够从右侧文件夹中搜索hbm文件
答案 1 :(得分:0)
在您的文件Hibernate.cfg.xml中添加此
<mapping resource="firsthb/table.hbm.xml"/>