Mapping 2 Tables with same structure in Hibernate












0















I have an existing application with an entity, for example Employee (Table EMP) and it is used at several places. Now after a long time, a replica (EMP_ARC) for the table EMP is created and we are supposed to refer it in certain scenarios.



As the table structure is exactly same I thought of reusing the POJO declaration. But I could not find a way to reuse the same POJO for two different table declarations. (I had seen examples of XML mapping to map one POJO to multiple tables but our application is using annotations and not able to find such option with annotation)



I cannot use Inheritance strategy because polymorphic queries will degrade the performance of the application and the EMP_ARC table has to be referred only when required.



Could anyone please tell me if creating a duplicate POJO and mapping it to the replica table is the only solution or any other approach is available.



**Sample Code:**

Table structure for EMP and EMP_ARC (exactly same structure)
EMP_ID LONG
EMP_NAME VARCHAR2(100)

**Existing Java Bean mapped to EMP**
**This entity bean cannot be modified**
//getter and setters omitted

@Entity
@Table(name="EMP")
public class Employee{

@Id
@Column(name="EMP_ID")
Long empid;

@Column(name="EMP_NAME")
String empName;
}

//Scenario 1 Query EMP table
String hql = "from Employee";

//Scenario 2 Query EMP_ARC table -- Like SELECT * FROM EMP_ARC
//Not sure of how to write the HQL using the same Employee POJO.
//Output of this HQL has to be an Employee POJO.


I want to map the same Employee POJO to the table EMP_ARC also but not able to find a way.



Alternatively, if I can selectively enable/disable polymorphic queries then I will be able to use Inheritance with strategy TABLE_PER_CLASS.



--- Workaround ---



To dynamically enable or disable polymorphic queries, I am using two different session factories.




  • I have two POJOs Employee and EmployeeArc (inheriting Employee).

  • Inheritance strategy used is TABLE_PER_CLASS.

  • One session factory will refer to the Employee entity alone and the other session factory will have both the Employee and EmployeeArc entities.

  • Whenever polymorphic query feature is not required then I will use the first session factory, otherwise use the second one.


If anybody is aware of a better solution, please help me in solving this in a better way.










share|improve this question

























  • Have you produced any code? If so, please share it.

    – Christopher
    Nov 20 '18 at 10:22











  • Hi @Christopher, I have placed the sample code. Could you please help.

    – Jayalakshmi Das
    Nov 23 '18 at 8:34
















0















I have an existing application with an entity, for example Employee (Table EMP) and it is used at several places. Now after a long time, a replica (EMP_ARC) for the table EMP is created and we are supposed to refer it in certain scenarios.



As the table structure is exactly same I thought of reusing the POJO declaration. But I could not find a way to reuse the same POJO for two different table declarations. (I had seen examples of XML mapping to map one POJO to multiple tables but our application is using annotations and not able to find such option with annotation)



I cannot use Inheritance strategy because polymorphic queries will degrade the performance of the application and the EMP_ARC table has to be referred only when required.



Could anyone please tell me if creating a duplicate POJO and mapping it to the replica table is the only solution or any other approach is available.



**Sample Code:**

Table structure for EMP and EMP_ARC (exactly same structure)
EMP_ID LONG
EMP_NAME VARCHAR2(100)

**Existing Java Bean mapped to EMP**
**This entity bean cannot be modified**
//getter and setters omitted

@Entity
@Table(name="EMP")
public class Employee{

@Id
@Column(name="EMP_ID")
Long empid;

@Column(name="EMP_NAME")
String empName;
}

//Scenario 1 Query EMP table
String hql = "from Employee";

//Scenario 2 Query EMP_ARC table -- Like SELECT * FROM EMP_ARC
//Not sure of how to write the HQL using the same Employee POJO.
//Output of this HQL has to be an Employee POJO.


I want to map the same Employee POJO to the table EMP_ARC also but not able to find a way.



Alternatively, if I can selectively enable/disable polymorphic queries then I will be able to use Inheritance with strategy TABLE_PER_CLASS.



--- Workaround ---



