Hibernate:初始SessionFactory创建failed.org.hibernate.MappingException

时间:2016-10-29 19:05:38

标签: java xml eclipse hibernate mappingexception

我有一个名为db_produts的数据库,我使用配置文件“.hbm.xml”通过hibernate映射eclipse。我设置了配置文件,并创建了HibernateUtil.java类来生成会话。我创建了一个名为HibernateMain.java的类,应该在这个数据库中设置一个表,表“ingredient”并对应“ingredient.java”类。问题是当我开始这个课时给我这个错误日志:

> ott 29, 2016 8:26:30 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
ott 29, 2016 8:26:30 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.5.Final}
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: BankAccount.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: RtFactoryProduct.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: RtDispenserProduct.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: MessageHistory.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: User.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Category.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Factory.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: StaffLogin.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: GeoArea.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: RtIngredientProduct.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: RtFamilyProduct.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: UserLogin.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: DispenserState.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Product.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Capacity.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Producer.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: StaffType.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Family.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Inner.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: RtUserProduct.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Ingredient.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: RtStaffDispenser.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Dispenser.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Staff.hbm.xml
ott 29, 2016 8:26:30 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Hibernate Configuration loaded
Hibernate serviceRegistry created
ott 29, 2016 8:26:30 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
ott 29, 2016 8:26:30 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/db_products]
ott 29, 2016 8:26:30 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
ott 29, 2016 8:26:30 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
ott 29, 2016 8:26:30 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
ott 29, 2016 8:26:31 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
ott 29, 2016 8:26:31 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
ott 29, 2016 8:26:31 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
ott 29, 2016 8:26:31 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Initial SessionFactory creation failed.org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
Exception in thread "main" java.lang.ExceptionInInitializerError
	at it.jack.fdd.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:42)
	at it.jack.fdd.util.HibernateUtil.getSessionFactory(HibernateUtil.java:124)
	at it.jack.fdd.main.HibernateMain.main(HibernateMain.java:19)
Caused by: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
	at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:185)
	at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:135)
	at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:401)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857)
	at it.jack.fdd.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:35)
	... 2 more
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
	at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:138)
	at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:188)
	at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:403)
	at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:520)
	at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:148)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:163)
	... 6 more
Caused by: java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:135)
	... 15 more
Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for fkcategory in class it.jack.fdd.model.Product
	at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:310)
	at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:304)
	at org.hibernate.mapping.Property.getGetter(Property.java:323)
	at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertyGetter(PojoEntityTuplizer.java:411)
	at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:200)
	at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:80)
	... 20 more

HibernateUtil.Class:

package it.jack.fdd.util;

import java.util.Properties;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import it.jack.fdd.model.*;

//import com.journaldev.hibernate.model.Employee1;

public class HibernateUtil {

	//XML based configuration
	private static SessionFactory sessionFactory;
	
	//Annotation based configuration
	private static SessionFactory sessionAnnotationFactory;
	
	//Property based configuration
	private static SessionFactory sessionJavaConfigFactory;

    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);
        	
            return sessionFactory;
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    private static SessionFactory buildSessionAnnotationFactory() {
    	try {
            // Create the SessionFactory from hibernate.cfg.xml
        	Configuration configuration = new Configuration();
        	configuration.configure("hibernate-annotation.cfg.xml");
        	System.out.println("Hibernate Annotation Configuration loaded");
        	
        	ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        	System.out.println("Hibernate Annotation serviceRegistry created");
        	
        	SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        	
            return sessionFactory;
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
	}

    private static SessionFactory buildSessionJavaConfigFactory() {
    	try {
    	Configuration configuration = new Configuration();
		
		//Create Properties, can be read from property files too
		Properties props = new Properties();
		props.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
		props.put("hibernate.connection.url", "jdbc:mysql://localhost/db_products");
		props.put("hibernate.connection.username", "root");
		props.put("hibernate.connection.password", "root");
		props.put("hibernate.current_session_context_class", "thread");
		
		configuration.setProperties(props);
		
		//we can set mapping file or class with annotation
		//addClass(Employee1.class) will look for resource
		// com/journaldev/hibernate/model/Employee1.hbm.xml (not good)
		
		configuration.addAnnotatedClass(BankAccount.class);
		configuration.addAnnotatedClass(Capacity.class);
		configuration.addAnnotatedClass(Category.class);
		configuration.addAnnotatedClass(Dispenser.class);
		configuration.addAnnotatedClass(DispenserState.class);
		configuration.addAnnotatedClass(Factory.class);
		configuration.addAnnotatedClass(Family.class);
		configuration.addAnnotatedClass(GeoArea.class);
		configuration.addAnnotatedClass(Ingredient.class);
		configuration.addAnnotatedClass(Inner.class);
		configuration.addAnnotatedClass(MessageHistory.class);
		configuration.addAnnotatedClass(Producer.class);
		configuration.addAnnotatedClass(Product.class);
		configuration.addAnnotatedClass(RtDispenserProduct.class);
		configuration.addAnnotatedClass(RtFactoryProduct.class);
		configuration.addAnnotatedClass(RtFamilyProduct.class);
		configuration.addAnnotatedClass(RtIngredientProduct.class);
		configuration.addAnnotatedClass(RtStaffDispenser.class);
		configuration.addAnnotatedClass(RtUserProduct.class);
		configuration.addAnnotatedClass(Staff.class);
		configuration.addAnnotatedClass(StaffLogin.class);
		configuration.addAnnotatedClass(StaffType.class);
		configuration.addAnnotatedClass(User.class);
		configuration.addAnnotatedClass(UserLogin.class);
		
		ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
    	System.out.println("Hibernate Java Config serviceRegistry created");
    	
    	SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    	
        return sessionFactory;
    	}
        catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
	}
    
	public static SessionFactory getSessionFactory() {
		if(sessionFactory == null) sessionFactory = buildSessionFactory();
        return sessionFactory;
    }
	
	public static SessionFactory getSessionAnnotationFactory() {
		if(sessionAnnotationFactory == null) sessionAnnotationFactory = buildSessionAnnotationFactory();
        return sessionAnnotationFactory;
    }
	
	public static SessionFactory getSessionJavaConfigFactory() {
		if(sessionJavaConfigFactory == null) sessionJavaConfigFactory = buildSessionJavaConfigFactory();
        return sessionJavaConfigFactory;
    }
	
}

