-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Problem deploying HAR using Annotated class files
PostPosted: Tue Nov 21, 2006 6:37 pm 
Newbie

Joined: Thu Sep 08, 2005 9:25 pm
Posts: 3
I'm having problems understanding how to deploy a HAR archive on Jboss when my class files use Hibernate Annotations. I'm running Jboss 4.05 with EJB 3.0.

In the past, I would build a HAR archive and deploy it on JBoss. The HAR deployer would find all the *.hbm.xml mapping files and I would be able to refer to the objects at runtime from my MDB's.

Now when I create a HAR archive and use annotations, I have no mapping files. The HAR archive gets deployed, but when my bean attempts to persist the object it throws an exception.

17:16:26,406 ERROR [STDERR] org.hibernate.MappingException: Unknown entity: hibernateTestObjects.MyHibernateObject
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFacto
ryImpl.java:548)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:13
38)
...

My HAR archive deploys with the following jboss-service.xml in the META-INF directory:

<server>
<mbean code="org.jboss.hibernate.jmx.Hibernate"
name="jboss.har:service=MyHibernate">
<attribute name="DatasourceName">
java:/mysqlDataService
</attribute>
<attribute name="Dialect">
org.hibernate.dialect.MySQLDialect
</attribute>
<attribute name="SessionFactoryName">
java:/hibernate/SimpleSessionFactory
</attribute>
<attribute name="ShowSqlEnabled">
false
</attribute>
<attribute name="CacheProviderClass">
org.hibernate.cache.HashtableCacheProvider
</attribute>
<attribute name="Hbm2ddlAuto">
create-drop
</attribute>
</mbean>
</server>

The bean code simply looks up the session , creates and persists an object.

factory = (SessionFactory)ctx.lookup("java:/hibernate/SimpleSessionFactory");
hsession = factory.getCurrentSession();
hsession.beginTransaction();
MyHibernateObject obj = new MyHibernateObject("Some Test Data");
hsession.save(obj);
hsession.getTransaction().commit();


Here is the code for MyHibernateObject :

package hibernateTestObjects;

import javax.persistence.*;

@Entity
@Table(name = "TestData")
public class MyHibernateObject {

@Id @GeneratedValue
@Column(name = "MESSAGE_ID")
private Long id;
@Column(name = "MESSAGE_TEXT")
private String text;


public MyHibernateObject() {
super();
}

public MyHibernateObject(String text) {
super();
this.text = text;
}

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}


}

Of course this will work fine if I create a mapping file, but I'd like to use the annotations method. The HAR archive uses the jboss-service.xml
and I don't see any way of indicating the mapping uses annotations.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 1:34 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you should use a regular ejb 3 jar rather than a har. (ie use persistence.xml even if it delegates to an hibernate.cfg.xml)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 8:51 am 
Newbie

Joined: Mon Nov 27, 2006 8:43 am
Posts: 5
emmanuel wrote:
you should use a regular ejb 3 jar rather than a har. (ie use persistence.xml even if it delegates to an hibernate.cfg.xml)


Hi,
how do I use the regular ejb3 deployer. So far, I have created a regular .har file containing the .hbm.xml files (specified to load in jboss-app.xml, see the following code).

Code:
<jboss-app>
   <module>
      <har>persistencelayer.har</har>
   </module>
</jboss-app>


Is it right to replace only the .har file with a .jar file containing the annotated classes? How do I have to declare it then in jboss-app.xml?

Thanks for your help.

Manuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 12:52 pm 
Newbie

Joined: Thu Sep 08, 2005 9:25 pm
Posts: 3
emmanuel wrote:
you should use a regular ejb 3 jar rather than a har. (ie use persistence.xml even if it delegates to an hibernate.cfg.xml)


That seems to have done the trick. I created a persistence.xml and included it in my meta-inf directory. The file just referenced the data source and the
hibernate config file.

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/ ... ce_1_0.xsd"
version="1.0">
<persistence-unit name="SimpleHibernateTest">
<jta-data-source>java:/mysqlDataService</jta-data-source>
<properties>
<property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml"/>
</properties>
</persistence-unit>
</persistence>

Here is my simple hibernate configuration file.

<!DOCTYPE hibernate-configuration SYSTEM
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory
name="java:/hibernate/SimpleSessionFactory">
<property name="connection.datasource">
java:/mysqlDataService
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- List of annotated classes-->
<mapping class="hibernateTestObjects.MyHibernateObject"/>
</session-factory>
</hibernate-configuration>

I packaged this along with the hibernate.cfg.xml file in a jar and deployed it and it seems to work fine.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 1:51 pm 
Newbie

Joined: Mon Nov 27, 2006 8:43 am
Posts: 5
So the persistence.xml contains only the reference to the hibernate.cfg.xml where you load the annotated classes?

Does Hibernate still run as a JMX Service MBean using this configuration (in fact the style of a mbean is like this: <mbean code="...">...</mbean>, isn't it?)

Regards
Manuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 2:57 pm 
Newbie

Joined: Thu Sep 08, 2005 9:25 pm
Posts: 3
Warlock wrote:
So the persistence.xml contains only the reference to the hibernate.cfg.xml where you load the annotated classes?

Does Hibernate still run as a JMX Service MBean using this configuration (in fact the style of a mbean is like this: <mbean code="...">...</mbean>, isn't it?)

Regards
Manuel


No, it doesn't deploy as a JMX service.

I found that you can deploy it using a jboss-service.xml and then load the hibernate.cfg.xml at runtime. That way you get the JMX managment.

Here is my jboss-service.xml

<server>
<mbean code="org.jboss.hibernate.jmx.Hibernate"
name="jboss.har:service=MyHibernate">
<attribute name="DatasourceName">
java:/mysqlDataService
</attribute>
<attribute name="Dialect">
org.hibernate.dialect.MySQLDialect
</attribute>
<attribute name="SessionFactoryName">
java:/hibernate/SimpleSessionFactory
</attribute>
<attribute name="ShowSqlEnabled">
false
</attribute>
<attribute name="CacheProviderClass">
org.hibernate.cache.HashtableCacheProvider
</attribute>
<attribute name="Hbm2ddlAuto">
create-drop
</attribute>
</mbean>
</server>

I used the same hibernate.cfg.xml mentioned in my earlier post. To
load the hibernate.cfg.xml, I modified my HibernateUtil class to use
an AnnotationConfiguration object. The code below will load the
hibernate.cfg.xml file ( as long as it's on the classpath )

static {
try {
// Create the SessionFactory
AnnotationConfiguration cfg = new AnnotationConfiguration();
sessionFactory = (SessionFactory) cfg.configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}

if ( sessionFactory == null )
throw new ExceptionInInitializerError("Creation of Hibernate SessionFactory using JNDI failed.");
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 28, 2006 2:16 am 
Newbie

Joined: Mon Nov 27, 2006 8:43 am
Posts: 5
What do you prefer?

Is it better to load the annotated classes with a persistence.xml or otherwise use of a jmx service and load them at runtime?

Or should I fall back to classic .hbm.xml mapping files? In this scenario I can use JMX and have no further HibernateUtil class.

A third way is perhaps to use Spring for the configuration of the Sessionfactory. You can define a bean like this:

Code:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
     <property name="annotatedClasses">
          <list>
               <value>model.Class1</value>
          </list>
     </property>
...
</bean>


What do you think?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.