-->
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: Unable to control the table and field name
PostPosted: Fri Feb 24, 2006 10:41 am 
Newbie

Joined: Fri Feb 24, 2006 10:31 am
Posts: 2
Location: Waterloo, Ontario, Canada
Hibernate version:3.1.2

I need to define a one to many unidirectional collection using annotations.
Hibernate creates the collection table by concatenating the main table name with the collection member name and the foreign key in the collection table as the name of the main table.

Is there a way to control these two names with annotations as it is with hbm files (<key name=""> tag)?

Thanks much in advance,
Tony Serban


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 27, 2006 4:47 pm 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
Code:
@OneToMany(mappedBy="<collection_foreign_key_property>")
private Set<CollectionType> collection;


Put the above in your parent entity's class. You'll need to create a child entity class:

Code:
@Entity
@Table(name="<child_table_name>")
public class CollectionType {
  @ManyToOne
  @JoinColumn(name="<foreign_key_column>")
  private ParentType parent;
}


The collection table name will be taken from <child_table_name> in your collection entity. The foreign key column name will be taken from <foreign_key_column> in your collection entity as well. In the above example, <collection_foreign_key_property> would be "parent" (the name of the private member in the CollectionType class).

Additionally, according to Chapter 2. Entity Beans of the Hibernate Annotations Reference Guide (scroll down to 2.2.5.3.2.2),

Code:
A unidirectional one to many using a foreign key column in the owned entity is not that common and not really recommended. We strongly advise you to use a join table for this kind of association (as explained in the next section). This kind of association is described through a @JoinColumn


If you absolutely want it to be unidirectional, you'll need a join table (not just a main table and a collection table), and follow the instructions in 2.2.5.3.2.3 farther down in the document.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 27, 2006 7:03 pm 
Newbie

Joined: Fri Feb 24, 2006 10:31 am
Posts: 2
Location: Waterloo, Ontario, Canada
My problem was exactly the one I presented. Controlling those two names.
I am working with an imposed database schema. Otherwise I would have used bidirectional, tables per class, etc. and life would have been good.

Anyway, I switched to hbm files for now and wait when we'll be able to have the same degree of control with annotations.

Anyway, thank you very much silvaran for your kind assistance!
Cheers,
Tony Serban


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.