views:

29

answers:

2

Hi, I have a problem

Tables
sales_2009
sales_2008
sales_2007

And only one class (sales), How change the table at runtime?

+1  A: 

How change the table at runtime?

That's not supported out-of-the-box by standard JPA. But implementation might provide extensions for Horizontal partitioning:

I can't say anything about the maturity of Slice.

An alternative solution would be to define three different persistence units and to map the Sales entity specifically in each of them (possibly using XML mappings to reuse the Java code). And then, obtain the "right" EntityManager from the appropriate EntityManagerFactory. This solution is not ideal from a memory point of view though (plus some other limitations).

See also

Pascal Thivent
A: 

Thanks for the reply, I solved with iBATIS

In the file map (xml)
select * from $table$ where date = #date# and product_id = #product_id#

*In the file Java - example with Servlet *
Map parameter = new HashMap();
parameter.put("table", "sales2009");
parameter.put("date", "2009-10-08"); //MySQL date :)
parameter.put("product_id",17);

SqlMapClient sqlMap = (SqlMapClient) getServletContext().getAttribute("sqlMap");

List result = sqlMap.queryForList("selectSalesByParameters",parameter);
for(Sales s : result) { }

*Important $table$ != #table" Not working