-->
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.  [ 7 posts ] 
Author Message
 Post subject: ManyToMany childtable hbm2ddl
PostPosted: Sun Nov 09, 2008 1:39 pm 
Newbie

Joined: Wed Jun 25, 2008 12:59 pm
Posts: 9
Location: Belgium
Hibernate version: 3.2.6.ga

Name and version of the database you are using: Oracle 10.2.0.3.0

Hi,

I have 3 annotated entity classes: FieldWidget, GridWidget and GridFieldWidget.
FieldWidget has the fields: id and uid.
GridWidget has the fields: id, name and a collection of GridFieldWidget.
GridFieldWidget has the fields: id, grid (ref to GridWidget), field (ref to FieldWidget) and show.

I use hbm2ddl to generate their sqls.
I have a question about one generated sql.
This is the sql:
Code:
alter table eval_x_grdwdg_fldwdg
  add constraint FK7EDC7B30718ADE14
  foreign key (xgrdwdgfldwdg_fldwdg_id)
  references eval_x_grdwdg_fldwdg;

I known that this sql is generated with this annotation:
Code:
@ManyToMany
@JoinTable(name = "eval_x_grdwdg_fldwdg",
           joinColumns = @JoinColumn(name = "xgrdwdgfldwdg_grdwdg_id"),
           inverseJoinColumns = @JoinColumn(name = "xgrdwdgfldwdg_fldwdg_id")
)
private Collection<GridFieldWidget> fields;

But actually this generated sql should be something like (I think):
Code:
alter table eval_x_grdwdg_fldwdg
  add constraint FK7EDC7B30F2EA5D3A
  foreign key (xgrdwdgfldwdg_fldwdg_id)
  references eval_field_widgets;

Any ideas what I'm doing wrong ?
How can I solved/fix this ?
See below for a more in detail summary of the 3 annotated classes and their generated sqls.


Thanks in advance.
Tim

The FieldWidget entity class:
Code:
@Entity
@Table(name = "eval_field_widgets")
public class FieldWidget{
  @Id
  @Column(name = "fldwdg_id")
  private Long id;

  @Column(name = "fldwdg_uid", unique = true, nullable = false, length = 40)
  private String uid;
}

This gives the following sql generated with the hbm2ddl tool:
Code:
create table eval_field_widgets(
  fldwdg_id number(19,0) not null,
  fldwdg_uid varchar2(40 char) not null unique,
  primary key (fldwdg_id)
);


The GridWidget entity class:
Code:
@Entity
@Table(name = "eval_grid_widgets")
public class GridWidget{
  @Id
  @Column(name = "grdwdg_id")
  private Long id;

  @Column(name = "grdwdg_desc", nullable = false, length = 240)
  private String name;

  @ManyToMany
  @JoinTable(name = "eval_x_grdwdg_fldwdg",
             joinColumns = @JoinColumn(name = "xgrdwdgfldwdg_grdwdg_id"),
             inverseJoinColumns = @JoinColumn(name = "xgrdwdgfldwdg_fldwdg_id")
  )
  private Collection<GridFieldWidget> fields;
}

This gives the following sql generated with the hbm2ddl tool:
Code:
create table eval_grid_widgets(
  grdwdg_id number(19,0) not null,
  grdwdg_desc varchar2(240 char) not null,
  primary key (grdwdg_id)
);


The GridFieldWidget entity class:
Code:
@Entity
@Table(name = "eval_x_grdwdg_fldwdg", uniqueConstraints = @UniqueConstraint(columnNames = {"xgrdwdgfldwdg_grdwdg_id", "xgrdwdgfldwdg_fldwdg_id"}))
public class GridFieldWidget{
  @Id
  @Column(name = "xgrdwdgfldwdg_id")
  private Long id;

  @OneToOne
  @JoinColumn(name = "xgrdwdgfldwdg_grdwdg_id", nullable = false)
  private GridWidget grid;

  @OneToOne
  @JoinColumn(name = "xgrdwdgfldwdg_fldwdg_id", nullable = false)
  private FieldWidget field;

  @Column(name = "xgrdwdgfldwdg_show", nullable = false)
  private boolean show;
}

