在实现hibernate-CRUD wia注释时发现hbm.xml未找到错误

时间:2014-05-13 09:11:00

标签: java xml hibernate

我是JPA的新手,我试图使用Hibernate实现在JPA中设置项目。我研究过我们可以使用注释编写代码或使用* .hbm.xml编写代码。我试图用注释来实现它,但是当我尝试向我的数据库插入值时,它会抛出一个错误,如下所示。

资源:找不到中心/ Centre.hbm.xml

我的Centre.java类如下

  /**
     * 
     */
    package centre;

    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;

    /**
     * @author anoop
     *
     */

    @Entity
    @Table(name="centre")
    public class Centre {

        private int id;
        private String name;
        private String address;
        private String comments;


        /**
         * @return the id
         */
        @Id
        public int getId() {
            return id;
        }
        /**
         * @param id the id to set
         */
        public void setId(int id) {
            this.id = id;
        }
        /**
         * @return the name
         */
        public String getName() {
            return name;
        }
        /**
         * @param name the name to set
         */
        public void setName(String name) {
            this.name = name;
        }
        /**
         * @return the address
         */
        public String getAddress() {
            return address;
        }
        /**
         * @param address the address to set
         */
        public void setAddress(String address) {
            this.address = address;
        }
        /**
         * @return the comments
         */
        public String getComments() {
            return comments;
        }
        /**
         * @param comments the comments to set
         */
        public void setComments(String comments) {
            this.comments = comments;
        }

    }

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="gurukul">
        <class>centre.Centre</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
    </persistence-unit>
</persistence>

冬眠-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.postgresql.Driver</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1/DBNAME</property>
            <property name="hibernate.connection.username">username</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="connection.pool_size">1</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
    </session-factory>
</hibernate-configuration>

HibernateUtil.java

    private static final SessionFactory sessionFactory;

    static {
        try {
            sessionFactory = new AnnotationConfiguration().configure().addClass(Centre.class).buildSessionFactory();

            } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

/**
 * @return the em
 */
public static EntityManager getEm() {
    return em;
}

GurukulDAO.java

  /**
     * 
     */
    package dao;

    import javax.persistence.EntityManager;
    import javax.persistence.EntityTransaction;

    import org.hibernate.Session;
    import org.hibernate.Transaction;

    import util.HibernateUtil;
    import centre.Centre;

    /**
     * @author anoop
     *
     */
    public class GurukulDAO {

        public void saveCentre(String name, String address, String comments){
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction txn = session.beginTransaction();

            Centre centre = new Centre();
            centre.setName(name);
            centre.setAddress(address);
            centre.setComments(comments);

            session.save(centre);
            txn.commit();

            txn.begin();
        }


    }

Nainclass.java

 package main;

    import dao.GurukulDAO;

    public class MainClass {

        public static void main(String s[]){
            GurukulDAO gurukulDAO = new GurukulDAO();
            gurukulDAO.saveCentre("BajajNagar", "Flat no. 5", "Head Office");
        }

    }

当我执行我的MainClass时,我收到了错误

 1 [main] INFO org.hibernate.cfg.annotations.Version - Hibernate Annotations 3.4.0.GA
    13 May, 2014 2:21:21 PM org.hibernate.cfg.Environment <clinit>
    INFO: Hibernate 3.2 cr4
    13 May, 2014 2:21:21 PM org.hibernate.cfg.Environment <clinit>
    INFO: hibernate.properties not found
    13 May, 2014 2:21:21 PM org.hibernate.cfg.Environment buildBytecodeProvider
    INFO: Bytecode provider name : cglib
    13 May, 2014 2:21:21 PM org.hibernate.cfg.Environment <clinit>
    INFO: using JDK 1.4 java.sql.Timestamp handling
    13 May, 2014 2:21:21 PM org.hibernate.cfg.Configuration configure
    INFO: configuring from resource: /hibernate.cfg.xml
    13 May, 2014 2:21:21 PM org.hibernate.cfg.Configuration getConfigurationInputStream
    INFO: Configuration resource: /hibernate.cfg.xml
    13 May, 2014 2:21:21 PM org.hibernate.cfg.Configuration doConfigure
    INFO: Configured SessionFactory: null
    13 May, 2014 2:21:21 PM org.hibernate.cfg.Configuration addClass
    INFO: Reading mappings from resource: centre/Centre.hbm.xml
    Initial SessionFactory creation failed.org.hibernate.MappingNotFoundException: resource: centre/Centre.hbm.xml not found
    Exception in thread "main" java.lang.ExceptionInInitializerError
        at util.HibernateUtil.<clinit>(HibernateUtil.java:32)
        at dao.GurukulDAO.saveCentre(GurukulDAO.java:22)
        at main.MainClass.main(MainClass.java:9)
    Caused by: org.hibernate.MappingNotFoundException: resource: centre/Centre.hbm.xml not found
        at org.hibernate.cfg.Configuration.addClass(Configuration.java:538)
        at org.hibernate.cfg.AnnotationConfiguration.addClass(AnnotationConfiguration.java:963)
        at util.HibernateUtil.<clinit>(HibernateUtil.java:25)
        ... 2 more

请指导我在我的代码中做错了什么。

2 个答案:

答案 0 :(得分:1)

persistence.xml 文件中删除<class>条目,并且根据日志的报告,您需要为Hibernate指定映射文件描述符,这可以是通过将<mapping>添加到 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.postgresql.Driver</property>
    <property name="hibernate.connection.password">password</property>
    <property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1/DBNAME</property>
    <property name="hibernate.connection.username">username</property>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="connection.pool_size">1</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">create</property>
    <mapping class="centre.Centre" /> <!-- You can also specify a whole package with <mapping package="centre" /> -->
  </session-factory>
</hibernate-configuration>

答案 1 :(得分:0)

从我的HibernateUtil.java中删除了addClass(Centre.class),它对我有用。