Hey Srini:
I got the solution.
In my facility pojo:
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "FACILITY_CLUB",
joinColumns = [EMAIL PROTECTED](name = "FACILITYID")},
inverseJoinColumns = [EMAIL PROTECTED](name = "CLUBID")}
)
public Set<Club> getClubs()
{
return clubs;
}
in my club pojo:
@ManyToMany(mappedBy="clubs", cascade=CascadeType.PERSIST)
public Set<Facility> getFacilities()
{
return facilities;
}
in my DAO:
public List<Facility> getAll(String query)
{
//Query sqlQuery = getSession().createSQLQuery("select * from Facility
facility WHERE facilityID in (select fc.FACILITYID from facility_club fc
where fc.CLUBID = '1')").addEntity(Facility.class);
Query sqlQuery =
getSession().createSQLQuery(query).addEntity(Facility.class);
List<Facility> facilities = sqlQuery.list();
return facilities;
}
Hope that helps you. Or if there is a better solution, please do let me
know, thanks
Srini Bobbala wrote:
>
> Fan,
> Did you get the solution?
> I am also facing same problem.
> Thanks.
>
> -----Original Message-----
> From: Fan [mailto:[EMAIL PROTECTED]
> Sent: 15 July 2007 05:34 PM
> To: [email protected]
> Subject: [appfuse-user] Hibernate mapping
>
>
> I have one POJO called Facility and another called Club, the
> relationship
> between them is many-to-many.
>
> I want to select all the facilities that belongs to one club, how should
> the
> hibernate mapping look like ?
>
> the following is my Facility POJO:
> ********************************************
> package com.smartclub.model;
>
> import java.util.HashSet;
> import java.util.Set;
>
> import javax.persistence.Column;
> import javax.persistence.Entity;
> import javax.persistence.FetchType;
> import javax.persistence.GeneratedValue;
> import javax.persistence.GenerationType;
> import javax.persistence.Id;
> import javax.persistence.JoinColumn;
> import javax.persistence.JoinTable;
> import javax.persistence.ManyToMany;
>
> import org.apache.commons.lang.builder.ToStringBuilder;
> import org.appfuse.model.BaseObject;
>
> @Entity
> public class Facility extends BaseObject
> {
> private static final long serialVersionUID =
> 6097436406544773648L;
> protected Long facilityID;
> protected String facilityDesc;
> protected Set<Club> clubs = new HashSet<Club>();
>
> @Column(nullable=false, length=50)
> public String getFacilityDesc() {
> return facilityDesc;
> }
> public void setFacilityDesc(String facilityDesc) {
> this.facilityDesc = facilityDesc;
> }
>
> @Id @GeneratedValue(strategy=GenerationType.AUTO)
> public Long getFacilityID() {
> return facilityID;
> }
> public void setFacilityID(Long facilityID) {
> this.facilityID = facilityID;
> }
>
> @ManyToMany(fetch = FetchType.EAGER)
> @JoinTable(
> name = "FACILITY_CLUB",
> joinColumns = [EMAIL PROTECTED](name = "FACILITYID")},
> inverseJoinColumns = [EMAIL PROTECTED](name = "CLUBID")}
> )
> public Set<Club> getClubs()
> {
> return clubs;
> }
> public void setClubs(Set<Club> clubs)
> {
> this.clubs = clubs;
> }
>
> public void addClub(Club club)
> {
> getClubs().add(club);
> }
>
> @Override
> public boolean equals(Object obj) {
> if (this == obj)
> return true;
> if (obj == null)
> return false;
> if (getClass() != obj.getClass())
> return false;
>
> final Facility other = (Facility) obj;
>
> if (facilityDesc == null) {
> if (other.facilityDesc != null)
> return false;
> } else if (!facilityDesc.equals(other.facilityDesc))
> return false;
>
> if (facilityID == null) {
> if (other.facilityID != null)
> return false;
> } else if (!facilityID.equals(other.facilityID))
> return false;
>
> return true;
> }
>
> /**
> * @see java.lang.Object#hashCode()
> */
> public int hashCode() {
> final int PRIME = 31;
> int result = 1;
> result = PRIME * result + ((facilityDesc == null) ? 0 :
> facilityDesc.hashCode());
> result = PRIME * result + ((facilityID == null) ? 0 :
> facilityID.hashCode());
>
> return result;
> }
>
> public String toString() {
> return new ToStringBuilder(this).toString();
> }
>
> }
>
> ***********************************************************
> ***********************************************************
>
> here is the method in my FacilityAction
>
> public String list()
> {
>
> String query = "from Facility";
>
> try
> {
> getUser();
> String clubID = user.getClub().getClubID().toString();
>
> if(clubID != null)
> {
> query += " WHERE facilityID in (select
> FACILITY_CLUB.FACILITYID from
> FACILITY_CLUB where FACILITY_CLUB.CLUBID ='"
> + clubID + "')";
> }
> }
> catch(Throwable e)
> {
>
> }
>
> facilities = facilityManager.getAll(query);
> return SUCCESS;
> }
>
> Eventually, the facilityManager.getAll(query) will call the following
> method
> in FacilityDaoHibernate:
>
> public List<Facility> getAll(String query)
> {
> return getHibernateTemplate().find(query);
> }
>
>
> But, it turned out to be the following error:
>
> Data Access Failure
> FACILITY_CLUB is not mapped [from com.smartclub.model.Facility WHERE
> facilityID in (select FACILITY_CLUB.FACILITYID from FACILITY_CLUB where
> FACILITY_CLUB.CLUBID ='1')]; nested exception is
> org.hibernate.hql.ast.QuerySyntaxException: FACILITY_CLUB is not mapped
> [from com.smartclub.model.Facility WHERE facilityID in (select
> FACILITY_CLUB.FACILITYID from FACILITY_CLUB where FACILITY_CLUB.CLUBID
> ='1')]
>
>
> How should I map it ??
> --
> View this message in context:
> http://www.nabble.com/Hibernate-mapping-tf4082477s2369.html#a11603283
> Sent from the AppFuse - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>
--
View this message in context:
http://www.nabble.com/Hibernate-mapping-tf4082477s2369.html#a11785367
Sent from the AppFuse - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]