-->
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: Stumped By One-To-One and One-To-Many in @NamedNativeQuery
PostPosted: Tue Jun 05, 2007 10:55 am 
Newbie

Joined: Tue Jul 26, 2005 10:25 am
Posts: 12
I have the following class:


Code:
public class Organization {

    private static Log log = LogFactory.getLog(Organization.class);

    private int orgId;
    private String orgName;
    private String orgComp;
    private Date orgActivationDate;
    private Date orgInactivationDate;
    private Location orgLocation;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="ORG_ID")
    public int getOrgId() {
        return orgId;
    }

    public void setOrgId(int orgId) {
        this.orgId = orgId;
    }

    @Column(name="ORG_NAME")
    public String getOrgName() {
        return orgName;
    }

    public void setOrgName(String orgName) {
        this.orgName = orgName;
    }

    @Column(name="ORG_COMP")
    public String getOrgComp() {
        return orgComp;
    }

    public void setOrgComp(String orgComp) {
        this.orgComp = orgComp;
    }

    @Column(name="ACT_DATE")
    public Date getOrgActivationDate() {
        return orgActivationDate;
    }

    public void setOrgActivationDate(Date orgActivationDate) {
        this.orgActivationDate = orgActivationDate;
    }

    @Column(name="INACT_DATE")
    public Date getOrgInactivationDate() {
        return orgInactivationDate;
    }

    public void setOrgInactivationDate(Date orgInactivationDate) {
        this.orgInactivationDate = orgInactivationDate;
    }

    @OneToOne(mappedBy = "locationId", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
    public Location getOrgLocation() {
        return orgLocation;
    }

    public void setOrgLocation(Location orgLocation) {
        this.orgLocation = orgLocation;
    }


I need to populate it via a Native Query. I created the following @SqlResultSetMapping:

Code:
@SqlResultSetMapping(name="explicit", entities={
        @EntityResult(entityClass = Organization.class, fields= {
                @FieldResult(name="orgId", column="ORG_ID"),
                @FieldResult(name="orgName", column="ORG_NAME"),
                @FieldResult(name="orgComp", column="ORG_COMP"),
                @FieldResult(name="orgActivationDate", column="ACT_DATE"),
                @FieldResult(name="orgInactivationDate", column="INACT_DATE")

                }),
        @EntityResult(entityClass = Location.class, fields= {
                @FieldResult(name="locationId", column="LOC_ID"),
                @FieldResult(name="locationName", column="LOC_NAME")
                })

        })


So, I can create a native query to return those fields. The above mapping will create an Organization and Location object. However, since the Location is an attribute of Organization, I don't know how to set the Location to the returned result.

Can anyone help?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 12:00 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
try something like that
Code:
@SqlResultSetMapping(name="explicit", entities={
        @EntityResult(entityClass = Organization.class, fields= {
                @FieldResult(name="orgId", column="ORG_ID"),
                @FieldResult(name="orgName", column="ORG_NAME"),
                @FieldResult(name="orgComp", column="ORG_COMP"),
                @FieldResult(name="orgActivationDate", column="ACT_DATE"),
                @FieldResult(name="orgInactivationDate", column="INACT_DATE")
                @FieldResult(name="localtion.locationId", column="LOC_ID"),
                @FieldResult(name="localtion.locationName", column="LOC_NAME")
                })

        })


Not sure it works though, maybe the unit test suite has some examples

_________________
Emmanuel


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.