-->
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: How to join 2 table in hbm file
PostPosted: Fri Jul 18, 2008 8:56 am 
Newbie

Joined: Thu Jul 17, 2008 5:29 am
Posts: 3
How to join 2 tables into 1 class in hbm file. Both table have no relationship, both are independent. I just want to combine columns of both tables to 1 class in hbm file for only displaying column values in UI.

For example:

Table A:
a_id
a_name
a_age
....
....

Table B:
b_id
b_car_no
b_car_model
.....
.....


class AB:
//properties
a_id
b_id
a_name
a_age
b_car_no
c_car_model

I just want a class which combine columns of 2 tables for displaying purpose. I dont want 2 different class for 2 tables.

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 19, 2008 9:47 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
I'm not sure if you have the correct impetus driving the creation of your object model, but you can do what you need.

Here's a Hibernate Mapping Tutorial that will help you out:

Mapping two Tables to One Entity Class with Hibernate3

Quote:
One Class to Two Tables

From time to time, you'll run into a scenario where your application defines a single class, but the database maintains the correspoinding information in two separate tables. Well, with Hibernate, that's not a problem. All you have to do to map one class to two database tables is to define the first table mapping as you normally would, and then specify the name of the second table your class uses with the very intelligently named @SecondaryTable annotation. From there, when you get to a field that is supposed to be stored in the second table, well, you just mention the second table's name in the @Column annotation. It's all very simple and straight forward.

This tutorial will look at how we map one Java class to two database tables using the @SecondaryTable and @Column JPA annotations. We will create a single class named FooBar, and map it to two database tables, named foo and bar.



Code:
@Entity
@Table(name="bar")
@SecondaryTable(name="foo")
public class FooBar {
  int id;
  String fooName;
  String barCode;       
}



Code:
package com.examscam.mappings;
import javax.persistence.*;import org.hibernate.Session;
import com.examscam.HibernateUtil;

@Entity
@Table(name="bar")
@SecondaryTable(name="foo")
public class FooBar {
  int id;
  String fooName;
  String barCode;

  @Id
  @GeneratedValue
  public int getId() {return id;}
  public void setId(int id) {this.id = id;}

  @Column(table="foo")
  public String getFooName() {return fooName;}
  public void setFooName(String fooName) {
    this.fooName = fooName;
  }

  /* no need for mapping-goes to default bar table */
  public String getBarCode() {return barCode;}
  public void setBarCode(String barCode) {
    this.barCode = barCode;
  }

  public static void main(String args[]) {
/*HibernateUtil needs FooBar.class in AnnotationConfiguration*/
    HibernateUtil.recreateDatabase();
    FooBar fb = new FooBar();
    fb.setBarCode("90210");
    fb.setFooName("ManChu");
     Session session = HibernateUtil.beginTransaction();
    session.save(fb);
    HibernateUtil.commitTransaction();
  }
}


Check out my signature links for more Hibernate3 and Java Persistence API JPA Tutorials and Sample code examples.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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.