12

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.

2 Answers 2

13

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%'
Sign up to request clarification or add additional context in comments.

4 Comments

hello Blake. please help me When 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
I've edited the query, there shouldn't have been any User in the query.
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
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%'
4

Always difficult to write HQL without 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.

7 Comments

Thanks but i can't understand tha last condition v.name like 'foo%'
That's the same kind of like clause that is found in SQL. sql-tutorial.net/SQL-LIKE.asp
just corrected the typo. as Blake says the like clause is just the same as the SQL one
When 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
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.