Java – how to map a class to different tables using hibernate / JPA annotations
At present, my persistence seems to be a very simple problem, but I can't seem to find a way:
I have two identical tables:
> tbl_ creditcard_ approved_ txns > tbl_ creditcard_ declined_ txns
The fields in the two are the same, and I have a class - transactions that represent all the corresponding fields in the table
I tried to map two different entities (one per table) to the above class In the old world, I created two hbms XML file, one in each table, and map them to transaction Then, I will use entity names during persistence to ensure that objects remain unchanged in the correct table, depending on the situation
At present, I try to use annotations to achieve the same function, but so far I have no luck mapping two entities to one class Is this possible
I am currently using a different method to extract all common fields (the same column name) into @ mappedsuperclass, and provide two independent classes (one for each entity) for superclasses (these classes are the same fields with different column names, if applicable)
Solution
Using @ mappedsuperclass, you will continue 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 ...
If necessary, use @ attributeoverride to override the column names between two types of transaction objects
Update: I see you want to map an @ entity to two tables in the same entitymanagerfactory... I don't think you can do this