Is Proxy Factory necessary in NHibernate? - nhibernate

I've this configuration in the hibernate.cfg.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.connection_string">Data Source=.\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=SSPI;</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
I've just created a Class Library and I've created an integration test using MbUnit. It fails. A part of the report(the one which I think is enough) goes here:
** NO TESTS WERE RUN (No tests found) **
TestCase 'M:IntegrationTests.RepositoryTests.ListAllPostsReturnsAListOfPost'
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.
I have read many tutorials and haven't seen this proxy factory configuration. Is specifying it really necessary? If so, how can I do that? Do I've to reference some other library?

If you're using the latest of NHibernate(2.1), you'll notice that mainline for NH doesn't have a dependency on castle for proxy generation anymore, so all those tutorials you've been looking at are probably out of date.
Basically, you now have a few choices of how you want your dynamic proxies created, so you'll need to explicitly configure which proxy generator you want to use. Examples can be found in this how-to post on forge. A full list of the options is referenced here.
P.S. if you want to keep things simple, just use Castle as the older versions of NHibernate all used it by default.

Related

ActiveMQ connection in Fabric8 using Blueprint instead of DS

In Fabric8, the preferred way to obtain an ActiveMQ connection is via the mq-fabric profile, which provides an ActitveMQConnection object via Declarative Services. An example of this is given on GitHub, which works just fine.
However, I've yet to find a way for Declarative Services and Blueprint Services to collaborate in Fabric8 (or any OSGI-environment, really), thus, my OSGI application must either use DS or blueprint. Mixing both doesn't seem to be an option.
If you want to use blueprint (which I do), you must first create a broker through the web UI, then go back to the console and type cluster-list, finding the port that Fabric8 assigned to the broker and then configure a connection in blueprint like so:
<bean id="activemqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://mydomain:33056" />
<property name="userName" value="admin" />
<property name="password" value="admin" />
</bean>
While this does work, it's not exactly deployment-friendly, as it involves a few manual steps that I'd like to avoid if possible. The main issue is that I don't know what that port is going to be. I've combed through the config files and couldn't find it anywhere.
Is there a cleaner, more automated way to obtain an ActiveMQ connection in Fabric8 via blueprint, or must we use Declarative Services?
Stumbled across a solution to this issue in the fabric-camel-demo, which illustrates how to instantiate an ActiveMQConnectionFactory bean in Fabric8 via Blueprint.
<!-- use the fabric protocol in the brokerURL to connect to the ActiveMQ broker registered as default name -->
<!-- notice we could have used amq as the component name in Camel, and avoid any configuration at all,
as the amq component is provided out of the box when running in fabric -->
<bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="discovery:(fabric:default)"/>
<property name="userName" value="admin"/>
<property name="password" value="admin"/>
</bean>
Hope this helps!

Upgrade VelocityTools 2.0 new tools.xml doesn't load

I'm switching to VelocityTools 2.0 but the new tools.xml doesn't load. I replaced toolbox.xml and now I use tools.xml but when I run the server I get:
"XMLToolboxManager:100: XMLToolboxManager has been deprecated. Please
use org.apache.velocity.tools.ToolboxFactory instead.
ServletToolboxManager:131: ServletToolboxManager has been deprecated.
Please use org.apache.velocity.tools.ToolboxFactory instead."
How can I use the ToolboxFactory? XMLToolboxManager or ServletToolboxManager are not referenced anywhere in my code so I can't simply replace the classes.
When I use toolbox.xml with VelocityTools 2.0 it works just fine, but I need to get the new xml syntax work in tools.xml
My bean definition in velocity.xml looks like this:
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.velocity.VelocityLayoutView</value>
</property>
<property name="contentType">
<value>text/html;charset=UTF-8</value>
</property>
<property name="cache"><value>true</value></property>
<property name="prefix"><value></value></property>
<property name="suffix"><value>.vm</value></property>
<property name="toolboxConfigLocation"><value>/WEB-INF/tools.xml</value></property>
</bean>
I tried removing the property for "toolboxConfigLocation" from the bean and hoped that the default tools.xml will be available, but when I tried to use i.e $math didn't work. What am I doing wrong?
Can someone help please?

nhibernate 3.2 logging sql query

