Hibernate problem z konfiguracja pliku

0

Witam staram się rozpocząć "przygodę" z hibernatem i napotykam na problem bo próba konfiguracji kończy się wyjątkiem;/
Mój projekt składa się z 4 plików:
Plik hibernate.cfg.xml:

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

<hibernate-configuration xmlns="http://www.hibernate.org/xsd/hibernate-configuration">
<session-factory name="java:hibernate/SessionFactory">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/store_database
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>

</session-factory>
</hibernate-configuration>

Plik person.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 name="roseindia.Person" table="person">
<id name="id" type="int" column="Id" >
<generator class="assigned"/>
</id>

<property name="name">
<column name="Name" />
</property>

</class>
</hibernate-mapping>
 

Kod klasy uruchamiajacej wprowadzenie rekordu klasy person:


	private static SessionFactory sessionFactory;
	private static ServiceRegistry serviceRegistry;

	public static void main(String[] args)
	{
		Session session = null;
		try 
		{
			try
			{
                                /*Ta linijka rzuca wyjatek*/
				Configuration cfg = new Configuration().addResource(
						"person.hbm.xml").configure();
				serviceRegistry = new ServiceRegistryBuilder().applySettings(
						cfg.getProperties()).buildServiceRegistry();
				sessionFactory = cfg.buildSessionFactory(serviceRegistry);
				
			}
			catch (Throwable ex)
			{
				System.err.println("Failed to create sessionFactory object."+ ex);
				throw new ExceptionInInitializerError(ex);
			}
			session = sessionFactory.openSession();

			Person person = new Person();
			System.out.println("Inserting Record");
			Transaction tx = session.beginTransaction();
			person.setId(1);
			person.setName("Roseindia");
			session.save(person);
			tx.commit();
			System.out.println("Done");
			}
			catch (Exception e) 
			{
				System.out.println(e.getMessage());
			} 
			finally 
			{
				session.close();
			}
	}
}

Wynik działania programu:

sie 26, 2012 7:03:13 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
sie 26, 2012 7:03:13 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.5.Final}
sie 26, 2012 7:03:13 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
sie 26, 2012 7:03:13 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
sie 26, 2012 7:03:13 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: person.hbm.xml
sie 26, 2012 7:03:14 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
sie 26, 2012 7:03:14 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Failed to create sessionFactory object.org.hibernate.MappingException: invalid configuration
Exception in thread "main" java.lang.NullPointerException
	at roseindia.PersonDetail.main(PersonDetail.java:51)

0

Wystarczy przeglądnąć dokumentację, żeby znaleźć dobry przykład na SessionFactory. Najlepiej taką "fabrykę" stworzyć w osobnej klasie, ponieważ będziesz z niej korzystał często w różnych miejscach programu.
http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch01.html#tutorial-firstapp-configuration

0

Wiesz na pewno masz rację że należy to rozbić na jeszcze jedną klasę ale na razie chcę tylko napisać to "HelloWorld" w hibernate.

1 użytkowników online, w tym zalogowanych: 0, gości: 1