To dynamically enable or disable polymorphic queries, I am using two different session factories.




  • I have two POJOs Employee and EmployeeArc (inheriting Employee).

  • Inheritance strategy used is TABLE_PER_CLASS.

  • One session factory will refer to the Employee entity alone and the other session factory will have both the Employee and EmployeeArc entities.

  • Whenever polymorphic query feature is not required then I will use the first session factory, otherwise use the second one.


If anybody is aware of a better solution, please help me in solving this in a better way.










share|improve this question

























  • Have you produced any code? If so, please share it.

    – Christopher
    Nov 20 '18 at 10:22











  • Hi @Christopher, I have placed the sample code. Could you please help.

    – Jayalakshmi Das
    Nov 23 '18 at 8:34














0












0








0








I have an existing application with an entity, for example Employee (Table EMP) and it is used at several places. Now after a long time, a replica (EMP_ARC) for the table EMP is created and we are supposed to refer it in certain scenarios.



As the table structure is exactly same I thought of reusing the POJO declaration. But I could not find a way to reuse the same POJO for two different table declarations. (I had seen examples of XML mapping to map one POJO to multiple tables but our application is using annotations and not able to find such option with annotation)



I cannot use Inheritance strategy because polymorphic queries will degrade the performance of the application and the EMP_ARC table has to be referred only when required.



Could anyone please tell me if creating a duplicate POJO and mapping it to the replica table is the only solution or any other approach is available.



**Sample Code:**

Table structure for EMP and EMP_ARC (exactly same structure)
EMP_ID LONG
EMP_NAME VARCHAR2(100)

**Existing Java Bean mapped to EMP**
**This entity bean cannot be modified**
//getter and setters omitted

@Entity
@Table(name="EMP")
public class Employee{

@Id
@Column(name="EMP_ID")
Long empid;

@Column(name="EMP_NAME")
String empName;
}

//Scenario 1 Query EMP table
String hql = "from Employee";

//Scenario 2 Query EMP_ARC table -- Like SELECT * FROM EMP_ARC
//Not sure of how to write the HQL using the same Employee POJO.
//Output of this HQL has to be an Employee POJO.


I want to map the same Employee POJO to the table EMP_ARC also but not able to find a way.



Alternatively, if I can selectively enable/disable polymorphic queries then I will be able to use Inheritance with strategy TABLE_PER_CLASS.



--- Workaround ---



To dynamically enable or disable polymorphic queries, I am using two different session factories.




  • I have two POJOs Employee and EmployeeArc (inheriting Employee).

  • Inheritance strategy used is TABLE_PER_CLASS.

  • One session factory will refer to the Employee entity alone and the other session factory will have both the Employee and EmployeeArc entities.

  • Whenever polymorphic query feature is not required then I will use the first session factory, otherwise use the second one.


If anybody is aware of a better solution, please help me in solving this in a better way.










share|improve this question
















I have an existing application with an entity, for example Employee (Table EMP) and it is used at several places. Now after a long time, a replica (EMP_ARC) for the table EMP is created and we are supposed to refer it in certain scenarios.



As the table structure is exactly same I thought of reusing the POJO declaration. But I could not find a way to reuse the same POJO for two different table declarations. (I had seen examples of XML mapping to map one POJO to multiple tables but our application is using annotations and not able to find such option with annotation)



I cannot use Inheritance strategy because polymorphic queries will degrade the performance of the application and the EMP_ARC table has to be referred only when required.



Could anyone please tell me if creating a duplicate POJO and mapping it to the replica table is the only solution or any other approach is available.



**Sample Code:**

Table structure for EMP and EMP_ARC (exactly same structure)
EMP_ID LONG
EMP_NAME VARCHAR2(100)

**Existing Java Bean mapped to EMP**
**This entity bean cannot be modified**
//getter and setters omitted

@Entity
@Table(name="EMP")
public class Employee{

@Id
@Column(name="EMP_ID")
Long empid;

@Column(name="EMP_NAME")
String empName;
}

//Scenario 1 Query EMP table
String hql = "from Employee";

