Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate annotations instead of mapping files

 
Arpit Chaudhary
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to set up a new project using hibernate annotations instead of mapping files. I am running JBoss 4.2.0-GA.

My hibernate-service.xml looks like

| <server>
| <mbean code="org.jboss.hibernate.jmx.Hibernate"
| name="jboss.har:service=HibernateHCE">
| <attribute name="DatasourceName">java:/DefaultDS</attribute>
| <attribute name="Dialect">
| org.hibernate.dialect.OracleDialect
| </attribute>
| <attribute name="SessionFactoryName">
| java:/hibernate/SessionFactory
| </attribute>
| <attribute name="CacheProviderClass">
| org.hibernate.cache.HashtableCacheProvider
| </attribute>
| <attribute name="ShowSqlEnabled">true</attribute>
| </mbean>
| </server>
|

and an example entity looks like

| @Entity
| @Table( name="NETWORKS" )
| @NamedQuery( name="getNetworks", query="from Network" )
| public class Network {
|
| private Long id;
| private String name;
| private Country country;
|
| @Id
| @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="NETWORKS_PK_SEQ")
| @Column(name="PK_NETWORK_ID")
| public Long getId() {
| return id;
| }
| public void setId( Long _id ) {
| id = _id;
| }
|
|
| @ManyToOne( fetch=FetchType.LAZY )
| @Column(name="FK_COUNTRY_ID")
| public Country getCountry() {
| return country;
| }
| void setCountry( Country _country ) {
| country = _country;
| }
|
|
| @Column(name="NETWORK_NAME", unique=true)
| public String getName() {
| return name;
| }
| void setName( String _name ) {
| name = _name;
| }
|
| }
|

I am getting the session factory by
(SessionFactory) new InitialContext().lookup( "java:/hibernate/SessionFactory" );

But it doesn't appear to be seeing the annotated files. Running session.createQuery( "from Network" ).list() gives Network is not mapped [from Network] ???

Am I missing something? I found a reference to using
new AnnotationConfiguration().buildSessionFactory() - but isn't it better to get the factory from the lookup?
 
Scott Selikoff
author
Saloon Keeper
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have a forum dedicated to Hibernate and other object relational mapping tools, I'll move this there.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does the Hibernate configuration object know about your @Entities?

Typically, you need to somehow have to add your annotated entities to the AnnotationConfiguration object, or at least, have Hibernate search and add @Entities that are on the classpath. Here's how I do it manually:



From this Setting Up & Testing Hibernate Tutorial

Do you have this type of code anywhere in your project? It's often put in a HibernateUtil class, or run before binding the AnnotationConfiguration object or the SessionFactory to the JNDI server.

-Cameron McKenzie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic