2015-06-25 63 views
1

大家好我不擅長Hibernate。所以當我超越我的班級考試。它會在控制檯中顯示我的配置文件xml映射(資源:com.live.hibernate.User.hbm.xm)未找到。我做一個簡單的例子,如:在映射我的bean時在Hibernate配置文件中出現異常

User.java

package com.live.beans; 

public class User { 

    private int id; 
    private String Name; 
    private String email; 
    private String password; 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getName() { 
     return Name; 
    } 

    public void setName(String name) { 
     Name = name; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    public User(int id, String name, String email, String password) { 
     super(); 
     this.id = id; 
     Name = name; 
     this.email = email; 
     this.password = password; 
    } 

    @Override 
    public String toString() { 
     return "User [id=" + id + ", Name=" + Name + ", email=" + email 
       + ", password=" + password + "]"; 
    } 

} 

User.hbm.xml

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
"-//Hibernate/Hibernate Mapping DTD//EN" 
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
    <class> 
     <id name="id" 
      type="int" 
      column="id" 
      length="15"/> 
     <property name="name" 
        type="string" 
        column="name" 
        length="45" 
     /> 
     <property name="email" 
        type="string" 
        column="email" 
        length="45" 
     /> 
     <property name="password" 
        type="string" 
        column="password" 
        length="45" 
     /> 
    </class> 

</hibernate-mapping> 

我創建了一個簡單的Java類測試(HibernateTest。 java):

package com.live.presentation; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 

import com.live.beans.User; 

public class HibernateTest { 

    private Session session; 

    private void openSession(){ 
     Configuration conf = new Configuration(); 
     SessionFactory sessionFactory= conf.configure().buildSessionFactory(); 
     Session session = sessionFactory.openSession(); 
     session.beginTransaction(); 
    } 

    private void closeSession(){ 
     session.getTransaction().commit(); 
     session.close(); 
    } 

    public HibernateTest() { 
     openSession(); 
     User p = new User(1, "Ali", "[email protected]", "password"); 
     session.save(p); 
     System.out.println("sauvegarde reussi"); 
     closeSession(); 
    } 

    public static void main(String[] args) { 

     new HibernateTest(); 
    } 

} 

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>p01</groupId> 
    <artifactId>p01</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <build> 
    <sourceDirectory>src</sourceDirectory> 
    <plugins> 
     <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>3.1</version> 
     <configuration> 
      <source>1.6</source> 
      <target>1.6</target> 
     </configuration> 
     </plugin> 
     <plugin> 
     <artifactId>maven-war-plugin</artifactId> 
     <version>2.4</version> 
     <configuration> 
      <warSourceDirectory>WebContent</warSourceDirectory> 
      <failOnMissingWebXml>false</failOnMissingWebXml> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
    <name>HibernateTest</name> 
    <url>http://maven.apache.org</url> 
    <dependencies> 
      <dependency> 
      <groupId>com.sun.faces</groupId> 
      <artifactId>jsf-api</artifactId> 
      <version>2.1.7</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.faces</groupId> 
      <artifactId>jsf-impl</artifactId> 
      <version>2.1.7</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>4.0.1.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-validator</artifactId> 
      <version>4.2.0.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate.common</groupId> 
      <artifactId>hibernate-commons-annotations</artifactId> 
      <version>4.0.1.Final</version> 
      <classifier>tests</classifier> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate.javax.persistence</groupId> 
      <artifactId>hibernate-jpa-2.0-api</artifactId> 
      <version>1.0.1.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <version>4.0.1.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.validation</groupId> 
      <artifactId>validation-api</artifactId> 
      <version>1.0.0.GA</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>1.6.4</version> 
     </dependency> 
     <dependency> 
      <groupId>org.jboss.logging</groupId> 
      <artifactId>jboss-logging</artifactId> 
      <version>3.1.0.CR2</version> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-log4j12</artifactId> 
      <version>1.6.4</version> 
     </dependency> 

     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.10</version> 
     </dependency> 


    </dependencies> 
</project> 

這是我的控制檯:

log4j:WARN No appenders could be found for logger (org.jboss.logging). 
log4j:WARN Please initialize the log4j system properly. 
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 
Exception in thread "main" org.hibernate.MappingNotFoundException: resource: com.live.hibernate.User.hbm.xml not found 
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:724) 
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2102) 
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2074) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2054) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2007) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1922) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1901) 
    at com.live.presentation.HibernateTest.openSession(HibernateTest.java:15) 
    at com.live.presentation.HibernateTest.<init>(HibernateTest.java:26) 
    at com.live.presentation.HibernateTest.main(HibernateTest.java:35) 

編輯

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
"-//Hibernate/Hibernate Configuration DTD//EN" 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 

     <property name="hibernate.connection.url">jdbc:mysql://localhost/ment</property> 

     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 

     <property name="hibernate.connection.username">root</property> 

     <property name="hibernate.connection.password"></property> 

     <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 

     <property name="hibernate.show_sql">false</property> 

     <mapping resource="com.live.hibernate.User.hbm.xml" /> 

    </session-factory> 
</hibernate-configuration> 

文件夾結構

src 
|-- com 
|-- live 
| |-- hibernate 
| | `-- User.hbm.xml 
    live 
     |`--beans 
     | `-- User.java 
    live 
     `--presentation 
     `-- HibernateTest.java 
WebContent 
|-- META-INF 
|-- WEB-INF 
|   
|-- index.xhtml 
pom.xml 

別的東西 我的話是,當我調試,我這裏有一個例外:

SessionFactory sessionFactory= conf.configure().buildSessionFactory(); 

你知道buildSessionFactory()方法已過時。任何想法 ?

請有人可以幫助我!

+0

你寫過Hibernate配置文件? – Badal

+0

@Badal:我編輯了它,請再次檢查我的文章 – laciane

+0

Donot在你的配置文件中指定你的hbm文件名和軟件包。簡單地寫'<映射資源=「User.hbm.xml」/>' – Badal

回答

0

您的hibernate.cfg.xml在哪裏?

確保你把你的hibernate.cfg.xml和你User.hbm.xml在相同的源文件夾和hibernate.cfg.xml你把這樣的內部:

<mapping resource="User.hbm.xml" /> 
+0

是的,我做到了 laciane

+0

如果你的'User.hbm.xml'與你的'hibernate.cfg.xml'在同一個文件夾中,你應該聲明像這樣:''不要把'com。liveSpeaker.hibernate.User.hbm.xml' –

+0

不,它們不在同一個文件夾中 – laciane

相關問題