-->
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 combine data from two table without many toone mappig
PostPosted: Wed Sep 16, 2009 3:19 am 
Beginner
Beginner

Joined: Mon Sep 14, 2009 9:29 am
Posts: 30
how to combine data from two table without many toone mappig


Top
 Profile  
 
 Post subject: Re: how to combine data from two table without many toone mappig
PostPosted: Wed Sep 16, 2009 9:06 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
There are a few ways. Here's a tutorial I put together on mapping one Java class to two database tables:

http://jpa.ezhibernate.com/Javacode/lea ... ahibernate

Here's some sample code. Notice the @SecondaryTable annotation. That's really the key:

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();
  }
}


http://jpa.ezhibernate.com/Javacode/lea ... ahibernate

_________________
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.