-->
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.  [ 4 posts ] 
Author Message
 Post subject: hibernate annotation with spring
PostPosted: Tue Feb 13, 2007 12:25 am 
Newbie

Joined: Tue Feb 13, 2007 12:17 am
Posts: 5
i am new to hibernate annotation

am trying to use hibernate annotaion with spring

flollowing is my code

the problem is that table is not geting created

----------------------------
Employee(pojo)
--------------------------------
package org.employee.model;

import java.io.*;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import org.hibernate.annotations.AccessType;
import javax.persistence.Entity;
@Entity
@Table(name="annotation_test")
public class Employee {

private long id;
private String nam;
private int age;
@Id
@Column(name ="id")
@GeneratedValue(strategy=GenerationType.AUTO, generator = "ID_SEQUENCE")
@SequenceGenerator(name="ID_SEQUENCE", sequenceName = "id_sequence")
public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

@Column(name = "nam", nullable = false, length=50)
public String getNam() {
return nam;
}

public void setNam(String nam) {
this.nam = nam;
}
@Column(name="age")
public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}


}



--------------------------
employee-servlet.xml
---------------------------

<!--============================ Hibernate SessionFactory configuration===================================-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="configLocation">
<value>WEB-INF/hibernate.cfg.xml</value></property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>

</props>
</property>
</bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://...."/>
<property name="user" value="xuser"/>
<property name="password" value="xuser"/>
</bean>



----------------------
hibernate.cfg.xml
-----------------------------
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<mapping class="org.employee.model.Employee"/>
</session-factory>
</hibernate-configuration>


Last edited by arbiter22 on Wed Feb 14, 2007 4:58 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: hibernate annotation with spring
PostPosted: Tue Feb 13, 2007 3:51 am 
Newbie

Joined: Mon Jan 15, 2007 9:51 am
Posts: 17
arbiter22 wrote:
i am new to hibernate annotation

am trying to use hibernate annotaion with spring

flollowing is my code

the problem is that table is not geting created


I'd recommend using the AnnotationSessionFactoryBean instead, which is an extension of LocalSessionFactoryBean. If all you keep in hibernate.cfg.xml is your mappings you can thereby eliminate this (though I'd suggest you put your datasource parameters here).

Using AnnotationSessionFactoryBean is you give it a list of annotated classes:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>

<property name="annotatedClasses">
<list>
<value>org.employee.model.Employee</value>
</list>
</property>

<property name="configLocation">
<value>WEB-INF/hibernate.cfg.xml</value></property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
</props>
</property>
</bean>

Also make sure you use Hibernate 3.2 or newer, and that you use the @Entity annotation from javax.persistence.*, not Hibernate's.
Hope that helps!

See also http://www.onjava.com/pub/a/onjava/2007 ... tions.html for a gentle introduction to Hibernate annotations.

And btw, this should probably been posted on the Spring forums. :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 13, 2007 3:54 am 
Newbie

Joined: Mon Jan 15, 2007 9:51 am
Posts: 17
Oh, and you need hibernate.hbm2ddl.auto=create-drop or something similar (see docs) if you expect the tables to be generated automatically...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 14, 2007 5:10 am 
Newbie

Joined: Tue Feb 13, 2007 12:17 am
Posts: 5
thanks for ur reply

i would like to asku one more thing
i have written the pojo's inside hibernate.cfg.xml
--------hibernate.cfg.xml---------
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<mapping class="org.employee.model.Employee"/>
<mapping class="org.employee.model.Test"/>
</session-factory>
</hibernate-configuration>
--------------------------------------
inside application-servlet.xml

-----------------------------------
<property name="configLocation">
<value>WEB-INF/hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
--------------------------

i have changed the previous code acoording to ur reply
but i would like to work the application with hibernate.cfg.xml
is the above code enough ????
nd one more thing
if we have to take all the classes from a particular package how could we specify that
i tried
"annotatedPackages" tag instead of annotatedClass
but itzzznot working


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.