//Scenario 2 Query EMP_ARC table -- Like SELECT * FROM EMP_ARC
//Not sure of how to write the HQL using the same Employee POJO.
//Output of this HQL has to be an Employee POJO.


I want to map the same Employee POJO to the table EMP_ARC also but not able to find a way.



Alternatively, if I can selectively enable/disable polymorphic queries then I will be able to use Inheritance with strategy TABLE_PER_CLASS.



--- Workaround ---



To dynamically enable or disable polymorphic queries, I am using two different session factories.




  • I have two POJOs Employee and EmployeeArc (inheriting Employee).

  • Inheritance strategy used is TABLE_PER_CLASS.

  • One session factory will refer to the Employee entity alone and the other session factory will have both the Employee and EmployeeArc entities.

  • Whenever polymorphic query feature is not required then I will use the first session factory, otherwise use the second one.


If anybody is aware of a better solution, please help me in solving this in a better way.







hibernate-mapping






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 10:16







Jayalakshmi Das

















asked Nov 20 '18 at 10:15









Jayalakshmi DasJayalakshmi Das

12




12













  • Have you produced any code? If so, please share it.

    – Christopher
    Nov 20 '18 at 10:22











  • Hi @Christopher, I have placed the sample code. Could you please help.

    – Jayalakshmi Das
    Nov 23 '18 at 8:34



















  • Have you produced any code? If so, please share it.

    – Christopher
    Nov 20 '18 at 10:22











  • Hi @Christopher, I have placed the sample code. Could you please help.

    – Jayalakshmi Das
    Nov 23 '18 at 8:34

















Have you produced any code? If so, please share it.

– Christopher
Nov 20 '18 at 10:22





Have you produced any code? If so, please share it.

– Christopher
Nov 20 '18 at 10:22













Hi @Christopher, I have placed the sample code. Could you please help.

– Jayalakshmi Das
Nov 23 '18 at 8:34





Hi @Christopher, I have placed the sample code. Could you please help.

– Jayalakshmi Das
Nov 23 '18 at 8:34












1 Answer
1






active

oldest

votes


















0














You could go with inherited classes/entities. As I do not know your exact usecase, I would suggest you check these annotation-keywords:




  1. @MappedSuperclass

  2. @Inheritance


Here is an explanation about their differences: @MappedSuperclass vs. @Inheritance






