-->
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 Produces Bad Schema (and Queries & Results)
PostPosted: Sun Jun 07, 2009 2:35 pm 
Newbie

Joined: Mon Aug 11, 2008 1:00 pm
Posts: 2
Here is the relevant part of my JPA-annotated class (it's an item belonging to a category and also having an owner; both of these are many-to-one relationships):

Code:
{
public class Item {
    @Id
    @GeneratedValue
    private long id;
    @NotNull
    private String name;
    @ManyToOne()
    @JoinColumn(name="item_category_id")
    @NotNull
    private ItemCategory itemCategory;
    @ManyToOne()
    @JoinColumn(name="owner_id")
    @NotNull
    private Person owner;
    ...
}


If I use hbm2ddl, I find that a strange extra foreign-key constraint is produced (HSQL):

Code:
alter table Item add index FKACE9553AC59115F (owner_id), add constraint FKACE9553AC59115F foreign key (owner_id) references ItemCategory (id);
alter table Item add index FKACE9553A77E487B4 (owner_id), add constraint FKACE9553A77E487B4 foreign key (owner_id) references Person (id);
alter table Item add index FKACE9553AC16B9B52 (item_category_id), add constraint FKACE9553AC16B9B52 foreign key (item_category_id) references ItemCategory (id);


Woah! Why two foreign keys for owner_id, one referencing table ItemCategory!?

My initial thought was that this is a Hibernate ddl generation defect, so I simply dropped the bad foreign key.

But one of my unit tests were failing to fetch join the right Item for the ItemCategory class, and I believe this is because Hibernate's schema/metadata is confused on how to join the table.

Or perhaps my annotations are incorrect.

Can someone comment? Also to note, I'm using Hibernate 3.2.5.ga.


Top
 Profile  
 
 Post subject: Re: Hibernate Produces Bad Schema (and Queries & Results)
PostPosted: Sun Jun 07, 2009 9:28 pm 
Newbie

Joined: Mon Aug 11, 2008 1:00 pm
Posts: 2
I just resolved this one - my JPA annotations were bad in the ItemCategory class.
Code:
class ItemCategory {
    ...
    @OneToMany(mappedBy="owner")
    private Set<Item> items;
    ...
}


What's bad here is that I say "mappedBy=owner" but it should be "mappedBy=itemCategory".

TAKE HOME: Hibernate (core-3.2.x, annot-3.3.1, entity-mgr-3.3.2) doesn't validate each side of your mappings... it seems like Hibernate could detect this error and fail, or at least log a warning, but it doesn't.

Hopefully this helps someone else...


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.