-->
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: Mapping different roles using the same One to many relation
PostPosted: Sun Dec 04, 2005 5:51 pm 
Newbie

Joined: Sun Oct 30, 2005 9:58 am
Posts: 2
Hi,

I'm currently trying to map this kind of relations:

Computer - 1 -------------- N - InstalledSoftware
Computer - 1 -------------- N - InstallableSoftware
Computer - 1 -------------- N - EquivalentSoftware


In fact such relations are equivalent to the next one:

Computer - 1 -------------- N - software

where sofware is the generic entity for (InstalledSoftware,InstallableSoftware,EquivalentSoftware).
These 3 specifics entities all have the same attributes...

The only way I find to map this relation is to add a discriminator
column in the sofware table:
TYPE=I for Installed
TYPE=A for installable
TYPE=E for Equivalent...
and declaring a OneToMany relation between Computer and software... and finally implementing methods like:

Code:
Collection<Software> getAllinstallableSoftwares() {
List<Software> result = new ArrayList <Software>();

for (Software s:getSoftwares() ){
  if (s.getType.equals("A") ) result.add(s);
  }
return result;
}


I'm not sure this the best solution...
any ideas are welcome ;-)

(I'm interested in hibernate solution too)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 07, 2005 1:08 pm 
Beginner
Beginner

Joined: Sat Sep 17, 2005 10:41 am
Posts: 49
Something like the following should work:

Code:
@Entity (access = AccessType.FIELD)
public class Software
{
  @Id
  private Long id ;

  @Basic
  private String version ;
  // whatever...
}

@Entity (access = AccessType.FIELD)
public class Computer
{
  @Id
  private Long id ;

  @OneToMany (mappedBy = "computer")
  private Set<Software> installedSoftware ;

  @OneToMany (mappedBy = "computer")
  private Set<Software> installableSoftware ;

  @OneToMany (mappedBy = "computer")
  private Set<Software> equivalentSoftware ;

  public Set<Software> getInstalledSoftware () {
    return this.installedSoftware ;
  }

  public void setInstalledSoftware (Set<Software> p) {
    this.installedSoftware = p ;
  }

  // etc...

}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 07, 2005 1:13 pm 
Beginner
Beginner

Joined: Sat Sep 17, 2005 10:41 am
Posts: 49
Hrm... maybe I didn't quite understand your original post...

If you had a superclass Software, which is subclassed by InstalledSoftware, etcetera, then you could do the following:

@Entity
public class Software {
// ...
}

@Entity
public class InstalledSoftware
extends Software
{
// ...
}

@Entity
public class InstallableSoftware
extends Software
{
// ...
}

@Entity
public class EquivalentSoftware
extends Software
{
// ...
}

Then you could just set up three simple OneToMany relations on Computer:

@Entity
public class Computer
{

@OneToMany
private Set<InstalledSoftware> installedSoftware ;

@OneToMany
private Set<InstallableSoftware> installableSoftware ;


// etc...
}


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.