tags:

views:

2882

answers:

2

I have created two beans User and VirtualDomain with many to many relationship

@Entity
@Table(name = "tblUser")
public class User implements Serializable {
    private Long id;
    private String username;
    private Set<VirtualDomain> virtualdomainset;
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
 return id;
}

public void setId(Long id) {
 this.id = id;
}

@Column(name = "username", length = 50, nullable = false)
public String getUsername() {
 return username;
}

public void setUsername(String username) {
 this.username = username;
}
   @ManyToMany(targetEntity = VirtualDomain.class, cascade = {CascadeType.PERSIST},fetch=FetchType.EAGER)
    @JoinTable(name = "tblUserDomainRel", joinColumns = @JoinColumn(name = "userid"), inverseJoinColumns = @JoinColumn(name = "domainid"))
    public Set<VirtualDomain> getVirtualdomainset() {
     return virtualdomainset;
    }

    public void setVirtualdomainset(Set<VirtualDomain> virtualdomainset) {
     this.virtualdomainset = virtualdomainset;
    }

}

@Entity
@Table(name = "tblVirtualDomain")
public class VirtualDomain  {
    private Long id;
    private String domainname;
    private Set<User> userset;
@Id
@JoinColumn(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
 return id;
}

public void setId(Long id) {
 this.id = id;
}

@Column(name = "domain_name")
public String getDomainname() {
 return domainname;
}

public void setDomainname(String domainname) {
 this.domainname = domainname;
}
@ManyToMany(cascade = {CascadeType.ALL},fetch=FetchType.EAGER, mappedBy = "virtualdomainset", targetEntity = User.class)

public Set<User> getUserset() {
 return userset;
}

public void setUserset(Set<User> userset) {
 this.userset = userset;
}
}

how to get data of user like username related to particular domain through hibernate.

+1  A: 

Always differcult todo HQL with out a test system...but here we go:

select u from VirtualDomain vd join User vd.usersset u 
       where vd.domainname = 'example.com' and u.username like 'foo%'

Let me know how you get on.

One tip I often did prior to buying Intellji was to stop the app in the debugger and then use the immediate window to experiment with HQL.

The hibernate documentation on joins has always been a bit cryptic in my opinion.

Gareth Davis
Thanks but i can't understand tha last condition v.name like 'foo%'
Jugal
That's the same kind of like clause that is found in SQL.http://www.sql-tutorial.net/SQL-LIKE.asp
Blake Pettersson
just corrected the typo. as Blake says the like clause is just the same as the SQL one
Gareth Davis
When I used this query it is giving me the errorjava.lang.NoSuchMethodError: org.hibernate.hql.antlr.HqlBaseParser.recover(Lantlr/RecognitionException;Lantlr/collections/impl/BitSet;)V
Jugal
that'll be an issue with the libraries that you have included in your application. I'm guessing that antlr.jar is the wrong version
Gareth Davis
i have included antlr-2.7.2
Jugal
I'm not going be able to help you much on this one. Other than to say , download the same version of hibernate again and manually check that you have exactly the same jars in your application as hibernate recommends.
Gareth Davis
any luck on that NoSuchMethod?
Gareth Davis
Problem is solved with Your and Blake help. Thank You
Jugal
+2  A: 

To add to gid's answer, if for some reason you need to eagerly fetch an entites relations, then the join syntax would be join fetch.

from VirtualDomain vd join fetch vd.usersset u 
   where vd.domainname = 'example.com' and u.username like 'foo%'
Blake Pettersson
hello Blake. please help meWhen I used this query it is giving me the error java.lang.NoSuchMethodError: org.hibernate.hql.antlr.HqlBaseParser.recover(Lantlr/RecognitionException;Lantlr/collections/impl/BitSet;)V
Jugal
I've edited the query, there shouldn't have been any User in the query.
Blake Pettersson
your edited query looks ok but I want only the username field from User so when i add select clause like select u.username it gives me error org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list
Jugal
How about when reversing the query? select u.username from User u join u.virtualdomainset vd where vd.domainname = "example.com" and v.name like 'foo%'
Blake Pettersson
At last its working Thank You very much Blake
Jugal