映射类时,Hibernate Association引用未映射的类

时间:2014-08-02 07:33:43

标签: java xml hibernate

我在hibernate项目中映射了所有实体类。但我仍然得到一个关联引用关联引用未映射的类:Items。请有人帮我解决这个问题。由于我对冬眠很新,请给我一个详细的答案。提前谢谢。

Items.java

public class Items {

private long id;
private String itemId;
private double itemTotal;
private int quantity;
private Cart cart;

// no-args constructor getters and setters

Cart.java

    package com.journaldev.hibernate.model;

import java.util.Set;

public class Cart {

    private long id;
    private double total;
    private String name;
    private Set<Items> items;



     // no-args constructor getters and setters

的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.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/composition?zeroDateTimeBehavior=convertToNull</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root123</property>
    <mapping resource="cart.hbm.xml"/>
    <mapping resource="Item.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

Item.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">
<hibernate-mapping>
  <class name="com.journaldev.hibernate.model.Items" table="items">
      <id name="id" type="long">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="itemId" type="string">
            <column name="item_id"></column>
        </property>
        <property name="itemTotal" type="double">
            <column name="item_total"></column>
        </property>
        <property name="quantity" type="integer">
            <column name="quantity"></column>
        </property>

        <many-to-one name="cart" class="Cart">
            <column name="cart_id" not-null="true"></column>
        </many-to-one>
    </class>
</hibernate-mapping>

cart.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">
<hibernate-mapping>
  <class name="com.journaldev.hibernate.model.Cart" table="cart">
       <id name="id" type="long">
            <column name="cart_id" />
            <generator class="identity" />
        </id>
        <property name="total" type="double">
            <column name="total" />
        </property>
        <property name="name" type="string">
            <column name="name" />
        </property>
        <set name="items" table="ITEMS" fetch="select">
            <key>
                <column name="cart_id" not-null="true"></column>
            </key>
            <one-to-many class="Items"/>
        </set>
    </class>
</hibernate-mapping>

HibernateUtil.java

package com.journaldev.hibernate.util;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {

    private static SessionFactory sessionFactory;

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            Configuration configuration = new Configuration();
            configuration.configure("hibernate.cfg.xml");
            System.out.println("Hibernate Configuration loaded");

            /* ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
            System.out.println("Hibernate serviceRegistry created");

            SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

             */

            sessionFactory = new Configuration().configure().buildSessionFactory();
            return sessionFactory;
        }
        catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            ex.printStackTrace();
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        if(sessionFactory == null) sessionFactory = buildSessionFactory();
        return sessionFactory;
    }
}

HibernateOneToManyMain.java

package com.journaldev.hibernate.main;

import java.util.HashSet;
import java.util.Set;

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

import com.journaldev.hibernate.model.Cart;
import com.journaldev.hibernate.model.Items;
import com.journaldev.hibernate.util.HibernateUtil;

public class HibernateOneToManyMain {

    public static void main(String[] args) {

        Cart cart = new Cart();
        cart.setName("MyCart");

        Items item1 = new Items("I1", 10, 1, cart);
        Items item2 = new Items("I2", 20, 2, cart);
        Set<Items> itemsSet = new HashSet<Items>();
        itemsSet.add(item1); itemsSet.add(item2);

        cart.setItems(itemsSet);
        cart.setTotal(10*1 + 20*2);

        SessionFactory sessionFactory = null;
        Session session = null;
        Transaction tx = null;
        try{
        //Get Session
        sessionFactory = HibernateUtil.getSessionFactory();
        session = sessionFactory.getCurrentSession();
        System.out.println("Session created");
        //start transaction
        tx = session.beginTransaction();

        //Save the Model objects
        session.save(cart);
        session.save(item1);
        session.save(item2);

        //Commit transaction
        tx.commit();
        System.out.println("Cart ID="+cart.getId());

        }catch(Exception e){
            System.out.println("Exception occured. "+e.getMessage());
            e.printStackTrace();
        }finally{
            if(!sessionFactory.isClosed()){
                System.out.println("Closing SessionFactory");
                sessionFactory.close();
            }
        }
    }

}

这是我的StackTrace

run:
Aug 02, 2014 3:23:06 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Aug 02, 2014 3:23:06 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.6.Final}
Aug 02, 2014 3:23:06 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Aug 02, 2014 3:23:06 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Aug 02, 2014 3:23:06 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
Aug 02, 2014 3:23:06 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
Aug 02, 2014 3:23:06 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Aug 02, 2014 3:23:06 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: cart.hbm.xml
Aug 02, 2014 3:23:06 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Aug 02, 2014 3:23:06 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Items.hbm.xml
Aug 02, 2014 3:23:06 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Aug 02, 2014 3:23:06 PM org.hibernate.cfg.Configuration doConfigure
Hibernate Configuration loaded
INFO: HHH000041: Configured SessionFactory: null
Aug 02, 2014 3:23:06 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Aug 02, 2014 3:23:06 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Aug 02, 2014 3:23:06 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Aug 02, 2014 3:23:06 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: cart.hbm.xml
Aug 02, 2014 3:23:06 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Aug 02, 2014 3:23:06 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Items.hbm.xml
Aug 02, 2014 3:23:06 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Aug 02, 2014 3:23:06 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Aug 02, 2014 3:23:06 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Aug 02, 2014 3:23:06 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
Aug 02, 2014 3:23:06 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
Aug 02, 2014 3:23:06 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/composition?zeroDateTimeBehavior=convertToNull]
Aug 02, 2014 3:23:06 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****}
Aug 02, 2014 3:23:06 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Initial SessionFactory creation failed.org.hibernate.MappingException: Association references unmapped class: Items
org.hibernate.MappingException: Association references unmapped class: Items
    at org.hibernate.cfg.HbmBinder.bindCollectionSecondPass(HbmBinder.java:2521)
    at org.hibernate.cfg.HbmBinder$CollectionSecondPass.secondPass(HbmBinder.java:2807)
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:69)
    at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1632)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1390)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1777)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1861)
    at com.journaldev.hibernate.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:26)
    at com.journaldev.hibernate.util.HibernateUtil.getSessionFactory(HibernateUtil.java:37)
    at com.journaldev.hibernate.main.HibernateOneToManyMain.main(HibernateOneToManyMain.java:34)
Exception in thread "main" java.lang.NullPointerException
    at com.journaldev.hibernate.main.HibernateOneToManyMain.main(HibernateOneToManyMain.java:53)
Java Result: 1

2 个答案:

答案 0 :(得分:1)

尝试更换cart.hbm.xml文件中的set,如下所示:

<set name="items" table="ITEMS" fetch="select" cascade="all">
        <key>
            <column name="cart_id" not-null="true"></column>
        </key>
        <one-to-many class="Items"/>
    </set>

希望它有所帮助。

答案 1 :(得分:1)

按照建议更换套装后,替换xml

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

由:

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

警告将消失