can we use same hibernate annotated pojo class for multiple databases?
I have a JAVA POJO class with hibernate annotation for postgresql database.
Now, I have a requirement that we support multiple databases in our application. My question is : Should we use the same class with other databases (Oracle, MySQL, SQL Server) or should I write separate annotated class for each different database ?
Reason: To support special characters we are using database proprietary types instead of hibernate types like
// for oracle
@Column(sql-type="nvarchar2")
private String name;
// for sql server
@Column(sql-type="nvarchar")
private String name;
// hibernate doesn't support different proprietary sql types at same type like this
@Column(sql-type="nvarchar","nvarchar2")
private String name;
java hibernate pojo
add a comment |
I have a JAVA POJO class with hibernate annotation for postgresql database.
Now, I have a requirement that we support multiple databases in our application. My question is : Should we use the same class with other databases (Oracle, MySQL, SQL Server) or should I write separate annotated class for each different database ?
Reason: To support special characters we are using database proprietary types instead of hibernate types like
// for oracle
@Column(sql-type="nvarchar2")
private String name;
// for sql server
@Column(sql-type="nvarchar")
private String name;
// hibernate doesn't support different proprietary sql types at same type like this
@Column(sql-type="nvarchar","nvarchar2")
private String name;
java hibernate pojo
Which java package and hibernate version is this@Column
annotation from?
– Selaron
Nov 15 '18 at 13:48
javax.persistence.Column and hibernate version 5.0.2
– Ahmad
Nov 15 '18 at 13:50
I did not find an annotation that provides ansql-type
attribute. Did you mean Column#columnDefinition attribute instead? docs.oracle.com/javaee/7/api/javax/persistence/…
– Selaron
Nov 15 '18 at 14:33
if you question is answered, please mark it answerd.
– Selaron
Jan 15 at 10:20
add a comment |
I have a JAVA POJO class with hibernate annotation for postgresql database.
Now, I have a requirement that we support multiple databases in our application. My question is : Should we use the same class with other databases (Oracle, MySQL, SQL Server) or should I write separate annotated class for each different database ?
Reason: To support special characters we are using database proprietary types instead of hibernate types like
// for oracle
@Column(sql-type="nvarchar2")
private String name;
// for sql server
@Column(sql-type="nvarchar")
private String name;
// hibernate doesn't support different proprietary sql types at same type like this
@Column(sql-type="nvarchar","nvarchar2")
private String name;
java hibernate pojo
I have a JAVA POJO class with hibernate annotation for postgresql database.
Now, I have a requirement that we support multiple databases in our application. My question is : Should we use the same class with other databases (Oracle, MySQL, SQL Server) or should I write separate annotated class for each different database ?
Reason: To support special characters we are using database proprietary types instead of hibernate types like
// for oracle
@Column(sql-type="nvarchar2")
private String name;
// for sql server
@Column(sql-type="nvarchar")
private String name;
// hibernate doesn't support different proprietary sql types at same type like this
@Column(sql-type="nvarchar","nvarchar2")
private String name;
java hibernate pojo
java hibernate pojo
edited Nov 15 '18 at 13:50
Selaron
2,48611323
2,48611323
asked Nov 15 '18 at 12:52
AhmadAhmad
7381827
7381827
Which java package and hibernate version is this@Column
annotation from?
– Selaron
Nov 15 '18 at 13:48
javax.persistence.Column and hibernate version 5.0.2
– Ahmad
Nov 15 '18 at 13:50
I did not find an annotation that provides ansql-type
attribute. Did you mean Column#columnDefinition attribute instead? docs.oracle.com/javaee/7/api/javax/persistence/…
– Selaron
Nov 15 '18 at 14:33
if you question is answered, please mark it answerd.
– Selaron
Jan 15 at 10:20
add a comment |
Which java package and hibernate version is this@Column
annotation from?
– Selaron
Nov 15 '18 at 13:48
javax.persistence.Column and hibernate version 5.0.2
– Ahmad
Nov 15 '18 at 13:50
I did not find an annotation that provides ansql-type
attribute. Did you mean Column#columnDefinition attribute instead? docs.oracle.com/javaee/7/api/javax/persistence/…
– Selaron
Nov 15 '18 at 14:33
if you question is answered, please mark it answerd.
– Selaron
Jan 15 at 10:20
Which java package and hibernate version is this
@Column
annotation from?– Selaron
Nov 15 '18 at 13:48
Which java package and hibernate version is this
@Column
annotation from?– Selaron
Nov 15 '18 at 13:48
javax.persistence.Column and hibernate version 5.0.2
– Ahmad
Nov 15 '18 at 13:50
javax.persistence.Column and hibernate version 5.0.2
– Ahmad
Nov 15 '18 at 13:50
I did not find an annotation that provides an
sql-type
attribute. Did you mean Column#columnDefinition attribute instead? docs.oracle.com/javaee/7/api/javax/persistence/…– Selaron
Nov 15 '18 at 14:33
I did not find an annotation that provides an
sql-type
attribute. Did you mean Column#columnDefinition attribute instead? docs.oracle.com/javaee/7/api/javax/persistence/…– Selaron
Nov 15 '18 at 14:33
if you question is answered, please mark it answerd.
– Selaron
Jan 15 at 10:20
if you question is answered, please mark it answerd.
– Selaron
Jan 15 at 10:20
add a comment |
2 Answers
2
active
oldest
votes
If it's ok to use nvarchar
for all String
typed columns by default, you could extend Oracle and MS SQLServer dialects and do something like this:
public class CustomOracleDialect extends org.hibernate.dialect.Oracle10gDialect
@Override
protected void registerCharacterTypeMappings()
super.registerCharacterTypeMappings();
registerColumnType(Types.VARCHAR, "nvarchar2");
public class CustomSQLServerDialect extends org.hibernate.dialect.SQLServer2012Dialect
public CustomSQLServerDialect()
super();
registerColumnType(Types.VARCHAR, "nvarchar");
Then configure these dialects in dependence to the database type used.
add a comment |
The same exact class WILL work with other DB engines, as far as Hibernate is concerned all it cares about is the dialect for the most part. However, some DB engines don't support the identity generation strategy for ID fields for example (from past experience). Depending on what DB engines you are required to work with you may have to get creative a bit but, for the most part, as long as you don't have any DB-engine-specific code in your entity classes, everything should work just fine. I for example switched a project from HSQLDB to SQLite and the only thing that did not work was the identity generation i mentioned earlier. If i were you i would experiment with different dialects and thoroughly test everything.
EDIT
Just saw your edit and that is definitely engine-specific code. In this case you might indeed need different entities to accommodate the specific data type you want to explicitly assign to each column.
Good luck!
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319915%2fcan-we-use-same-hibernate-annotated-pojo-class-for-multiple-databases%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If it's ok to use nvarchar
for all String
typed columns by default, you could extend Oracle and MS SQLServer dialects and do something like this:
public class CustomOracleDialect extends org.hibernate.dialect.Oracle10gDialect
@Override
protected void registerCharacterTypeMappings()
super.registerCharacterTypeMappings();
registerColumnType(Types.VARCHAR, "nvarchar2");
public class CustomSQLServerDialect extends org.hibernate.dialect.SQLServer2012Dialect
public CustomSQLServerDialect()
super();
registerColumnType(Types.VARCHAR, "nvarchar");
Then configure these dialects in dependence to the database type used.
add a comment |
If it's ok to use nvarchar
for all String
typed columns by default, you could extend Oracle and MS SQLServer dialects and do something like this:
public class CustomOracleDialect extends org.hibernate.dialect.Oracle10gDialect
@Override
protected void registerCharacterTypeMappings()
super.registerCharacterTypeMappings();
registerColumnType(Types.VARCHAR, "nvarchar2");
public class CustomSQLServerDialect extends org.hibernate.dialect.SQLServer2012Dialect
public CustomSQLServerDialect()
super();
registerColumnType(Types.VARCHAR, "nvarchar");
Then configure these dialects in dependence to the database type used.
add a comment |
If it's ok to use nvarchar
for all String
typed columns by default, you could extend Oracle and MS SQLServer dialects and do something like this:
public class CustomOracleDialect extends org.hibernate.dialect.Oracle10gDialect
@Override
protected void registerCharacterTypeMappings()
super.registerCharacterTypeMappings();
registerColumnType(Types.VARCHAR, "nvarchar2");
public class CustomSQLServerDialect extends org.hibernate.dialect.SQLServer2012Dialect
public CustomSQLServerDialect()
super();
registerColumnType(Types.VARCHAR, "nvarchar");
Then configure these dialects in dependence to the database type used.
If it's ok to use nvarchar
for all String
typed columns by default, you could extend Oracle and MS SQLServer dialects and do something like this:
public class CustomOracleDialect extends org.hibernate.dialect.Oracle10gDialect
@Override
protected void registerCharacterTypeMappings()
super.registerCharacterTypeMappings();
registerColumnType(Types.VARCHAR, "nvarchar2");
public class CustomSQLServerDialect extends org.hibernate.dialect.SQLServer2012Dialect
public CustomSQLServerDialect()
super();
registerColumnType(Types.VARCHAR, "nvarchar");
Then configure these dialects in dependence to the database type used.
edited Nov 19 '18 at 7:26
answered Nov 15 '18 at 14:05
SelaronSelaron
2,48611323
2,48611323
add a comment |
add a comment |
The same exact class WILL work with other DB engines, as far as Hibernate is concerned all it cares about is the dialect for the most part. However, some DB engines don't support the identity generation strategy for ID fields for example (from past experience). Depending on what DB engines you are required to work with you may have to get creative a bit but, for the most part, as long as you don't have any DB-engine-specific code in your entity classes, everything should work just fine. I for example switched a project from HSQLDB to SQLite and the only thing that did not work was the identity generation i mentioned earlier. If i were you i would experiment with different dialects and thoroughly test everything.
EDIT
Just saw your edit and that is definitely engine-specific code. In this case you might indeed need different entities to accommodate the specific data type you want to explicitly assign to each column.
Good luck!
add a comment |
The same exact class WILL work with other DB engines, as far as Hibernate is concerned all it cares about is the dialect for the most part. However, some DB engines don't support the identity generation strategy for ID fields for example (from past experience). Depending on what DB engines you are required to work with you may have to get creative a bit but, for the most part, as long as you don't have any DB-engine-specific code in your entity classes, everything should work just fine. I for example switched a project from HSQLDB to SQLite and the only thing that did not work was the identity generation i mentioned earlier. If i were you i would experiment with different dialects and thoroughly test everything.
EDIT
Just saw your edit and that is definitely engine-specific code. In this case you might indeed need different entities to accommodate the specific data type you want to explicitly assign to each column.
Good luck!
add a comment |
The same exact class WILL work with other DB engines, as far as Hibernate is concerned all it cares about is the dialect for the most part. However, some DB engines don't support the identity generation strategy for ID fields for example (from past experience). Depending on what DB engines you are required to work with you may have to get creative a bit but, for the most part, as long as you don't have any DB-engine-specific code in your entity classes, everything should work just fine. I for example switched a project from HSQLDB to SQLite and the only thing that did not work was the identity generation i mentioned earlier. If i were you i would experiment with different dialects and thoroughly test everything.
EDIT
Just saw your edit and that is definitely engine-specific code. In this case you might indeed need different entities to accommodate the specific data type you want to explicitly assign to each column.
Good luck!
The same exact class WILL work with other DB engines, as far as Hibernate is concerned all it cares about is the dialect for the most part. However, some DB engines don't support the identity generation strategy for ID fields for example (from past experience). Depending on what DB engines you are required to work with you may have to get creative a bit but, for the most part, as long as you don't have any DB-engine-specific code in your entity classes, everything should work just fine. I for example switched a project from HSQLDB to SQLite and the only thing that did not work was the identity generation i mentioned earlier. If i were you i would experiment with different dialects and thoroughly test everything.
EDIT
Just saw your edit and that is definitely engine-specific code. In this case you might indeed need different entities to accommodate the specific data type you want to explicitly assign to each column.
Good luck!
answered Nov 15 '18 at 13:26
MartinMartin
145220
145220
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319915%2fcan-we-use-same-hibernate-annotated-pojo-class-for-multiple-databases%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Which java package and hibernate version is this
@Column
annotation from?– Selaron
Nov 15 '18 at 13:48
javax.persistence.Column and hibernate version 5.0.2
– Ahmad
Nov 15 '18 at 13:50
I did not find an annotation that provides an
sql-type
attribute. Did you mean Column#columnDefinition attribute instead? docs.oracle.com/javaee/7/api/javax/persistence/…– Selaron
Nov 15 '18 at 14:33
if you question is answered, please mark it answerd.
– Selaron
Jan 15 at 10:20