-->
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: ManyToMany Question
PostPosted: Fri Jul 21, 2006 3:19 pm 
Newbie

Joined: Fri Apr 29, 2005 2:35 pm
Posts: 7
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1 and Hibernate Annotation

Mapping documents:

User:

@Entity
@Table(name = "USUARIO")
@javax.persistence.SequenceGenerator(name = "SEQ_USUARIO", sequenceName = "SEQ_USUARIO")
public class User extends BaseModel {

private Integer codigo;

@Id(generate = GeneratorType.SEQUENCE, generator = "SEQ_USUARIO")
@Column(name = "CD_USUARIO")
public Integer getCodigo() {
return codigo;
}

public void setCodigo(Integer codigo) {
this.codigo = codigo;
}

private List<Group> grupos;

@ManyToMany(cascade = { CascadeType.ALL, CascadeType.MERGE }, mappedBy = "usuarios")
public List<Group> getGrupos() {
return grupos;
}

public void setGrupos(List<Group> grupos) {
this.grupos = grupos;
}

}

Group:

@Entity
@Table(name = "GRUPO")
@javax.persistence.SequenceGenerator(name = "SEQ_GRUPO", sequenceName = "SEQ_GRUPO")
public class Group extends BaseModel {

private Integer codigo;

@Id(generate = GeneratorType.SEQUENCE, generator = "SEQ_GRUPO")
@Column(name = "CD_GRUPO")
public Integer getCodigo() {
return codigo;
}

public void setCodigo(Integer codigo) {
this.codigo = codigo;
}

private DSystem sistema;

@ManyToOne
@JoinColumn(name = "CD_SISTEMA")
public DSystem getSistema() {
return sistema;
}

public void setSistema(DSystem sistema) {
this.sistema = sistema;
}

private Collection<User> usuarios;

@ManyToMany(cascade = { CascadeType.ALL, CascadeType.MERGE })
@JoinTable(table = @Table(name = "GRUPO_USUARIO"), joinColumns = { @JoinColumn(name = "CD_GRUPO") }, inverseJoinColumns = { @JoinColumn(name = "CD_USUARIO") })
public Collection<User> getUsuarios() {
return usuarios;
}

public void setUsuarios(Collection<User> usuarios) {
this.usuarios = usuarios;
}

}

System:

@Entity
@Table(name = "SISTEMA")
@javax.persistence.SequenceGenerator(name = "SEQ_SISTEMA", sequenceName = "SEQ_SISTEMA")
public class DSystem extends BaseModel {

private Integer codigo;

@Id(generate = GeneratorType.SEQUENCE, generator = "SEQ_SISTEMA")
@Column(name = "CD_SISTEMA")
public Integer getCodigo() {
return codigo;
}

public void setCodigo(Integer codigo) {
this.codigo = codigo;
}

private String nome;

@Column(name = "NM_SISTEMA")
public String getNome() {
return nome;
}

public void setNome(String nome) {
this.nome = nome;
}

private List<Group> grupos;

@OneToMany(mappedBy = "sistema")
@JoinColumn(name = "CD_SISTEMA")
public List<Group> getGrupos() {
return grupos;
}

public void setGrupos(List<Group> grupos) {
this.grupos = grupos;
}

}



Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:



Hi everyone,

I was writing a hql query and didn't finished because i have a question about manyToMany relationship.
I have 3 entities: User, Group and System.
User have a collection of Groups and Group have a collection of Users (Here is the ManyToMany relationship), and Group have a System (ManyToOne relationship).

So, I want to write a query to retrieve all users that are in some group that is part of one System.

Does anyone know how to help me?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 21, 2006 4:35 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
I find trying to write a query to do 3 tier searches all at one time to be too troublesome.

Find the system. Get the correct group from the system. Load the users for that group.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


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.