akshay
akshay

Reputation: 11

how to write java hibernate many to many relationship retrieving query

I have two tables as specified below. I want to write a query to get all the contacts of a particular group.(According to group id). Please help me. Thanks in advance.

1.this contacts table , which has many has many to many relationship with contact groups table.

@Entity
@Table(name="contacts")
public class Contacts implements Serializable { 
private Long id;
private String userId;
private String emailId;
private Set<ContactGroups> contactGroups;
private String firstName;
private String lastName;

@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}

@Column(name="user_id")
public String getUserId() {
    return userId;
}
public void setUserId(String userId) {
    this.userId = userId;
}

@Column(name="email_id")
public String getEmailId() {
    return emailId;
}
public void setEmailId(String emailId) {
    this.emailId = emailId;
}

@ManyToMany(targetEntity = ContactGroups.class, cascade =          {CascadeType.ALL},fetch=FetchType.EAGER)
@JoinTable(name="contact_group",
    joinColumns=@JoinColumn(name="c_id", referencedColumnName="id"),
    inverseJoinColumns=@JoinColumn(name="g_id", referencedColumnName="id")
)
public Set<ContactGroups> getContactGroups() {
    return contactGroups;
}
public void setContactGroups(Set<ContactGroups> contactGroups) {
    this.contactGroups = contactGroups;
}

@Column(name="first_name")
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
@Column(name="last_name")
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}


 }





 @Entity
 @Table(name="contact_groups")
 public class ContactGroups implements Serializable{
private Long id;
private String groupName;
private String userName;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}

@Column(name="user_name")
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}

@Column(name="group_name")
public String getGroupName() {
    return groupName;
}
public void setGroupName(String groupName) {
    this.groupName = groupName;
}


}

Upvotes: 1

Views: 2597

Answers (2)

JB Nizet
JB Nizet

Reputation: 691755

select c from Contacts c 
inner join c.contactGroups group
where group.id = :groupId

But everything would be simpler if

  • you named your entities ContactGroup and Contact (without the final s)
  • you mapped the association as a bidirectional one. It would allow getting the ContactGroup by ID, and just call getContacts() to get its contacts.

Upvotes: 2

Jean-Philippe Briend
Jean-Philippe Briend

Reputation: 3515

select c from Contacts c, ContactGroups g
where c.contactGroups.id = g.id
    and g.id = 'whatever id you want'

Upvotes: 0

Related Questions