-->
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.  [ 3 posts ] 
Author Message
 Post subject: One Entity, Multiple Tables?
PostPosted: Thu Jul 12, 2007 1:10 pm 
Newbie

Joined: Thu Jul 12, 2007 12:52 pm
Posts: 5
Location: Boise, Idaho USA
Hello,
I am new to Hibernate and am trying to convert a system using Oracle ORM framework to Hibernate. One issue I am currently having is I would like to map an Entity or POJO to multiple tables (or better yet some sort of custom query).

I'm using annotations so what I have so far is akin to...
Code:
@Entity
@Table(name = "DEPT_ACTIVITY")
public class DeptActivity
{
    private Long activityOid;
    private String definitionTx;
    private Boolean uosFl = false;
    private Boolean universalFl = false;
    private Boolean defaultVolumeFl = false;
    private Boolean eventVolumeFl = false;
   
    private List<Activity> activity = null;
   
    @Column(name = "ACTIVITY_OID", nullable = false)
    public Long getActivityOid()
    {
        return activityOid;
    }
    public void setActivityOid(Long activityOid)
    {
        this.activityOid = activityOid;
    }
    ...

In the above example, DeptActivity maps each of its instance variables to each column in table DEPT_ACTIVITY, what I would like is to create a DeptActivity object that also maintains instance variables from a different table.

Does Hibernate provide a way for creating Entity objects that maintain instance variables which are populated by two different tables?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 12, 2007 1:47 pm 
Newbie

Joined: Thu Jun 21, 2007 11:05 am
Posts: 5
I believe we are taking about the same thing....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 30, 2007 11:28 am 
Newbie

Joined: Thu Jul 12, 2007 12:52 pm
Posts: 5
Location: Boise, Idaho USA
Well I finally came across a solution to my problem. I figured I'd post it in case anybody else is having a similar issue...

The key is to use @SqlResultSetMapping in conjunction with @NamedNativeQuery. The code looks something like this:

Code:
package com.test.model.entity.dept

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityResult;
import javax.persistence.Id;
import javax.persistence.NamedNativeQuery;
import javax.persistence.SqlResultSetMapping;

@Entity
@SqlResultSetMapping(name = "implicit_mapping", entities = @EntityResult(entityClass = com.test.model.entity.dept.DeptActivity.class))
@NamedNativeQuery(name = "DeptActivity",
                query = "SELECT " +
                            "d.DEPT_ID as deptName, " +
                            "da.ACTIVITY_ID as activityName " +
                        "FROM " +
                            "DEPT d INNER JOIN " +
                            "DEPT_ACTIVITY da ON d.CHRONICLE_OID = da.DEPT_OID INNER JOIN " +
                        "WHERE " +
                            "(d.DEPT_OID = :deptOid) " +
                        "GROUP BY " +
                            "da.ACTIVITY_DT",
                             
                 resultSetMapping = "implicit_mapping")
public class DeptActivity
{
     private String deptName;
     private String activityName;

     @Id
     @Column
     public String getDeptName()
     {
           return deptName();
     }

     @Column
     pubic String getActivityName()
     {
           return activityName();
     }


Then you can refer to the query and replace the variables as such:
Code:
Query query = session.getNamedQuery("DeptActivity");
query.setLong("deptOid", 56L);
List<DeptActivity> list = query.list();


Hopefully this is helpful for others...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.