使用eclipse的hibernate自定义生成器类应用程序

时间:2014-05-20 05:50:37

标签: hibernate

这是我的应用程序代码,我的目的是通过我自己的生成器类为对象生成id,并且我希望将该对象存储到具有该id的数据库中。

这是我的域类:

package com.suresh.model;


public class Customer 
{
    private String customerID; 
    private String customerName;
    private String place;
    public String getCustomerID() 
    {
        return customerID;
    }
    public void setCustomerID(String customerID) 
    {
        this.customerID = customerID;
    }
    public String getCustomerName()
    {
        return customerName;
    }
    public void setCustomerName(String customerName) 
    {
        this.customerName = customerName;
    }
    public String getPlace() 
    {
        return place;
    }
    public void setPlace(String place)
    {
        this.place = place;
    }


}

这是我的实用程序类:

package com.suresh.util;


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

public class HibernateUtils 
{
    public static SessionFactory factory;
    private HibernateUtils()
    {

    }
    public static SessionFactory getSession()
    {
        if(factory==null)
        {
            factory=new Configuration().configure("com/suresh/config/hibernate.cfg.xml").buildSessionFactory();

        }
        return factory;
    }
}

这是我的DAO界面:

package com.suresh.dao;


public interface TestDAO 
{
    public void insertCustomer(String custName,String address);
}

这是我的DAO实现类:

package com.suresh.dao;


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

import com.suresh.model.Customer;
import com.suresh.util.HibernateUtils;

public class TestDAOImpl implements TestDAO {

    @Override
    public void insertCustomer(String customerName, String address)
    {
        Customer c1=new Customer();
        c1.setCustomerName(customerName);
        c1.setPlace(address);
        SessionFactory factory=HibernateUtils.getSession();

        Session session=factory.openSession();
        Transaction tx=session.beginTransaction();
        session.save(c1);
        tx.commit();
        session.close();
        factory.close();

    }

}

这是我的DAO工厂类,它返回DAO类的对象:

package com.suresh.dao;


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

import com.suresh.model.Customer;
import com.suresh.util.HibernateUtils;

public class TestDAOImpl implements TestDAO {

    @Override
    public void insertCustomer(String customerName, String address)
    {
        Customer c1=new Customer();
        c1.setCustomerName(customerName);
        c1.setPlace(address);
        SessionFactory factory=HibernateUtils.getSession();

        Session session=factory.openSession();
        Transaction tx=session.beginTransaction();
        session.save(c1);
        tx.commit();
        session.close();
        factory.close();

    }

}

这是我的自定义生成器类:

package com.suresh.dao;


import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.id.IdentifierGenerator;

public class MyIdGenerator implements IdentifierGenerator 
{


    @Override
    public Serializable generate(SessionImplementor arg0, Object arg1)
            throws HibernateException 
            {
                String id=null;
                try
                {
                    Connection con=arg0.connection();
                    Statement stmt=con.createStatement();
                    ResultSet rs=stmt.executeQuery("select new_sequence.nextval as next from dual");
                    rs.next();
                    int i=rs.getInt("next");
                    if(i<=9)
                        id="c00"+i;
                        else if(i>9 && i<=99)
                            id="c0"+i;
                        else
                            id="c"+i;
                    rs.close();
                    stmt.close();
                    con.close();
                }
                catch(Exception e)
                {
                    System.out.println(e);
                }
                return id;
            }
}

这是我的映射文件:

<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 <hibernate-mapping>
 <class name="com.suresh.model.Customer" table="customer">
 <id name="customerId" column="custid">
 <generator class="MyIdGenerator"/>
 </id>
 <property name="customerName" column="custname"></property>
 <property name="place" column="place"></property>
 </class>
 </hibernate-mapping>

这是我的配置文件:

    <!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>

<!-- connection properties -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="connection.username">system</property>
<property name="connection.password">system</property>

<!-- hibernate properties -->
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.hbm2ddl.auto"></property>
<property name="hibernate.show_sql">true</property>

<!-- mapping resources -->
<mapping resource="com/suresh/config/customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>

这是我的主要课程:

import com.suresh.dao.TestDAO;
import com.suresh.dao.TestDAOFactory;



public class Main 
{

    public static void main(String[] args)
    {
        TestDAO dao=TestDAOFactory.getInstance();
        for(int i=0;i<=10;i++)
        {
            dao.insertCustomer("suresh","ponnur");
            System.out.println("record inserted successfully");
        }

    }

}

当我尝试运行我的主类时,我得到以下异常:

                Exception in thread "main" org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/suresh/config/customer.hbm.xml
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:4009)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueue(Configuration.java:3998)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3986)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1398)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1856)
    at com.suresh.util.HibernateUtils.getSession(HibernateUtils.java:18)
    at com.suresh.dao.TestDAOImpl.insertCustomer(TestDAOImpl.java:19)
    at Main.main(Main.java:14)
Caused by: org.hibernate.PropertyNotFoundException: field [customerId] not found on com.suresh.model.Customer
    at org.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:182)
    at org.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:174)
    at org.hibernate.property.DirectPropertyAccessor.getGetter(DirectPropertyAccessor.java:197)
    at org.hibernate.util.ReflectHelper.getter(ReflectHelper.java:241)
    at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:229)
    at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:316)
    at org.hibernate.cfg.HbmBinder.bindSimpleId(HbmBinder.java:454)
    at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:387)
    at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:326)
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:177)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:4006)
    ... 7 more
===================================================

任何人都可以解决我的问题,我也添加了所有必需的jar文件,但我仍然面临问题

1 个答案:

答案 0 :(得分:0)

例外Caused by: org.hibernate.PropertyNotFoundException: field [customerId] not found on com.suresh.model.Customer。您的问题很简单,就是在hbm.xml中使用小写d引用customerId,但是您的Java类有一个名为customerID的字段,大写为D.