ingredient.java:

package it.jack.fdd.model;
// Generated 29-ott-2016 12.52.16 by Hibernate Tools 4.3.1.Final

/**
 * Ingredient generated by hbm2java
 */
public class Ingredient implements java.io.Serializable {

	private Integer idingredient;
	private String name;

	public Ingredient() {
	}

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

	public Integer getIdingredient() {
		return this.idingredient;
	}

	public void setIdingredient(Integer idingredient) {
		this.idingredient = idingredient;
	}

	public String getName() {
		return this.name;
	}

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

}

Ingredient.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 29-ott-2016 12.52.16 by Hibernate Tools 4.3.1.Final -->
<hibernate-mapping>
    <class name="it.jack.fdd.model.Ingredient" table="ingredient" catalog="db_products" optimistic-lock="version">
        <id name="idingredient" type="java.lang.Integer">
            <column name="idingredient" />
            <generator class="identity" />
        </id>
        <property name="name" type="string">
            <column name="name" length="45" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

HibernateMain.java

package it.jack.fdd.main;



import org.hibernate.Session;


import it.jack.fdd.model.Ingredient;
import it.jack.fdd.util.HibernateUtil;

public class HibernateMain {

	public static void main(String[] args) {
		Ingredient ing = new Ingredient();
		ing.setName("Olio di palma");
		
		
		//Get Session
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		//start transaction
		session.beginTransaction();
		//Save the Model object
		session.save(ing);
		//Commit transaction
		session.getTransaction().commit();
		System.out.println("Ingredient ID="+ing.getIdingredient());
		
		//terminate session factory, otherwise program won't end
		HibernateUtil.getSessionFactory().close();
	}

}

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.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.pool_size">1</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db_products</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.search.autoregister_listeners">false</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.validator.apply_to_ddl">false</property>
        <mapping resource="it/jack/fdd/model/BankAccount.hbm.xml" />
        <mapping resource="it/jack/fdd/model/RtFactoryProduct.hbm.xml" />
        <mapping resource="it/jack/fdd/model/RtDispenserProduct.hbm.xml" />
        <mapping resource="it/jack/fdd/model/MessageHistory.hbm.xml" />
        <mapping resource="it/jack/fdd/model/User.hbm.xml" />
        <mapping resource="it/jack/fdd/model/Category.hbm.xml" />
        <mapping resource="it/jack/fdd/model/Factory.hbm.xml" />
        <mapping resource="it/jack/fdd/model/StaffLogin.hbm.xml" />
        <mapping resource="it/jack/fdd/model/GeoArea.hbm.xml" />
        <mapping resource="it/jack/fdd/model/RtIngredientProduct.hbm.xml" />
        <mapping resource="it/jack/fdd/model/RtFamilyProduct.hbm.xml" />
        <mapping resource="it/jack/fdd/model/UserLogin.hbm.xml" />
        <mapping resource="it/jack/fdd/model/DispenserState.hbm.xml" />
        <mapping resource="it/jack/fdd/model/Product.hbm.xml" />
        <mapping resource="it/jack/fdd/model/Capacity.hbm.xml" />
        <mapping resource="it/jack/fdd/model/Producer.hbm.xml" />
        <mapping resource="it/jack/fdd/model/StaffType.hbm.xml" />
        <mapping resource="it/jack/fdd/model/Family.hbm.xml" />
        <mapping resource="it/jack/fdd/model/Inner.hbm.xml" />
        <mapping resource="it/jack/fdd/model/it/jack/fddispenser/model/RtUserProduct.hbm.xml" />
        <mapping resource="it/jack/fdd/model/Ingredient.hbm.xml" />
        <mapping resource="it/jack/fdd/model/RtStaffDispenser.hbm.xml" />
        <mapping resource="it/jack/fdd/model/Dispenser.hbm.xml" />
        <mapping resource="it/jack/fdd/model/Staff.hbm.xml" />
    </session-factory>
</hibernate-configuration>

有人可以帮我理解这个问题吗?感谢

0 个答案:

没有答案