Moving auditing table with java envers to new database instance - java

I have a Mysql DB, schema name "myschema". This schema has tables, where some of this tables are
audit tables. This is my dependency on my java project.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>5.1.0.Final</version>
</dependency>
Now I had like to remore this audit tables on my currect DB and more them on a separate instance, with a branch new uri.
How can this be done? Any advise, thank you in advance

Please take a look at the Configuration Options:
org.hibernate.envers.default_schema
The default schema name that should be used for audit tables.
org.hibernate.envers.default_catalog
The default catalog name that should be used for audit tables.
The option that you should use depends on if your database.
There is also a bug that if you use the default revision entity mappings provided out of the box by Envers that those mappings won't be properly mapped when using these configuration options, only the tables that are related to the entity mappings.
In order to properly map the REVINFO table to the appropriate schema or catalog, a custom revision entity mapping will need to be used in cojunction with a the #Table annotation in order to explicitly specify the schema/catalog. Please see the section Revision Log that describes using a custom #RevisionEntity annotated entity mapping with Envers.

AFAIK Envers does not support using a separate database/DataSource. There seem to be a couple of ways to get the data to the other database: Oracle Database Link - MySQL Equivalent?
Alternatively I guess you could write a custom DataSource that delegates statements to different datasources depending on the statement.
Check if an auditing table is mentioned and if so send it to the audit database.

Related

Hibernate auto code-generator from db schema

I'm trying to auto generate class codes with annotation from existing db (mySql).
I have problem with configuration hibernate tools in eclipse this way to dont map foregin ID-key to int but to class.
Eg, I have entity A with ID indicates to entity B (ofc integer), when I use auto generete that tool maps to int, not to class.
How to configure this tool to achive what i mention above?
Thanks,

HIBERNATE: Auto append word into TABLE

hi I have an entity with this annotation
#Entity
#Table(name = "REPORT_WORK")
But for some reason hibernate keep saying Missing Table: REPORT_REPORT_WORK
I know that is the problem because of when I change the name to "REPORT_WORKX"
It will say Missing Table: REPORT_REPORT_WORKX
Has any encountered this issue before?
Update: when I change the name to JJJJ
It will say Missing Table: REPORT_JJJJ
so for some reason there it is auto appending REPORT_
Configuration:
hibernate.hbm2ddl.auto=validate
I suspect that the problem is your Hibernate configurations. Specifically, if you don't have an appropriate setting for hibernate.hbm2ddl.auto, Hibernate won't automatically update the database schema when you change your model.
(And if you don't want the updates to happen automatically, then you need to figure out what schema changes are needed, code them as SQL DDL, and run them manually.)
Can you post your persistence.xml (or equivalent)?
It sounds like you are implementing org.hibernate.cfg.NamingStrategy, get rid of this configuration.
Some additional info:
JPA (Hibernate) and custom table prefixes

Unit testing Hibernate with multiple database catalogs

I have an issue testing a Hibernate application which queries multiple catalogs/schemas.
The production database is Sybase and in addition to entities mapped to the default catalog/schema there are two entities mapped as below. There are therefore three catalogs in total.
#Table(catalog = "corp_ref_db", schema = "dbo", name = "WORKFORCE_V2")
public class EmployeeRecord implements Serializable {
}
#Table(catalog = "reference", schema = "dbo", name="cntry")
public class Country implements Serializable {
}
This all works in the application without any issues. However when unit testing my usual strategy is to use HSQL with hibernate's ddl flag set to auto and have dbunit populate the tables.
This all works fine when the tables are all in the same schema.
However, since adding these additional tables, testing is broken as the DDL will not run as HSQL only supports one catalog.
create table corp_ref_db.dbo.WORKFORCE_V2
user lacks privilege or object not found: CORP_REF_DB
If there were only two catalogs then I think it would maybe be possible to get round this by changing the default catalog and schema in the HSQL database to that one explicitly defined:
Is there any other in-memory database for which this might work or is there any strategy for getting the tests to run in HSQL.
I had thought of providing an orm.xml file which specified the default catalog and schema (overiding any annotations and having all the defined tables created in the default catalog/schema) however these overrides do not seem to be observed when the DDL is executed i.e. I get the same error as above.
Essentially, then I would like to run my existing tests and either somehow have the tables created as they are defined in the mappings or somehow override the catalog/schema definitions at the entity level.
I cannot think of any way to achieve either outcome. Any ideas?
I believe H2 supports catalogs. I haven't used them in it myself, but there's a CATALOGS table in the Information Schema.
I managed to achieve something like this in H2 via IGNORE_CATALOGS property and version 1.4.200
However, the url example from their docs did not seem to work for me, so I added a statement in my schema.xml:
SET IGNORE_CATALOGS = true;

Hibernate:how to make entity class with all database fields automatically

I am working on a project with MySQL,Hibernate,Java,GWT
I am making an entity class where i am making each and every field as per the fields in mysql
like there is a table UserInfo with fields ,id,name,password,and many other fields in MySQL
now when i am making an entity class in java/hibernate
I have to create each field i.e id,name,password and others with my hand
I was just wondering if there's a way with which my entity class automatically detects all fields which are in my Database table and make all the fields in java itself and link them with the database fields.
thanks
hibernate has a property called "hibernate.hbm2ddl.auto".
hibernate.hbm2ddl.auto
Automatically validates or exports schema DDL
to the database when the SessionFactory is created. With create-drop,
the database schema will be dropped when the SessionFactory is closed
explicitly.
e.g. validate | update | create | create-drop
you can set the proper value when you start creating your sessionfactory. then you will see the tables were created in your datastore, of course, the table creation was based on your entity classes.
if you want to generate java classes (entities) from your database table. you may want to check hibernate reverse engineering out:
http://docs.jboss.org/tools/3.3.0.M5/en/hibernatetools/html/reverseengineering.html
Try Hibernate Tools.

How to create table from Hibernate POJO class ? How to implement reverse enginnering strategies in Hibernate?

I have witten the POJO class, *.cfg.xml file, *.hbm.xml file and hibernate code to retrieve data.
But I didn't created any table. Is there any tag available in hibernate which will create table by using configuration files.
How to implement reverse engineering in hibernate ?
Thanks !
You can actually set a configuration property and instruct hibernate to create the database schema for you.
hibernate.hbm2ddl.auto = create-drop | update
See this link Asking hibernate to generate schema for you

Categories