-->
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.  [ 5 posts ] 
Author Message
 Post subject: @Embedded and secondary table
PostPosted: Fri Dec 01, 2006 2:28 pm 
Newbie

Joined: Fri Dec 01, 2006 2:12 pm
Posts: 5
Hibernate version: 3.2.1
Hibernate Annotations: 3.2.0
Hibernate EntityManager: 3.2.0

Hi, everyone. I'm currently working on upgrading an existing Hibnernate 3.0 project to 3.2.1 using annotations, and I'm running into an issue with an embedded object.

In the current (3.0) project, the classes look like this:

Code:
public class Ballot
{
  /* misc private fields */
  private BallotWebInfo webInfo;

  public BallotWebInfo getWebInfo()
  {
    return this.webInfo;
  }

  /* Misc. other getters and setters */
}

public class BallotWebInfo
{
  private String imageUrl;
  private String imageText;
  /* Misc other fields */

  public String getImageUrl()
  {
    return this.imageUrl;
  }

  /*  Misc other getters/setters */
}


The BallotWebInfo object is mapped as a component of the Ballot object, like this:

Code:
<class name="Ballot" table="Ballots">
      <id name="id" column="ballot_id" type="java.lang.Long">
         <generator class="org.hibernate.id.SequenceGenerator">
            <param name="sequence">ballot_id_seq</param>
         </generator>
      </id>

      <join table="ballot_web_infos" optional="true" >
         <key column="ballot_id" unique="true"/>
         <component name="webInfo" class="BallotWebInfo">
            <property name="imageUrl" type="java.lang.String" column="image_url"/>
            <property name="imageCaption" type="java.lang.String" column="image_caption"/>
            <property name="thanksForVotingText" type="java.lang.String" column="thanks_text"/>
         </component>
      </join>


I'm having one heck of a time trying to get this mapping to work the same using annotations, however. All of the examples I can find for @Embedded properties assume you're using the same table to store the component's fields (which would be the more correct way to do this, but I'm stuck with the existing schema), and all the examples I can find for a @OneToOne association using a @PrimaryKeyJoinColumn require the associated object to contain a backreference to be used with the custom generator to assign the associated object's id. I've attempted to use @SecondaryTable on the Ballot object along with @AttributeOverrides on the embedded class to map the embedded class' properties to a different table, but Hibernate complains in a most annoying way. Example:

Using
Code:
@Entity
@Table(name="Ballots")
@SecondaryTable(name="ballot_web_infos")
@SequenceGenerator(name="id_gen", sequenceName="ballot_id_seq", allocationSize = 1)
public class Ballot
   implements Serializable
{

  /* etc. etc... */
   @Embedded
   @AttributeOverrides({
      @AttributeOverride(name = "thanksForVotingText", column=@Column(name="thanks_text", table="ballot_web_infos")),
      @AttributeOverride(name = "imageCaption", column=@Column(name = "image_caption", table="ballot_web_infos")),
      @AttributeOverride(name = "imageUrl", column=@Column(name = "image_url", table="ballot_web_infos"))
   })
   public BallotWebInfo getWebInfo()
   {
      return webInfo;
   }
};


produces the exception: org.hibernate.AssertionFailure: Table apps.ballot_web_infos not found.

Am I missing something obvious, or is this mapping simply not going to work with annotations?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 01, 2006 2:59 pm 
Newbie

Joined: Fri Dec 01, 2006 2:12 pm
Posts: 5
I found my problem. In order to simplify the documents I was posting, I left out the inheritance mappings of Ballot. The full annotations on the class are:

Code:
@Entity
@Table(name="Ballots")
@SecondaryTable(name="ballot_web_infos")
@SequenceGenerator(name="id_gen", sequenceName="ballot_id_seq", allocationSize = 1)
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "ballot_type", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("BASIC")
@NamedQuery(name="BallotById", query="FROM Ballot b WHERE b.id = :ballotId")


This is not a problem, of itself, but once I add a subclass:

Code:
@Entity
@Table(name = "Ballots_Federal")
@DiscriminatorValue("FEDERAL")
public class FederalBallot
   extends Ballot
{
  // etc.
}


Hibernate starts claiming it can't find the ballot_web_infos table. I've attempted adding the @SecondaryTable annotation to the subclass to no avail.

So; progress, but no solution. Any extra ideas would be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 17, 2006 4:37 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I would apreciate a runnable unit test case in JIRa, there might be an issue

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 1:33 pm 
Newbie

Joined: Fri Dec 01, 2006 2:12 pm
Posts: 5
I opened a JIRA ticket for it here, with included JUnit tests.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 4:07 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
thanks

_________________
Emmanuel


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