-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: How to change the @Table name value dynamically
PostPosted: Fri Apr 15, 2016 12:01 am 
Newbie

Joined: Thu Apr 14, 2016 11:46 pm
Posts: 3
Hi ,

I have a requirement to support one entity class to map to mutliple tables using hibernate annotations , so how i can i change the table name dynamically ?? , i want to change the table name "persons" to "<dynamic value>" , how can we change that value , if changed then how to reload the session factory ?

Sample example :

Code:
@Entity
@Table("persons")
public class Person {

    @Column(name="first_name")
    private String firstName;

    @Column(name="last_name")
    private String lastName;

    public Person() {
     
    }
}


Thanks & Regards,
Veena


Top
 Profile  
 
 Post subject: Re: How to change the @Table name value dynamically
PostPosted: Fri Apr 15, 2016 1:56 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1638
Location: Romania
You can map an entity to multiple tables, using @SecondaryTable annotation, but you cannot change the underlying table(s) on-the-fly.

I wonder why do you even need that because the table names are usually given by a database schema that doesn't change.

However, you can use a custom NamingStartegy (pre-Hibernate 5) or a PhysicalNamingStrategy (Hibernate 5+) so that you can change the entity-table mapping at run-time. But this cannot happen while the SessionFactory is running. You have to switch to a new SessionFactory that takes the custom naming strategy.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: How to change the @Table name value dynamically
PostPosted: Fri Apr 15, 2016 3:26 am 
Newbie

Joined: Thu Apr 14, 2016 11:46 pm
Posts: 3
Hi mihalcea ,

Thanks for replying , Essentially my problem is I have 1 database containing all of my tables of different basic types, Users, Groups, Addresses. Now to save on performance those 3 types have been split into the respective state of where they are located. Exampe as follows.
---Arizona Users---
Agent_AZ
Groups_AZ
Addresses_AZ
---California Users---
Agent_CA
Groups_CA
Addresses_CA

Example Agent Entity class will be same , and it has to support for (Agent_AZ & Agent_CA) , my entities are tightly binded with annotations , how we can change the @table(name=<dynamically>) and build the session factory again.

Kindly please do need full.


Top
 Profile  
 
 Post subject: Re: How to change the @Table name value dynamically
PostPosted: Fri Apr 15, 2016 4:10 am 
Newbie

Joined: Thu Apr 14, 2016 4:01 am
Posts: 2
Hi Veena22

I have had to implement the same strategy in one of my projects.
Below is the pattern that I followed.

Create a entity pointing to a base table of Agent, Group etc. with the @Table annotation. Remember that the base tables need to exist. I have a Token table

Code:
@Table(name="TOKEN")
@Entity
public class Token implements Serializable{}


Lets say I name the Token table Token_SA I need to query that table in my code.
Below is how I do it.

Create the SQL (Native)
Code:
       
private final String QUERY = "SELECT t.ID, t.EXPIRE_ON, t.NUMBER, t.SERIAL_NUMBER, t.TOKEN_STATE_ID, t.STATE_REASON, t.BATCH_ID "
                + "FROM %s AS t "
                + "INNER JOIN BATCH AS b ON t.BATCH_ID = b.ID WHERE b.NUMBER = ?"
                + "AND t.TOKEN_STATE_ID = ?";


Replace the %s with the table (TOKEN_SA) that you want to query. You can use String.format(QUERY, "TOKEN_SA")

Then I create a native query using the sql

Code:
       
List<Token> tokens = em.createNativeQuery(sql, Token.class)
                .setParameter(1, batchNumber)
                .setParameter(2, tokenStateId)
                .getResultList();


Not sure if the solution above is perfect, but it did solve my problem of changing the tables in Runtime


Top
 Profile  
 
 Post subject: Re: How to change the @Table name value dynamically
PostPosted: Fri Apr 15, 2016 4:50 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1638
Location: Romania
Hi Rentius,

Your solution works only for select queries, but, if you want to have CRUD as well, you need the Naming Strategy option.

You might be interested in a solution such as this one.

Vlad

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: How to change the @Table name value dynamically
PostPosted: Fri Apr 15, 2016 5:03 am 
Newbie

