4 Replies Latest reply on Jun 7, 2010 5:07 PM by cbensemann

    Usage of @Table annotation

    grisha
      Is it possible to set the name of @Table(name = ) dynamically, for example by EL expression? Or is there other way to use the same entity for different tables?
        • 1. Re: Usage of @Table annotation
          sean.tozer

          You really have multiple tables containing identical entities? I don't know of any way to do that. I'm curious as to what use case you have that needs that, though.

          • 2. Re: Usage of @Table annotation
            grisha

            I have about 1000 tables in database and they all have the same columns, COD and DSC. And I don't want to have 1000 entity classes for them

            • 3. Re: Usage of @Table annotation
              sean.tozer

              That's an... interesting database design. Perhaps there's a good reason for that?


              If there's a number of tables with a similar sub-set of columns, an option is to use the @embedded and @embeddable tags to define a sub-entity that is then used in the main entity. For example, this can be used with addresses to avoid specifying address number, street, city, postal code, and country on a number of tables. Instead, you could just refer to the group of columns as @embedded Address contactAddress, or something similar.


              If you actually have 1000 identical tables.... that's such an unorthodox database design that I can't really think of anything that would help. I mean, it would be easy to automatically generate the entities, I suppose, since they're all identical.

              • 4. Re: Usage of @Table annotation
                cbensemann

                Having large numbers of identical tables isn't all that uncommon. It is often done in large scalable apps or in apps that require large amounts of concurrency where locking in the DB has become an issue. As to whether it is a good design I don't know but it is definitely a solution to these problems. Depending on the reasons behind having these tables hibernate shards may provide a solution to what your problem. I have never used it but I believe it addresses similar problems. Alternatively (and again depending on how you access these tables) using database views to abstract away the tables and updating the view to point to the correct table may be a solution.