share|improve this answer
























  • Not able to use this because I cannot modify the existing class to use MappedSuperClass and I don't want polymorphic queries to be enabled while using Inheritance. If there is a way to selectively disable polymorphic queries then I can use Inheritance

    – Jayalakshmi Das
    Nov 26 '18 at 13:39













  • Then I do not see a possibility do reuse the POJO. I think, you have to duplicate your POJO here :-(

    – Tobi
    Nov 26 '18 at 13:42











  • Thank you for confirming. If you are aware of programmatically disabling polymorphic queries or any workaround for dynamically enabling/disabling them, please let me know.

    – Jayalakshmi Das
    Nov 27 '18 at 6:52











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390734%2fmapping-2-tables-with-same-structure-in-hibernate%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You could go with inherited classes/entities. As I do not know your exact usecase, I would suggest you check these annotation-keywords:




  1. @MappedSuperclass

  2. @Inheritance


Here is an explanation about their differences: @MappedSuperclass vs. @Inheritance






share|improve this answer
























  • Not able to use this because I cannot modify the existing class to use MappedSuperClass and I don't want polymorphic queries to be enabled while using Inheritance. If there is a way to selectively disable polymorphic queries then I can use Inheritance

    – Jayalakshmi Das
    Nov 26 '18 at 13:39













  • Then I do not see a possibility do reuse the POJO. I think, you have to duplicate your POJO here :-(

    – Tobi
    Nov 26 '18 at 13:42











  • Thank you for confirming. If you are aware of programmatically disabling polymorphic queries or any workaround for dynamically enabling/disabling them, please let me know.

    – Jayalakshmi Das
    Nov 27 '18 at 6:52
















0














You could go with inherited classes/entities. As I do not know your exact usecase, I would suggest you check these annotation-keywords:




  1. @MappedSuperclass

  2. @Inheritance


Here is an explanation about their differences: @MappedSuperclass vs. @Inheritance






share|improve this answer
























  • Not able to use this because I cannot modify the existing class to use MappedSuperClass and I don't want polymorphic queries to be enabled while using Inheritance. If there is a way to selectively disable polymorphic queries then I can use Inheritance

    – Jayalakshmi Das
    Nov 26 '18 at 13:39













  • Then I do not see a possibility do reuse the POJO. I think, you have to duplicate your POJO here :-(

    – Tobi
    Nov 26 '18 at 13:42











  • Thank you for confirming. If you are aware of programmatically disabling polymorphic queries or any workaround for dynamically enabling/disabling them, please let me know.

    – Jayalakshmi Das
    Nov 27 '18 at 6:52














0












0








0







You could go with inherited classes/entities. As I do not know your exact usecase, I would suggest you check these annotation-keywords:




  1. @MappedSuperclass

  2. @Inheritance


Here is an explanation about their differences: @MappedSuperclass vs. @Inheritance






share|improve this answer













You could go with inherited classes/entities. As I do not know your exact usecase, I would suggest you check these annotation-keywords:




  1. @MappedSuperclass

  2. @Inheritance


Here is an explanation about their differences: @MappedSuperclass vs. @Inheritance







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 9:43









TobiTobi

2198




2198













  • Not able to use this because I cannot modify the existing class to use MappedSuperClass and I don't want polymorphic queries to be enabled while using Inheritance. If there is a way to selectively disable polymorphic queries then I can use Inheritance

    – Jayalakshmi Das
    Nov 26 '18 at 13:39













  • Then I do not see a possibility do reuse the POJO. I think, you have to duplicate your POJO here :-(

    – Tobi
    Nov 26 '18 at 13:42











  • Thank you for confirming. If you are aware of programmatically disabling polymorphic queries or any workaround for dynamically enabling/disabling them, please let me know.

    – Jayalakshmi Das
    Nov 27 '18 at 6:52



















  • Not able to use this because I cannot modify the existing class to use MappedSuperClass and I don't want polymorphic queries to be enabled while using Inheritance. If there is a way to selectively disable polymorphic queries then I can use Inheritance

    – Jayalakshmi Das
    Nov 26 '18 at 13:39













  • Then I do not see a possibility do reuse the POJO. I think, you have to duplicate your POJO here :-(

    – Tobi
    Nov 26 '18 at 13:42











  • Thank you for confirming. If you are aware of programmatically disabling polymorphic queries or any workaround for dynamically enabling/disabling them, please let me know.

    – Jayalakshmi Das
    Nov 27 '18 at 6:52

















Not able to use this because I cannot modify the existing class to use MappedSuperClass and I don't want polymorphic queries to be enabled while using Inheritance. If there is a way to selectively disable polymorphic queries then I can use Inheritance

– Jayalakshmi Das
Nov 26 '18 at 13:39







Not able to use this because I cannot modify the existing class to use MappedSuperClass and I don't want polymorphic queries to be enabled while using Inheritance. If there is a way to selectively disable polymorphic queries then I can use Inheritance

– Jayalakshmi Das
Nov 26 '18 at 13:39















Then I do not see a possibility do reuse the POJO. I think, you have to duplicate your POJO here :-(

– Tobi
Nov 26 '18 at 13:42





Then I do not see a possibility do reuse the POJO. I think, you have to duplicate your POJO here :-(

– Tobi
Nov 26 '18 at 13:42













Thank you for confirming. If you are aware of programmatically disabling polymorphic queries or any workaround for dynamically enabling/disabling them, please let me know.

– Jayalakshmi Das
Nov 27 '18 at 6:52





Thank you for confirming. If you are aware of programmatically disabling polymorphic queries or any workaround for dynamically enabling/disabling them, please let me know.

– Jayalakshmi Das
Nov 27 '18 at 6:52


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390734%2fmapping-2-tables-with-same-structure-in-hibernate%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]