Joined: Thu Apr 14, 2016 11:46 pm
Posts: 3
mihalcea_vlad ,

Can you please expalin about Naming Strategy option , i didnot understand with below mentioned link.


Thanks & Regards,
Veena


Top
 Profile  
 
 Post subject: Re: How to change the @Table name value dynamically
PostPosted: Fri Apr 15, 2016 9:11 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1638
Location: Romania
What exactly you did not understand?

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: How to change the @Table name value dynamically
PostPosted: Mon Apr 25, 2016 8:31 am 
Newbie

Joined: Sat Dec 12, 2009 5:33 am
Posts: 2
Well, I am solving similar problem, however, with usage of table partitioning provided by PostgreSQL.

for your case you should create the following structure:
single table USER with field where it belongs (belongs_to) (AZ, TX etc.) (this table will be empty)
create inherited table USER_AZ like
Code:
CREATE TABLE USER_AZ (constraint USER_AZ_CHECK CHECK (belongs_to = 'AZ') ) INHERITS USER;

create inherited table USER_TX like
Code:
CREATE TABLE USER_TX (constraint USER_TX_CHECK CHECK (belongs_to = 'TX') ) INHERITS USER;



provide trigger if you need to insert data: trigger is applied on USER table, and makes a real insert into partition-specific table. Another trigger is required to trick Hibernate, to make him fill that the data was inserted. If needed I will show you.

All the details you can find in comprehensive PostgreSQL documentation.

Then You can map USER table to your User Entity, and make transparent queries, either on all users (this will make PostgreSQL to work with each tables), or with specifying to which partition it belongs (adding another query option belongs_to = 'AZ' for example), and in this case PostgreSQL will work strait forward with your single table USER_AZ, but you will be querying same hibernate entity and all your logic will be implemented once.

The problem in your case could be, your database is at the moment already partitioned, so you have to create some pl/sql script to efficiently dump and recreate your structure based on partitioning.

and I guess same features could be achieved using other databases. However I would recommend to stick to PostgreSQL - for last 10 years it never let me down.


Top
 Profile  
 
 Post subject: Re: How to change the @Table name value dynamically
PostPosted: Wed Nov 09, 2016 2:49 pm 
Newbie

Joined: Wed Nov 09, 2016 2:45 pm
Posts: 3
Rentius wrote:
Hi Veena22

I have had to implement the same strategy in one of my projects.
Below is the pattern that I followed.

Create a entity pointing to a base table of Agent, Group etc. with the @Table annotation. Remember that the base tables need to exist. I have a Token table

Code:
@Table(name="TOKEN")
@Entity
public class Token implements Serializable{}


Lets say I name the Token table Token_SA I need to query that table in my code.
Below is how I do it.

Create the SQL (Native)
Code:
       
private final String QUERY = "SELECT t.ID, t.EXPIRE_ON, t.NUMBER, t.SERIAL_NUMBER, t.TOKEN_STATE_ID, t.STATE_REASON, t.BATCH_ID "
                + "FROM %s AS t "
                + "INNER JOIN BATCH AS b ON t.BATCH_ID = b.ID WHERE b.NUMBER = ?"
                + "AND t.TOKEN_STATE_ID = ?";


Replace the %s with the table (TOKEN_SA) that you want to query. You can use String.format(QUERY, "TOKEN_SA")

Then I create a native query using the sql

Code:
       
List<Token> tokens = em.createNativeQuery(sql, Token.class)
                .setParameter(1, batchNumber)
                .setParameter(2, tokenStateId)
                .getResultList();


Not sure if the solution above is perfect, but it did solve my problem of changing the tables in Runtime


Rentius, i'm in similar sittuation, but i implement hibertnate annotation, can i use this solution?? if the answer is YES, how?.


Top
 Profile  
 
 Post subject: Re: How to change the @Table name value dynamically
PostPosted: Wed Nov 09, 2016 2:56 pm 
Newbie

Joined: Wed Nov 09, 2016 2:45 pm
Posts: 3
Dear Rentius,

i have the same problem, but i implement hibernate annotations, can you say me if it works or i need another solution.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.