1
@Entity
@Table(name = TaxiBusiness.tableName)
public class TaxiBusiness {
    public static final String tableName = "gps_accept_order_" + 20170306;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private int ID;
    ...
}

I want to reuse the same entity to go through all the schemas and tables. One schema per month and one table per day. How to do it?

enter image description here

Zetian
  • 503
  • 1
  • 3
  • 9
  • What you want is sharding this has been implemented in the [Hibernate Shards](http://hibernate.org/others/) project, however your milage may vary due to not keeping up with newer Hibernate versions. So it might depend on which hibernate version you use. – M. Deinum Apr 20 '17 at 08:08

3 Answers3

1

As @Subrata mentioned, it may not be a good practice to create a new table daily.

But Anyway if you want to do it there is an option I can think of would be to use a database synonym / alias: FOO would be an alias for FOO_20170306 until... you change the alias to point on FOO_20170307.

For details you may look into the answer: https://stackoverflow.com/a/3880200/1934211

Hope this works for you, haven't tested it though.

Edited:

Here is a way to use synonyms of tables in hibernate: JPA Entiy on synonym instead of table

Another option I found is to create a custom Naming Strategy for hibernate tables: For details take a look at: https://stackoverflow.com/a/3618315/1934211

Community
  • 1
  • 1
OutOfMind
  • 874
  • 16
  • 32
1

You need to go through all tables which have the same structure. Therefore, why not just copy "table_date" one by one to another table, "current_table". Then, do whatever you need on "current_table".

dongx
  • 1,750
  • 4
  • 17
  • 37
-1

Actually it is not a good practice. It will be better if you keep only one schema. You can improve your database design so that no matter what the month and day is, we need not create separate table per day. It is actually bad idea what you thinking to achieve.

Think an efficient way to keep the database design simple.

Good Luck!!

Subrata nath
  • 172
  • 10
  • I am reading schema from an existing database. I got no control of the existing schema except reading it. – Zetian Apr 20 '17 at 05:43