A
Arved Sandstrom
When using "Entity Classes from Database" in Netbeans 6.x, and there is a
pre-existing join table, no @ManyToMany join table gets set up; instead the
join table becomes an entity that has two @JoinColumns, each a @ManyToOne
to one of the joined entities. Each of the joined entities has a @OneToMany
Collection of the join table entities.
To make it easier, let's say that one joined entity is Person, the other is
Address, and the join table ends up as PersonAddress. As it stands, in
order to get all the addresses for a given person, you need to get all the
PersonAddress'es (through getPersonAddressCollection in Person), and then
iterate through that and invoke getAddress on each PersonAddress in the
collection.
At this point, in order to conveniently get the addresses for any given
person (say in an app client), you just write up a method that does the
above. Question being, would people normally put this in a session EJB (a
facade), and hence have to call it like
sessionBean.getPersonAddresses(person)
or put it in the Person class and call it like
person.getAddresses()
Either one works just fine as far as the client is concerned. And to be
honest I prefer the second. I'm just wondering what others would normally
do, and what are the pros/cons of each approach? And I'm certainly open to
the suggestion that I am missing something very obvious...
As an aside, in Linq to SQL in .NET, you pretty much end up doing similar
extra coding when you have a join table (although I understand that the
Entity Framework will improve matters). But at least with that there is no
pretense that Linq to SQL is all that evolved yet.
AHS
pre-existing join table, no @ManyToMany join table gets set up; instead the
join table becomes an entity that has two @JoinColumns, each a @ManyToOne
to one of the joined entities. Each of the joined entities has a @OneToMany
Collection of the join table entities.
To make it easier, let's say that one joined entity is Person, the other is
Address, and the join table ends up as PersonAddress. As it stands, in
order to get all the addresses for a given person, you need to get all the
PersonAddress'es (through getPersonAddressCollection in Person), and then
iterate through that and invoke getAddress on each PersonAddress in the
collection.
At this point, in order to conveniently get the addresses for any given
person (say in an app client), you just write up a method that does the
above. Question being, would people normally put this in a session EJB (a
facade), and hence have to call it like
sessionBean.getPersonAddresses(person)
or put it in the Person class and call it like
person.getAddresses()
Either one works just fine as far as the client is concerned. And to be
honest I prefer the second. I'm just wondering what others would normally
do, and what are the pros/cons of each approach? And I'm certainly open to
the suggestion that I am missing something very obvious...
As an aside, in Linq to SQL in .NET, you pretty much end up doing similar
extra coding when you have a join table (although I understand that the
Entity Framework will improve matters). But at least with that there is no
pretense that Linq to SQL is all that evolved yet.
AHS