-->
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: Bidirectional @OneToOne mapping fails
PostPosted: Tue Jun 19, 2007 6:11 pm 
Newbie

Joined: Thu Mar 16, 2006 7:03 pm
Posts: 3
Hello all:

I'm in the process of migrating my project from XML mapping files to annotations and I've hit a snag. My attempt to map the Stream and Prices objects together results in the exception below. Excerpts from the XML mapping files that I am attempting to replicate as well as the annotations as I have them currently defined are included below. I've also attempted to define the targetEntity for the OneToOne relationship with no luck. Any insight into the problem is appreciated.

Thanks in advance.

Carl

Hibernate version: Hibernate 3.2.2, Hibernate Annotations 3.3

Mapping XML:

Prices.hbm.xml:
Code:
   <class name="Prices" table="stream_prices">
      <id name="id" type="long" column="stream_id">
         <generator class="foreign">
            <param name="property">stream</param>
         </generator>
      </id>
      <one-to-one name="stream" class="Stream" constrained="true" />
      ...
   </class>
   


Stream.hbm.xml:
Code:
   <class name="Stream" table="streams">
      <id name="id" type="long" column="id">
         <generator class="native"/>
      </id>
      ...   
      <one-to-one name="prices" class="Prices" />
      ...
   </class>
   



Annotations:

Prices.java:
Code:
      @Id
      @GeneratedValue(generator="fk")
      @GenericGenerator(
         name="fk",
         strategy="foreign",
         parameters={
            @Parameter(name="property", value="stream")
         }
      )
      @Column(name="stream_id", nullable=false)
      private Long id;
      
      @OneToOne(mappedBy="prices")
      private Stream stream;
   


Stream.java:
Code:
      @Id @GeneratedValue
      @Column(name="id", nullable=false)
      private Long id;
      
      @OneToOne(cascade = CascadeType.ALL)
      @PrimaryKeyJoinColumn
      private Prices prices;
   



Full stack trace of any exception that occurs:
Code:
   org.hibernate.AnnotationException: Unknown mappedBy in: entity.Prices.stream, referenced property unknown: entity.Stream.prices
   at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:127)
   at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1049)
   at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:302)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1205)
   at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:805)
   at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:745)
   at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:134)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1175)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1145)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:427)
   at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:144)
   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:276)
   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:360)
   at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:241)
   at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:184)
   at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)
   at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3729)
   at org.apache.catalina.core.StandardContext.start(StandardContext.java:4187)
   at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:759)
   at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:739)
   at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:524)
   at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:608)
   at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:535)
   at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:470)
   at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1122)
   at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:310)
   at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
   at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1021)
   at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
   at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1013)
   at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442)
   at org.apache.catalina.core.StandardService.start(StandardService.java:450)
   at org.apache.catalina.core.StandardServer.start(StandardServer.java:709)
   at org.apache.catalina.startup.Catalina.start(Catalina.java:551)
   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:585)
   at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:294)
   at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:432)
   


Name and version of the database you are using: MySQL 5.0.22


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 20, 2007 12:29 pm 
Newbie

Joined: Thu Mar 16, 2006 7:03 pm
Posts: 3
In continuing to work on the issue, I've removed the @OneToOne(mappedBy="prices") annotation from Stream.java. While the schema is now generated and no exceptions are thrown, 2 interesting things have occurred. The prices table is created correctly with the PK using the stream id, however the foreign key constraint is not created. Also there is now a TINYBLOB column for the stream entry in the prices table because I do not have it annotated. If I annotate the stream field with @Transient, the TINYBLOB column is not created anymore. Here are the annotations as they are currently defined:

Stream.java:
Code:
    @OneToOne(cascade = CascadeType.ALL, optional=false)
    @PrimaryKeyJoinColumn
    private Prices prices;


Prices.java
Code:
    @Id
    @GeneratedValue(generator="fk")
    @GenericGenerator(
        name="fk",
        strategy="foreign",
        parameters={
            @Parameter(name="property", value="stream")
        }
    )
    @Column(name="stream_id", nullable=false, unique=true)
    private Long id;

    @Transient
    private Stream stream;


In referencing THE book, chapter 7 deals with the OneToOne mapping, however they do not discuss the correct way to annotate the field to enable the bidirectional relationship. How should I be annotating the stream field so that the relationship is truly bidirectional? And why is the foreign key constraint not being created?

TIA,
Carl


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 4:17 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

From my unnder standing of chapter 7, a bidirectional relationship is based on FK and not shared PKs (or at least it does not document how to do it with shared pks). If it is not an issue to have a fk based relationship do the following:

in prices.java
Code:
@Id
@GeneratedValue
Long id;

@OneToOne(mappedBy="prices")
private Stream stream;


and in Stream.java
Code:
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "<name for column>")
private Prices prices;


Cheers,

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 6:02 pm 
Newbie

Joined: Thu Mar 16, 2006 7:03 pm
Posts: 3
Thanks for your response Andy. If you take a look at the mapping xml file snippets in my first post, you'll note that the table structure created should resemble something like the diagram found in figure 7.2 of chapter 7 and the example below.

Code:
+---------------+
|  <<Table>>    |
|   streams     |
+---------------+
|  id <<PK>>    |
+---------------+


Code:
+---------------------------+
|         <<Table>>         |
|     stream_prices         |
+---------------------------+
|  stream_id <<PK>> <<FK>>  |
+---------------------------+


And it is exactly this sort of thing that I am attempting to do with annotations. I can reproduce the table structure, however the foreign key constraint is not created by Hibernate at all while using annotations. I would have assumed that defining the GenericGenerator using the foreign strategy would take care of this. Unfortunately this is not the case. Can this actually be accomplished using annotations? Given that I am migrating an existing project to annotations, I'd prefer not to change the table structure.

Thanks.
Carl


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.