无法实例化默认的tuplizer:HIbernate

时间:2016-07-29 18:44:05

标签: java hibernate pojo composite-key hbmxml

我用复合键编写了示例hibernate appli,如下所示: 当我尝试运行应用程序来创建表时,我的错误。我是hibernate的新手。谁能让我知道我在这里失踪了什么。

Product.hbm.xml:

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

<hibernate-mapping> 
<class name="str.Product" table="products">  

    <composite-id name="id" class="Ptr">
    <key-property name="productId" type="int">
            <column name="productId" />
        </key-property>
        <key-property name="price" type="int">
            <column name="Price" />
        </key-property>

    </composite-id>

    <property name="proName" column="pname" length="10" />      


</class> 

Product.java

package str;

import javax.persistence.Transient;

public class Product implements java.io.Serializable{

    private static final long serialVersionUID = 1L;
    private Ptr id;
    private String proName;

    public void setProName(String proName)
    {
        this.proName = proName;
    }

    public String getProName()
    {
        return proName;
    }

    public Product(Ptr id, String proName) {
        super();
        this.id = id;
        this.proName = proName;
    }

    public Product() {

    }

    public Ptr getId() {
        return id;
    }

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

}

Ptr.java

package str;

public class Ptr {

    private int productId;
    private double price;

    public Ptr(){}

    public Ptr(int productId, double price) {
        super();
        this.productId = productId;
        this.price = price;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }    

}

错误:

INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Exception in thread "main" org.hibernate.HibernateException: Unable to    instantiate default tuplizer [org.hibernate.tuple.component.PojoComponentTuplizer]
at        org.hibernate.tuple.component.ComponentTuplizerFactory.constructTuplizer(ComponentTuplizerFactory.java:101)
at org.hibernate.tuple.component.ComponentTuplizerFactory.constructDefaultTuplizer(ComponentTuplizerFactory.java:122)
    at org.hibernate.tuple.component.ComponentMetamodel.<init>(ComponentMetamodel.java:81)
    at org.hibernate.mapping.Component.getType(Component.java:180)
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:310)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:271)
   at org.hibernate.cfg.Configuration.validate(Configuration.java:1358)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1849)
    at org .hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
    at com.cisco.common.ForOurLogic4Load.main(ForOurLogic4Load.java:14)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
   at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
   at org.hibernate.tuple.component.ComponentTuplizerFactory.constructTuplizer(ComponentTuplizerFactory.java:98)
... 9 more
Caused by: org.hibernate.MappingException: component class not found: Ptr
at org.hibernate.mapping.Component.getComponentClass(Component.java:141)
at org.hibernate.tuple.component.PojoComponentTuplizer.buildGetter(PojoComponentTuplizer.java:155)
at org.hibernate.tuple.component.AbstractComponentTuplizer.<init>(AbstractComponentTuplizer.java:64)
at org.hibernate.tuple.component.PojoComponentTuplizer.<init>(PojoComponentTuplizer.java:59)
... 14 more
Caused by: java.lang.ClassNotFoundException: Ptr
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)`enter code here`
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:191)
at     org.hibernate.internal.util.ReflectHelper.classForName(ReflectHelper.java:193)
at org.hibernate.mapping.Component.getComponentClass(Component.java:138)
... 17 more

1 个答案:

答案 0 :(得分:2)

您没有使用班级的完整路径而且

java.lang.ClassNotFoundException: Ptr

请在您的映射文件中使用:

<composite-id name="id" class="str.Ptr">