This gives the following sql generated with the hbm2ddl tool:
Code:
create table eval_x_grdwdg_fldwdg(
  xgrdwdgfldwdg_id number(19,0) not null,
  xgrdwdgfldwdg_grdwdg_id number(19,0) not null,
  xgrdwdgfldwdg_fldwdg_id number(19,0) not null,
  xgrdwdgfldwdg_show number(1,0) not null,
  primary key (xgrdwdgfldwdg_id),
  unique (xgrdwdgfldwdg_grdwdg_id, xgrdwdgfldwdg_fldwdg_id)
);

alter table eval_x_grdwdg_fldwdg
  add constraint FK7EDC7B30718ADE14
  foreign key (xgrdwdgfldwdg_fldwdg_id)
  references eval_x_grdwdg_fldwdg;

alter table eval_x_grdwdg_fldwdg
  add constraint FK7EDC7B30F2EA5D3A
  foreign key (xgrdwdgfldwdg_fldwdg_id)
  references eval_field_widgets;

alter table eval_x_grdwdg_fldwdg
  add constraint FK7EDC7B309430EAF1
  foreign key (xgrdwdgfldwdg_grdwdg_id)
  references eval_grid_widgets;


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 09, 2008 2:01 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
There's an inconsistency with the relationships you've defined. For GridWidget you say ManyToMany GridFieldWidget i.e. one GridWidget can refer to multiple GridFieldWidgets AND one GridFieldWidget can be refered to by multiple GridWidgets. However, in GridFieldWidget you state a OneToOne relationship with GridWidget i.e. each GridWidget has only one GridFieldWidget and that GridFieldWidget is not refered to by any other GridWidget.

No doubt this inconsistency is causing the generated ddl to be a little weird. What's the true relationship between these entities?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 09, 2008 2:55 pm 
Newbie

Joined: Wed Jun 25, 2008 12:59 pm
Posts: 9
Location: Belgium
let me explain with an example
hope this helps ?

for instance the FieldWidget:
all the fields that are available in the database
these fields can be used in multiple grids, a grid being a list of persons or employees ...
id: 1
uid: gender
id:2
uid: firstname

the GridFieldWidget:
all the fields for a specific grid
id: 10
grid: the specific grid so one to one
field: one of the fields above so eg (gender, firstname) so one to one
show: should the field be shown or not

the GridWidget:
the actual grid
id : 100
name: persons
fields: collection of gridfieldwidgets they are mapped with the grid id
so maybe this should be a onetomany
one gridwidget can have many gridfieldwidgets = one grid can have one or more fields to show or not to show


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 3:07 am 
Newbie

Joined: Wed Jun 25, 2008 12:59 pm
Posts: 9
Location: Belgium
actually when I (re)think about it

why do I need a collection of GridFieldWidget in the GridWidget entity?
I even won't use it!

my main sql will be something like
from GridFieldWidget where grid.uid = 'persons'
this will give me all the fields of the grid person, this is what I need

removed the collection and of course now the extra foreign contraint is gone :))


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 4:08 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
grid: the specific grid so one to one
field: one of the fields above so eg (gender, firstname) so one to one


Hmm... it doesn't sound correct to me. From our description it seems like you want to use the FieldWidget and GridWidget entries for more than one GridFieldWidget entry, and if you want to do that you should map with @ManyToOne.

The collection that you removed should then be mapped with @OneToMany(mappedBy="grid") in case you want it back.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 4:52 am 
Newbie

Joined: Wed Jun 25, 2008 12:59 pm
Posts: 9
Location: Belgium
ok will try that


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 5:16 am 
Newbie

Joined: Wed Jun 25, 2008 12:59 pm
Posts: 9
Location: Belgium
Quote:
Hmm... it doesn't sound correct to me. From our description it seems like you want to use the FieldWidget and GridWidget entries for more than one GridFieldWidget entry, and if you want to do that you should map with @ManyToOne.

correct
changed both grid and field widget to @ManyToOne
:))


Quote:
The collection that you removed should then be mapped with @OneToMany(mappedBy="grid") in case you want it back.

hmm never heard of mappedBy
but it is exactly what i need :))

thanks a lot

next time I better take a deeper look into the Annotations Reference Guide
below is a sample of the guide that could have helped me!
Code:
@Entity public class City {
    @OneToMany(mappedBy="city")
    @OrderBy("streetName")
    public List<Street> getStreets() {
        return streets;
    }
...
}

@Entity public class Street {
    public String getStreetName() {
        return streetName;
    }

    @ManyToOne
    public City getCity() {
        return city;
    }
    ...
}


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