-->
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.  [ 2 posts ] 
Author Message
 Post subject: hibernate creates foreign key in the wrong table
PostPosted: Mon Sep 10, 2007 10:16 am 
Beginner
Beginner

Joined: Thu Aug 31, 2006 7:41 am
Posts: 24
Hi,

I'm using Hibernate 3.2.0 with Hibernate Annotation 3.3.0GA.

Suppose I've got the following code

Code:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
    name="mediaclasstype",
    discriminatorType=DiscriminatorType.STRING
)
public abstract class AbstractMedia
{
    @Id
    @GeneratedValue
    private int id;
}

@Entity
@SecondaryTable(name = "media", pkJoinColumns = { @PrimaryKeyJoinColumn(name = "id", referencedColumnName = "id") })
public class Media
{
    @OneToOne( optional=false, cascade = { CascadeType.ALL } )
    @Cascade( { org.hibernate.annotations.CascadeType.ALL } )
    @JoinColumn( table="media", name="id" )
    private Revision revision = new Revision(this);
}

@Entity
public class Revision
{
    @Id
    @GeneratedValue
    private int id;
    @Column
    private int number;
}


The problem is that the foreign key constraint is always created in the abstractmedia table instead of the media table. This poses a problem because there's another subclass of abstractmedia which doesn't have a revision. Also I have to keep all the classes in one table, because I need to store one attribute of abstractmedia in a separate table (using SecondaryTable, which doesn't work with the other inheritance strategies).

Any idea how I can tell Hibernate to create the foreign key on the media table instead of the abstractmedia table?
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 11, 2007 4:53 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
One solution would be to go from a hierarchical model to a delegation model (association)

_________________
Emmanuel


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