[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.rt.eclipselink] Re: many table, one entity, annotation driven?

Hi,

there are two possible solutions for your problem.

* MappedSuperclass
* Embedable

But it's not the same as using discriminator columns. In both cases you no more can do queries like "select c from Common c"

1a) MappedSuperclass with one common table

@MappedSuperclass
@Table(name="COMMON")
public abstract class Common {
  @Id
  @GeneratedValue
  @Column
  private Long id;
  @Column
  private String objName;
  @Column
  private String objDocument;
  ...
}

@Entity
@AttributeOverride(name="id", column=@Column(name="IDREF"))
public class Newsletter extends Common {
@Column
@Temporal(TemporalType.TIMESTAMP)
private Date newsletterDate;
@Column
private String newsletterHeader;
@Column
private String newsletterBody;
...
}
-----------------------------------------------------------------------------
1b) MappedSuperclass with the possibility to use different "base" tables (see SecondaryTable annotation and AttributeOverrides)


@MappedSuperclass
public abstract class Common {
  @Id
  @GeneratedValue
  @Column
  private Long id;
  @Column
  private String objName;
  @Column
  private String objDocument;
  ...
}

@Entity
@SecondaryTable(name="COMMON", pkJoinColumns=@PrimaryKeyJoinColumn(name="ID"))
@AttributeOverrides({
@AttributeOverride(name="objName", column=@Column(name="OBJNAME", table="COMMON"))
,@AttributeOverride(name="objDocument", column=@Column(name="OBJDOCUMENT", table="COMMON"))
})
@AttributeOverride(name="id", column=@Column(name="IDREF"))
public class Newsletter extends Common {
@Column
@Temporal(TemporalType.TIMESTAMP)
private Date newsletterDate;
@Column
private String newsletterHeader;
@Column
private String newsletterBody;
...
}


-----------------------------------------------------------------------------
2) Embedable in a common table

public interface ObjectInfoProvider {
  ObjectInfo getObjectInfo();
}

@Embeddable
public class ObjectInfo {
  @Column
  private String objName;
  @Column
  private String objDocument;
  ...
}

@Entity
@SecondaryTable(name="COMMON", pkJoinColumns=@PrimaryKeyJoinColumn(name="ID"))
public class Newsletter implements ObjectInfoProvider {
@Id
@GeneratedValue
@Column(name="IDREF")
private Long id;
@Column
@Temporal(TemporalType.TIMESTAMP)
private Date newsletterDate;
@Column
private String newsletterHeader;
@Column
private String newsletterBody;
@Embedded
@AttributeOverrides({
@AttributeOverride(name="objName", column=@Column(name="OBJNAME", table="COMMON"))
,@AttributeOverride(name="objDocument", column=@Column(name="OBJDOCUMENT", table="COMMON"))
})
private ObjectInfo objectInfo;
...
}


One good book with explanation for all inheritance related issues is this one
http://www.amazon.de/Pro-EJB-Persistence-Experts-Voice/dp/1590596455
and maybe this one too, but the focus of this book is the hibernate JPA implementation
http://www.amazon.de/Java-Persistence-with-Hibernate/dp/1932394885


Hope it helps,
Michael

"Kristian Rink" <kawazu@xxxxxxxxxxxxx> schrieb im Newsbeitrag news:g7bo44$qd7$1@xxxxxxxxxxxxxxxxxxxx
Hi again;

speaking of "tinsel" and obscure database design:

Kristian Rink schrieb:
[...]
@Entity
@DiscriminatorValue("NEWSLETTER")
@Table(name="NEWSLETTER")
@PrimaryKeyJoinColumn(name="IDREF")
[...]

While this approach (@DiscriminatorColumn / @DiscriminatorValue) does rather
well do in most situations (and, for that matters, makes migration of our DB
abstraction a rather pleasant procedure), I now have stumbled across a set
of situations in which this is not applicable, because, even though the
hierarchy is right the same (extending classes, common base class, separate
tables), there is no column usable as discriminator, leaving our backend
system obviously doing a simple join across the two tables using the common
ID, assuming the Discriminator is not required as "while the data is in the
'extending class' table, it per se must extend 'common' and thus have an
entry there". Which leaves me dealing with this... simply dumping the
@Discriminator* annotations, as supposed, broke my code again... Any way of
doing an inheritance hierarchy here without @Discriminator*? And, asides
that, any good books to be read on JPA dealing with these issues? Don't want
to steal anyones time...


Cheers & thanks for your patience,
Kristian

--
Kristian Rink * http://zimmer428.net * http://flickr.com/photos/z428/ jab:
kawazu@xxxxxxxxxxxxx * icq: 48874445 * fon: ++49 176 2447 2771 "One dreaming
alone, it will be only a dream; many dreaming together is the beginning of a
new reality." (Hundertwasser)