编程语言
首页 > 编程语言> > java – object不是声明类-Hibernate的实例

java – object不是声明类-Hibernate的实例

作者:互联网

我是HIbernate的新手,正在练习一对多映射的一些例子,但我不知道它为什么会抛出错误.

1)Employee.java

package com.common.pojo;

import java.util.Set;

public class Employee 
{
private int id;
private String name;
private Set certificate;

public Employee (){}

public Employee(String name) {

    this.name = name;

}
public int getId() {
    return id;
}

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

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Set  getCertificate() {
    return certificate;
}

public void setCertificate(Set  certificate) {
    this.certificate = certificate;
}

}

2)Certificate.java

package com.common.pojo;

public class Certificate 
{
    private int id;
    private String name;

    public Certificate(){}
    public Certificate(String name)
    {
        this.name = name;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

3)Employee.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class table="Employee" name="com.common.pojo.Employee">
        <meta attribute="class-description">
            Employee class
        </meta>
        <id name="id" column="id" type="int">
            <generator class="native"></generator>
        </id>
        <property name="name" column="name" type="string"></property>
        <set name="certificate" cascade="all">
            <key column="id"></key>
            <one-to-many class="com.common.pojo.Certificate" />
        </set>
    </class>


    <class table="Certificate" name="com.common.pojo.Certificate">
        <meta attribute="class-description">
            Certificate class
        </meta>
        <id name="id" column="cer_id" type="int">
            <generator class="native"></generator>
        </id>
        <property name="name" column="cer_name" type="string"></property>

    </class>


</hibernate-mapping> 

4)的hibernate.cfg.xml

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

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dilect">org.hibernate.dialect.HSQLDialect</property>
        <property name="hibernate.driver.connection_class">org.hsqldb.jdbcDriver</property>
        <property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost/</property>
        <property name="hibernate.connection.username">SA</property>
        <property name="hibernate.connection.password"></property>
        <property name="show_sql">true</property>

        <mapping resource="Employee.hbm.xml" />
    </session-factory>
</hibernate-configuration>

5)TestApp.java

package com.common.mainapp;

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

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

import com.common.pojo.Employee;

public class TestApp {

    private static SessionFactory factory;
    @SuppressWarnings("deprecation")
    public static void main(String[] args) {
        try
        {
            factory =new Configuration().configure().buildSessionFactory();
        }
        catch(HibernateException exception)
        {
            exception.printStackTrace();
        }
        TestApp app=new TestApp();
        Set certificate =new HashSet();
        certificate.add("BE");
        certificate.add("MBA");
        certificate.add("BCA");
        app.addEmployee("Amit",certificate);
        Set certificate1 =new HashSet();
        certificate1.add("SCJP");
        certificate1.add("OCJP");
        app.addEmployee("Sunil",certificate1);
    }
    public void addEmployee(String name,Set certificate)
    {
        Transaction tx=null;
        Session session=factory.openSession();
        Integer employeeId =null;
        try
        {
            tx=session.beginTransaction();   
            Employee employee=new Employee(name);
            employee.setCertificate(certificate);
            employeeId = (Integer) session.save(employee);
            tx.commit();    
        }
        catch(HibernateException exception)
        {
            if (tx!=null) tx.rollback();
            exception.printStackTrace(); 
        }
        finally
        {
            session.close();
        }
    }

}

我使用下面的DDL命令创建了表Employee和Certificate

CREATE TABLE CERTIFICATE
(
cer_id int IDENTITY,
cer_name varchar(25),
emp_id int 
)

CREATE TABLE EMPLOYEE
(
id int IDENTITY,
name varchar(25)
)

但是在跑完之后我得到了以下错误.

Hibernate: insert into Employee (id, name) values (default, ?)
apr 14, 2014 6:34:35 EM org.hibernate.property.BasicPropertyAccessor$BasicGetter get
ERROR: HHH000122: IllegalArgumentException in class: com.common.pojo.Certificate, getter method of property: id
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.common.pojo.Certificate.id
    at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:192)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:346)
    at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4740)
    at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4459)
    at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:243)
    at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:511)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:100)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:680)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:672)
    at org.hibernate.engine.spi.CascadingActions$5.cascade(CascadingActions.java:235)
    at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:352)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:295)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161)
    at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:381)
    at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:321)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:298)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161)
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:118)
    at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:460)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:294)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:711)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:703)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:698)
    at com.common.mainapp.TestApp.addEmployee(TestApp.java:74)
    at com.common.mainapp.TestApp.main(TestApp.java:52)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:169)
    ... 32 more

解决方法:

您已将证书映射为证书对象集:

   <set name="certificate" cascade="all">
      <key column="id"></key>
      <one-to-many class="com.common.pojo.Certificate" />
   </set>

但是在你的测试中你将字符串添加到该集合中:

    Set certificate =new HashSet();
    certificate.add("BE");
    certificate.add("MBA");
    certificate.add("BCA");

Java的泛型帮助您在编译时识别此类错误,因此更好地指定您使用的集合的元素类型,因此在添加错误的元素类型时会出现编译错误:

private Set<Certificate> certificate;

标签:java,hibernate,hsqldb
来源: https://codeday.me/bug/20190830/1771374.html