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 ... ahibernateHere'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