-->
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: Possibility to declare foreign keys for properties
PostPosted: Sun Nov 16, 2008 10:56 am 
Newbie

Joined: Wed Oct 08, 2008 1:54 am
Posts: 14
Hi,

I'm using hibernate 3.3.1.GA with annotation 3.4.0.GA to access a local mysql database. Currently I also use hibernate to create/update my database during program startup by setting the "hibernate.hbm2ddl.auto" property to "update". But unfortunately I have some objects which use only the Integer or Long primary key to reference an associated object - e.g.
Code:
class A {
    @Id
    private int id;

}

class B {
    @Id
    private int id;

    @Column(name = "a_id");
    private int myAId;
}


Unfortunately I'm unable to create forein key constraints in the database this way since a possible @ForeignKey annotation is ignored.
However I'm able to let hibernate create the foreign key constraint if I change the annotations like follows:

Code:
class B {
    @Id
    private int id;

    @ManyToOne(optional=true, targetEntity=A.class)
    @ForeignKey(name="FK_B_A_ID")
    @JoinColumn(name = "a_id")
    private int myAId;
}


However this causes write and/or read accesses to the B table to fail with a "java.lang.IllegalArgumentException: object is not an instance of declaring class". Hibernate seems to try to the set id property of the A class on a primitive of the type int.

So I wonder if there is any way to cause hibernate to create foreign key constraints without having to use full Object references in the object model. (To have B reference a lazy loaded instance of A is not an option because the object is serialized and sent to other machines).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 18, 2008 9:02 am 
Newbie

Joined: Wed Oct 08, 2008 1:54 am
Posts: 14
I found out that adding a lazy loaded Set to class A will enable me to add a @JoinColumn(name = "a_id") annotation to this set which will result in the foreign key to be creted in the table of b - but this is kind of clumsy and smells like a Hack since this collection is never needed/read and further I have to add an annotation to class A to modify the table of class B - not really obvious.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2008 12:32 pm 
Newbie

Joined: Wed Oct 08, 2008 1:54 am
Posts: 14
Just for the case someone should be interested: I solved the problem in the end by adding an own "ForeignKey" annotation that can be added to any column/field. To apply it I overloaded the AnnotationConfiguration used - all I had to do is overloading the secondPassCompile() method and parse all table classes for my annotation after the super method was invoked.
I simply invoked the Table.createForeignKey() method for all annotated columns.

Perhaps such an annotation could be usefull in the hibernate-annotation package as well? Of course one would have to check more closely if the key would be valid than I did it to solve my problem.


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.