Hi, 
   Here is a simple example:

(1) EntityA.java:

package queryTest;

import javax.persistence.*;

@SqlResultSetMapping(name="ResultCMapping",
        entitie...@entityresult(entityClass=ResultC.class)}
    )
    
@NamedNativeQuery(
     name="nativefindAandB",
     query="SELECT a.id, a.name, b.value FROM EntityA a, EntityB b WHERE a.id = 
b.id",
     resultSetMapping="ResultCMapping"
    )
    

@Entity
public class EntityA {

    @Id 
    private long id;
    
    @OneToOne
    private EntityB b;
    
    String name;    
    
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
    
    public EntityB getB() {
        return b;
    }

    public void setB(EntityB b) {
        this.b = b;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
=================================
(2) EntityB.java:
package queryTest;

import javax.persistence.*;

@Entity
public class EntityB {
    @Id 
    long id;
    
    String value;
    
    @OneToOne
    EntityA a;
    
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
    
    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
    
    public EntityA getA() {
        return a;
    }

    public void setA(EntityA a) {
        this.a = a;
    }

}
======================================
(3) query:
List<ResultC> list1 =  
    em.createNamedQuery("nativefindAandB").getResultList();
for (ResultC c: list1) {
    System.err.println("value = " + c.getValue());
}


Hope this helps.


--- On Tue, 4/21/09, [email protected] <[email protected]> 
wrote:

> From: [email protected] <[email protected]>
> Subject: NativeQuery resulting in combination of Values
> To: [email protected]
> Date: Tuesday, April 21, 2009, 1:22 AM
> Hello,
> 
> I'm currently trying to build a native query for ORACLE
> that combines 
> several columns of different tables in a complex JOIN/UNION
> way including 
> some ORACLE specific things that do not allow me to
> redesign it for 
> JPA-native querys. However, I'm having trouble to
> understand how to handle 
> this kind of 'virtual' result set. I figured that I have to
> use an 
> @SqlResultSetMapping() somewhere on the resulting object. 
> 
> My problems arise here, which lead to several questions:
> 
> If I design a Result-Object, how do I tell JPA that this
> shall be the 
> represence of a single row of the ResultSet? I can't use
> @Entity nor 
> @Table somehow, cause I always get error messages telling
> me that there is 
> no such table (which is correct)
> What is the correct way of using SqlResultSetMapping for a
> simple result 
> with 12 columns that shall be mapped to 12 simple values in
> the resulting 
> 'Entity'?
> Does anybody have a good (maybe commented) example of such
> a 
> ResultSetMapping?
> Where exactly do you put the SqlResultSetMapping? Do you
> annotate the 
> class that shall hold a row? Do I have to register that
> class in 
> persistence.xml?
> 
> My current solution always stucks with error messages
> like:
> 
> Invalid Index (Oracle Error)
> the Message: Result path "{2}" in result type "{1}" of
> mapping "{0}" 
> attempts to map a field that does not have exactly 1
> column. (I figured it 
> actually can't find a single column)
> Table "{0}" given for "{1}" does not exist. (Happens when I
> try to use 
> @Entity on the resulting type)
> 
> Any help on these questions is gladly appreciated.
> 
> Thanks,
> 
> Heiko
> 
> If you are not the intended addressee, please inform us
> immediately that you have received this e-mail in error, and
> delete it. We thank you for your cooperation.  



Reply via email to