11

I'm currently stuck with what seems to be a very simple problem, but I just can't seem to find a way around:

I have 2 identical tables:

  1. tbl_creditcard_approved_txns
  2. tbl_creditcard_declined_txns

The fields in both are identical, and I have one class - Transaction that is used to represent all the appropriate fields in the tables.

I'm trying to map two different entities (one for each table) to the above class. In the old world, I'd have created two hbm.xml files, one for each table and map both of them to Transaction. I'd then use the entity name during persistence to ensure that the object gets persisted in the correct table, depending on the circumstance.

I am trying to use annotations currently to achieve the same but have had no luck so far in mapping the 2 entities to a single class. Is this possible at all?

I'm currently using a different approach in that I've extracted all the common fields (identical column names) into an @MappedSuperClass and have created two separate classes (one for each entity) that extend from the super class (these classes just have the same fields with different column names, where applicable).

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Jay
  • 133
  • 1
  • 1
  • 7
  • possible duplicate of [JPA, How to use the same class (entity) to map different tables?](http://stackoverflow.com/questions/997203/jpa-how-to-use-the-same-class-entity-to-map-different-tables) – Pascal Thivent Aug 17 '10 at 23:21
  • Pascal, This is indeed the intention, the only difference being that I would like two different entities mapped to the same class. Unlike JPA, in hibernate, classes can be disparate from entities. In essence, however, I really am trying to map one class to two tables. – Jay Aug 19 '10 at 14:10

3 Answers3

21

Using @MappedSuperclass, you would proceed as follows:

@MappedSuperclass
public class Transaction ...

@Entity
@Table(name="tbl_creditcard_approved_txns")
public class DeclinedTransaction extends Transaction ...

@Entity
@Table(name="tbl_creditcard_declined_txns")
public class ApprovedTransaction extends Transaction ...

Use @AttributeOverride to override column names between the two types of Transaction objects, if needed.

Update: I see that you want to map one @Entity to two tables in the same EntityManagerFactory ... I don't think you can do that.

les2
  • 14,093
  • 16
  • 59
  • 76
  • 1
    LES2, Thanks, for the response. What you suggested is the approach I've taken currently. As you observed, I was wondering if its possible to map one entity to two different tables. – Jay Aug 17 '10 at 19:23
  • if I move a row from one table to the other, how can I make sure the row ID is kept the same? – splinter123 Jul 28 '14 at 09:32
  • that sounds like a bad idea, but i'm not entirely sure why :) it's just against my instincts. what is the use case for keeping the row ID the same? – les2 Aug 12 '14 at 19:02
2

The other way to do it would be to use a partioned table on the database layer that would then make visible one single table to your java code.

This is probably the way to go as a general rule, the more smart partitioning you do, the faster your queries can be.

Jay
  • 19,649
  • 38
  • 121
  • 184
2

You have to use two different persistence units or two separate entities. This was already answered here.

Community
  • 1
  • 1
Javid Jamae
  • 8,741
  • 4
  • 47
  • 62