13.10.2009

Hibernate and NHibernate Configuration : Step 1

Today’s post is about configuring NHibernate and Hibernate.  In Eclipse, i haven’t configured my eclipse environment by hibernate tools so i created a project with Netbeans and then copied all necessary files to eclipse’s workspace.

1. Configure hbm files
Java Verion :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Oct 6, 2009 1:42:21 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.montoya.orm.Customers" schema="SYSTEM" table="CUSTOMERS">
<id name="customerid" type="string">
<column length="5" name="CUSTOMERID"/>
<generator class="assigned"/>
</id>
<property name="companyname" type="string">
<column length="40" name="COMPANYNAME" not-null="true"/>
</property>
<property name="contactname" type="string">
<column length="30" name="CONTACTNAME"/>
</property>
<property name="contacttitle" type="string">
<column length="30" name="CONTACTTITLE"/>
</property>
<property name="address" type="string">
<column length="60" name="ADDRESS"/>
</property>
<property name="city" type="string">
<column length="15" name="CITY"/>
</property>
<property name="region" type="string">
<column length="15" name="REGION"/>
</property>
<property name="postalcode" type="string">
<column length="10" name="POSTALCODE"/>
</property>
<property name="country" type="string">
<column length="15" name="COUNTRY"/>
</property>
<property name="phone" type="string">
<column length="24" name="PHONE"/>
</property>
<property name="fax" type="string">
<column length="24" name="FAX"/>
</property>
<set inverse="true" name="orderses">
<key>
<column length="5" name="CUSTOMERID"/>
</key>
<one-to-many class="com.montoya.orm.Orders"/>
</set>
<set inverse="false" name="customerdemographicses" table="CUSTOMERCUSTOMERDEMO">
<key>
<column length="5" name="CUSTOMERID" not-null="true"/>
</key>
<many-to-many entity-name="com.montoya.orm.Customerdemographics">
<column length="10" name="CUSTOMERTYPEID" not-null="true"/>
</many-to-many>
</set>
</class>
</hibernate-mapping>

.NET Version





<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Montoya.Core" namespace="Montoya.Core">
<class name="Montoya.Core.Customer,Montoya.Core" schema="SYSTEM" table="CUSTOMERS">
<id name="CustomerId" column="CUSTOMERID" type="System.String" unsaved-value="1">
<column length="5" name="CUSTOMERID"/>
<generator class="assigned"/>
</id>
<property name="CompanyName" column="COMPANYNAME" type="System.String" length="40" not-null="true"></property>
<property name="ContactName" column="CONTACTNAME" type="System.String" length="255" not-null="true"></property>
<property name="ContactTitle" column="CONTACTTITLE" type="System.String" length="255" not-null="true"></property>
<property name="Address" column="ADDRESS" type="System.String" length="255" not-null="true"></property>
<property name="City" column="CITY" type="System.String" length="255" not-null="true"></property>
<property name="Region" column="REGION" type="System.String" length="255" not-null="true"></property>
<property name="PostalCode" column="POSTALCODE" type="System.String" length="255" not-null="true"></property>
<property name="Country" column="COUNTRY" type="System.String" length="255" not-null="true"></property>
<property name="Phone" column="PHONE" type="System.String" length="255" not-null="true"></property>
<property name="Fax" column="FAX" type="System.String" length="255" not-null="true"></property>

</class>

</hibernate-mapping>





2. Configure hibernate.cfg.xml

java version



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:testDB</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.connection.password">xxx</property>
<mapping resource="com/montoya/orm/Region.hbm.xml"/>
<mapping resource="com/montoya/orm/Employees.hbm.xml"/>
<mapping resource="com/montoya/orm/Territories.hbm.xml"/>
<mapping resource="com/montoya/orm/Customerdemographics.hbm.xml"/>
<mapping resource="com/montoya/orm/Orderdetails.hbm.xml"/>
<mapping resource="com/montoya/orm/Categories.hbm.xml"/>
<mapping resource="com/montoya/orm/Orders.hbm.xml"/>
<mapping resource="com/montoya/orm/Products.hbm.xml"/>
<mapping resource="com/montoya/orm/Suppliers.hbm.xml"/>
<mapping resource="com/montoya/orm/Shippers.hbm.xml"/>
<mapping resource="com/montoya/orm/Customers.hbm.xml"/>
</session-factory>
</hibernate-configuration>





.Net Version







<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="Montoya.Core.Test">
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="show_sql">true</property>
<property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
<property name="connection.connection_string">Data Source=testDB;User Id=system;Password=xxx;</property>
<property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<mapping assembly="Montoya.Core"/>
</session-factory>
</hibernate-configuration>



3. Configure Hibernate Session





Java Version





public class HibernateUtil {
private static final SessionFactory sessionFactory;

static {
try {

sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

public static Session getSession()
{
return getSessionFactory().openSession();
}
}



.Net Version





NHibernate.Cfg.Configuration cfg = new Configuration();
cfg.Configure();

ISessionFactory sessionFactory = cfg.BuildSessionFactory();
ISession session = sessionFactory.OpenSession();






 

1 yorum:

blueray dedi ki...

merhaba;

aşağıdaki hatadan bir türlü kurtulamadım. sizinde böyle bir probleminiz olmuştur sanırım. hibernate.cfg.xml yapılandırmasına hibernate.cfg.xml yapılandırması tanımlamamı istiyor fakat onu yapsam başka bir hata, diğer başka bir hata ....
şeklinde zincirleme bir hata mesajları verip duruyor.

bu konuda yardımcı olabilir misiniz?

Hata mesajı:

failed: The ProxyFactoryFactory was not configured.
Initialize 'proxyfactory.factory_class' property of the session-factory configuration section with one of the available NHibernate.ByteCode providers.
Example:
NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
Example:
NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
NHibernate.Bytecode.ProxyFactoryFactoryNotConfiguredException
Message: The ProxyFactoryFactory was not configured.
Initialize 'proxyfactory.factory_class' property of the session-factory configuration section with one of the available NHibernate.ByteCode providers.
Example:
NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
Example:
NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle