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.