I found a some post about difficulties found on logging sql queries when using NHibernate 3.x
Indeed I'm using NHibernate 3.2 and I can't get the sql logging to work.
I just need the simple logging capabilities that write in the output window when testing or debugging.
It used to work in NH 2.2, but with this new version something is wrong.
As this article says, I simply configured NHibernate with
<property name="show_sql">true</property>
I have this simple code:
using (var session = PersistenceManager.Istance.GetSession()) {
var result = (from agenzia in session.Query<Agenzia>()
select agenzia).ToList();
return result;
}
But it seems that nothing is retrieved from the DB. So I don't know if nothing is logged (I'm debugging under visual studio 2010, so I expect to see something in the output window), or simply nothing is even executed for some strange reason.
There's no exception anywhere, so I'm a little confused
EDITED:
as requested, this is the complete configuration file for NHibernate:
<?xml version="1.0" encoding="utf-8" ?>
<!-- NHibernate Configuration -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="NHibernate.xlns">
<property name="dialect">
NHibernate.Dialect.MsSql2000Dialect
</property>
<property name="connection.driver_class">
NHibernate.Driver.SqlClientDriver
</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
the flag show_sql, AFAIK logs on the STDOUT, so it works perfectly in unit test, or console application, but it does nothing if your app is without stdout ( ie winapp or web app ) in such a case you should enable the log NHibernate.SQL in the log4net config ( or in the alternative logger you possibly use). In a web app you can configure a trace appender in order to see the logged query in the trace window of the debugger.
If you are using log4net, then to enable SQL logging, besides setting <property name="show_sql">true</property>, you need to set the level to INFO or DEBUG (verbose) as follows:
<log4net>
...
<!-- Print only messages of level INFO or above in 'NHibernate' package -->
<logger name="NHibernate">
<level value="INFO" />
</logger>
</log4net>

Glassfish create JDBCResources, -Pools and Security Realms from application

How can I create JDBCResources, -Pools and Security Realms in a Glassfish 3.1 Server from within my Application, if they are not already created? I am writing an application that relies on this resources, however I don't want to configure the server manually every time the application is deployed on a different server.
Doing this with a shell script feels like a workaround.
Glassfish provides a REST interface. You can create a new security (authentication) realm in a certain configuration (say, server-config in a DAS on localhost, admin port 4848) with a POST to:
http://localhost:4848/management/domain/configs/config/server-config/security-service/auth-realm
Do a GET to that resource to see the parameters.
You can use the same interface to create connection pools.
Ok, I found a solution for half of the Question.
I created a file called glassfish-resources.xml in my WEB-INF folder and added the following content to it:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
<jdbc-connection-pool
name="java:app/jdbc/BeerUserPool"
res-type="javax.sql.DataSource"
datasource-classname="org.postgresql.ds.PGSimpleDataSource"
pool-resize-quantity="2"
max-pool-size="32"
steady-pool-size="0"
statement-timeout-in-seconds="30">
<property name="User" value="USERNAME"></property>
<property name="Password" value="PASSWORD"></property>
<property name="PortNumber" value="12345678"></property>
<property name="dataBaseName" value="DATABASE_NAME"></property>
<property name="ServerName" value="yourDBUrl.com"></property>
<property name="Ssl" value="false"></property>
<property name="ProtocolVersion" value="0"></property>
</jdbc-connection-pool>
<jdbc-resource
pool-name="java:app/jdbc/BeerUserPool"
jndi-name="java:app/jdbc/BeerUser"></jdbc-resource>
<
</resources>
Addingt the java:app/ to the names is important, without it it won't work correctly. This connection pool is also only application scoped and gets destroyed after the application is undebloyed (except you add an additional argument).
This pool can now be accessed with JPA with the following persistence.xml.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="jsf-jpa-war" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:app/jdbc/BeerUser</jta-data-source>
<properties>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
</persistence>
However I found no soultion how I can define the security realms in the same way.

How can I use the NHibernate Configuration class with Fluent NHibernate

I want to be flexible even after deploying my code so I like to use the hibernate.cfg.xml file for configuring NHibernate. Now, I am planning to use Fluent NHibernate to do all my Class => Table mapping. Is there a way I could use the old NHibernate Configuration class to configure Fluent NHibernate?
Yes, if you're using the fluent configuration API the Configure method has an overload that takes an existing NHibernate Configuration instance, which can be built from your hibernate.cfg.xml.
Alright, So this was obviously my fault. I tried passing the NHibernate Configurtion object into the Fluently.Configure() method, but my code was throwing up all kinds of errors. The problem was with the version of NHibernate 'Fluent-NHibernate' users. I didn't know that the proxy factory class attribute was now mandatory. So, my hibernate.cfg.xml file was missing that attribute. It's wierd, Fluent NHibernate didn't give me any clue about that. It's when I tried using plain NHibernate that I found this problem. Below are the different version of my hibernate.cfg.xml files. Hope it helps future devs.
First Version
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect,NHibernate</property>
<property name="connection.connection_string">Data Source=.\SQLEXPRESS;Initial Catalog=SchoolPilot;Integrated Security=True</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
Second Version
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect,NHibernate</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<property name="connection.connection_string">Data Source=.\SQLEXPRESS;Initial Catalog=SchoolPilot;Integrated Security=True</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>