We have Hibernate based application where due to a large data set, two sets of tables are created where user_id will either be mapped in the UserTickets table or RestOfWorldTickets table. Would like to know how @Table on the entity java objects can be dynamically mapped based on some user selection.
@Entity
@Table(name = "**UserTickets**")
public class UserTickets {
@Id
@Column("Internal_id")
@GeneratedValue(strategy = IDENTITY)
private int internalId;
@Column("user_id")
private int userId;
@Column("state")
private String state;
@Column("city")
private String city;
@Column("address")
private String address;
@Column("ticketNumber")
private String ticketNumber;
..
// Setters and Getters
}
UserTickets DB Table
Internal_id | User_id | State | City | Address | ticket_number | ...
101 | 1025 | AZ | Tuscan | .. | 10256912 |
102 | 1026 | NC | Durham | .. | 10256983
RestOfWorldTickets DB Table
Internal_id | User_id | State | City | Address | ticket_number |..
101 | 1058 | {null} | London | .. | 102578963 |..
102 | 1059 | {null} | Berlin | .. | 112763458 |..
The user and table mapping are now defined in a new table.
TableMapping Database table.
Internal_id | User_id | TableMapped |
1 | 1025 | UserTickets |
2 | 1026 | UserTickets |
3 | 1058 | RestOfWorldTickets |
4 | 1059 | RestOfWorldTickets |
So, using the UserTickets result set, how I map @Table attribute on the UserTickets Java object dynamically so that my Criteria API queries will work automatically without changing them to HQL queries?
Maybe using Interceptors http://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/Interceptor.html?
I am quite unsure what you actually need but i try to give my solution based on a few quesses. Changing
@Tabledynamically is not -afaik- possible but if i guessed right you could have some benefit of inheritance in this case:1st modify
UserTicketsto allow inheritance2nd create a new entity
This allows you to use criteriaqueries against
UserTicketsbut in addition the queries are done againstRestOfWorldTicketsalso. So now when you search with user id result set will contain results from both tables. Checking/ loggin -for example withinstanceofoperator- you can see which one the ticket is."Disclaimer" i am using and testing with
openjpaso there can be some differences/probkems with this solution...