-->
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.  [ 3 posts ] 
Author Message
 Post subject: Foreign key using hibernate Annotation
PostPosted: Wed Jun 17, 2009 4:33 am 
Newbie

Joined: Tue Jun 16, 2009 8:14 am
Posts: 2
Hi.

I have two tables say Table1 and Table2.
-->Table1. has 1 PK(type_id), 1 unique key(code), and 1 Fk (Parent_id), this FK is referencing the PK in this same table.
-->Table2 has 1 PK(id), and 1 FK(type_id) that is referencing the PK of the "Table1".

For these two tables I have 2 java classes, and I am using Hibernate Annotations in these classes.
-->In class1(corresponding to Table1) , i am giving code like this..
@Entity
@Table(name = "Table1")
public class Class1 implements Serializable{
@Id @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "type_id")
@OneToOne(mappedBy = "type_id")
Integer id;

@Column(name = "parent_id")
@JoinColumn(name = "parent_id",referencedColumnName = "type_id")
Integer parent;

@Column(name = "code",unique = true)
String scanCode;

@OneToMany(cascade = CascadeType.PERSIST)
@JoinColumn(name = "typeId",referencedColumnName = "type_id")
Set<Class2> Class2;
}

Some more fields are also there, but they are independent.

--> In class2(corresponding to Table2) , i am giving code like this..
@Entity
@Table(name = "Table2")
public class Class2 implements Serializable{

@Id @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
Integer id;

@Column(name = "typeId")
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "typeId",referencedColumnName = "type_id")
Integer typeId;
}

For applicationContext.xml my code is like this :
<!-- Hibernate Data Source - settings for db-->

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/Database_name</value>
</property>
<property name="username"><value>root</value></property>
<property name="password"><value></value></property>
</bean>

<!-- Hibernate AnnotationSessionFactory - settings for Annotation oriented classes-->

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

<property name="annotatedClasses">
<list>
<value>com.package1.package2.Class1</value>
<value>com.package1.package2.Class2</value>
</list>
</property>

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!-- Hibernate cache provider(ehcache) - settings for caching -->
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">
ehcache.xml
</prop>

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

<!--Application classes (Spring beans)-->

<!-- net package DAO classes -->
<bean id="class1DAO" class="com.package1.package2.Class1DAO" scope="prototype">
<property name="sessionFactory">
<ref local="annotationSessionFactory"/>
</property>
</bean>

<!-- Interfaces to DWR -->
<!--net package Spring classes along with its javascript object references-->
<bean id="class1Int" class="com.package1.package2.Class1Interface" scope="singleton">
<dwr:remote javascript="Class1"/>
<constructor-arg ref="class1DAO"></constructor-arg>
</bean>

The code for my interface and DAO is like this :

public class Class1Interface {

//Instance variables
private Map<String, Class1> list = new HashMap<String, Class1>();
private Class1DAO class1DAO;

/**
* Parameterized Constructor for Class1Interface called from
* applicationContext.xml
*
* @param class1DAO
*/
Class1Interface(Class1DAO class1DAO) throws Exception{
this.class1DAO = class1DAO;
initClass1();
}

/**
* It is an application level cache of all the Class1 Objects in the
* system
*/
public void initClass1() {

// Call the DAO method to get all the class object defined in the system
// and store it in the List
List<Class1> l = class1DAO.getList();

// Check for the List size
if (l.size() > 0) {
---------------
}
}

DAO class---

public List<Class1> getList() {
try {
return getHibernateTemplate().find("from Class1");
} catch (DataAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
Now when I start the server it , it gives me exception like this :

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'class1Int' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.package1.package2.Class1Interface]: Constructor threw exception; nested exception is org.springframework.orm.hibernate3.HibernateQueryException: Class1 is not mapped [from Class1]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: Class1 is not mapped [from Class1]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:254)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:925)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:835)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4350)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)

Can anyone help me to solve this problem ??


Top
 Profile  
 
 Post subject: Re: Foreign key using hibernate Annotation
PostPosted: Wed Feb 03, 2010 8:13 am 
Newbie

Joined: Wed Feb 03, 2010 8:05 am
Posts: 1
I am also facing the Same problem.


Top
 Profile  
 
 Post subject: Re: Foreign key using hibernate Annotation
PostPosted: Wed Feb 03, 2010 9:31 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Seems that Hibernate Annotations doesn't parse your annotated classes at all.
This may be a classpath problem or anyway a configuration problem.
Is com.package1.package2 the correct package of Class1 ?

Please enable

log4j.logger.org.hibernate.cfg=INFO

in your log4j configuration, then check if you have a line logged like following on SessionFactory startup.

AnnotationBinder:446 - Binding entity from annotated class: com.package1.package2.Class1


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