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?