这是我在执行Hibernate程序时收到的错误
Jul 02, 2015 6:07:12 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Jul 02, 2015 6:07:12 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.2.Final}
Jul 02, 2015 6:07:12 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jul 02, 2015 6:07:12 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jul 02, 2015 6:07:12 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Jul 02, 2015 6:07:12 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2005)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1986)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1966)
at com.jlcindia.hibernate.Client.main(Client.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Process finished with exit code 0
我不确定我的问题是什么,但这就是我所拥有的:
我的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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/test</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="com/jlcindia/hibernate/Author.hbm.xml" />
</session-factory>
</hibernate-configuration>
Author.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" >
<!-- ${Example for "COLLECTION MAPPING" } -->
<hibernate-mapping package="com.jlcindia.hibernate">
<class name="Author" table="jlcauthors">
<id name="authorId" column="authorId" type="int">
<generator class="increment"/>
</id>
<property name="authorName"/>
<property name="designation"/>
<list name="emails" table="emails">
<key column="authorId"/>
<index column="myIndex"/>
<element column="emailId" type="string"/>
</list>
<bag name="qualifications" table="qualifications">
<key column="authorId"/>
<element column="qualification" type="string"/>
</bag>
<map name="phones" table="phones">
<key column="authorId"/>
<index column="ptype" type="string"/>
<element column="phoneNo" type="long"/>
</map>
<set name="books" table="books">
<key column="authorId"/>
<element column="bookName" type="string"/>
</set>
<array name="publications" table="publications">
<key column="authorId"/>
<index/>
<element column="publications" type="string"/>
</array>
</class>
</hibernate-mapping>
Client.java
代码:
package com.jlcindia.hibernate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class Client {
public static void main(String[] args) {
Transaction tx=null;
try {
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(
configuration.getProperties()).buildServiceRegistry();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
tx=session.beginTransaction();
List<String> ems=new ArrayList<String>();
ems.add("aa@jlc.com");
ems.add("bb@jlc.com");
Map<String,Long> phs=new HashMap<String,Long>();
phs.put("Off",new Long(111));
phs.put("Hou",new Long(222));
Set<String> bks=new HashSet<String>();
bks.add("Learn Java");
bks.add("Learn JDBC");
List<String> qus=new ArrayList<String>();
qus.add("M.Sc");
qus.add("B.Sc");
String[] pbs={"JLC1","JLC2"};
Author a=new Author("sri",ems,phs,bks,qus,pbs,"S.E");
session.save(a);
tx.commit();
session.close();
} catch (Exception e) {
e.printStackTrace();
if(tx!=null){
tx.rollback();
}
}
}
}
任何帮助将不胜感激。如果我遗漏任何细节或您需要更多信息,请告诉我。
感谢。
答案 0 :(得分:0)
取自hibernate.cfg.xml not found:
您的
hibernate.cfg.xml
需要位于src
目录中;否则它不会被Ant的copymetafiles
目标覆盖,所以它不会在你编译的类路径中结束。