envers audit table name - java

I have kind of naive question related to envers. Can we name a audit table to something other then the default one, i.e., TableName_AUD and ya not just Prefix or suffix, full name.

Yes, if you annotate a given entity with #AuditTable you can specify the table that should be used. I don't think you can specify a global naming strategy to use.

use configuration http://docs.jboss.org/envers/docs/index.html#configuration and you've got custom prefix or suffix for audited tables

Related

JPA entities with same name - exact uniqueness requirements for table name of #Table(name="MyPersistedEntity") annotation

#Entity(name="myEntity") which is used in JPQL shall be unique per persistence unit.
#Table defines SQL name of the database table for storing my entity (default name being just unqualified class name). But without specifying scheme this name shall be unique for the entire database.
What if my application uses a single database and different packages use/persist JPA #Entity classes with the same name? I guess I have to make table names unique, so I need to qualify all other entity classes with the same name with #Table(name="somePrefix_MyDuplicateClassName"), or specify scheme like #Table(name="MyDuplicateClassName", schema="specific_Schema_To_Distinguish_Same_ClassNames") right? Concerning schema - I guess it is possible to run into having no privileges to create new schemas, besides not all databases support it. Is schema solution ever used in such situations?
What are the exact requirements of #Table name uniqueness?
What shall be done in such situations in practice?
I find nothing googling and reading JPA specification...
In the #Entity annotation you can specify a different name to achieve uniqueness like so #Entity(name = "MyDuplicateEntity"). This does not reflect on the #Table in any way.

JPA #Formula without Schema Name or Configure with entity class

I have two tables, A_TABLE and B_TABLE. in A_TABLE entity class need on formula which has B_TABLE column combination like below code,
Working Code:
A_TABLEEntity {
#Column("BM_NAME_I")
private String bmdName;
#Formula("(select b.LAST_NAME || ', '||b.FIRST_NAME||' ('||b.BM_NAME||')'
from BR_SCHEMA.B_TABLE b where UPPER(b.BM_NAME)=UPPER(BM_NAME_I))")
private string nameCombinationB;
}
Need solution in Formula :
1) Is it possible to provide any way to give B_TABLEEntity class instead of B_TABLE directly and columns from B_table entity class?
And I have tried with entity class its throwing error, - table or view does not exist
2) Is it possible to avoid to give SCHEMA name in B_TABLE before in formula?
And without schema error is throwing - table or view does not exist
Please help me above #Formula JPA code
You should definitely look here:
https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#mapping-column-formula
According to the described comment, #Formula takes only native sql and the article warns you about coupling to the specific database in some cases.
You should be aware that the #Formula annotation takes a native SQL
clause which may affect database portability.
As the #Formula requires native SQL, you should always include schema. I think that some DB's have default schema that does not need to be defined explicitly.
For some advanced operations, I would probably provide some annotation like #PostLoad and load desired properties using good old entitymanager or direct jdbc.
Maybe these links may help:
https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#basic
https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#fetching

JPA Hibernate Typedef

In the project I am using JPA with Hibernate implementation. All the entities are annotation based and listed in persistence.xml.
All Entities are packaged into jar.
I need to define a typedef for few properties(in all the entities) which are a string to a enum type. In DB the Columns is varchar. How can i achieve this?
Can I achieve this by adding hbm.xml and hibernate-cfg.xml?
Thanks in advance.
Straight from the documentation:
#org.hibernate.annotations.TypeDef and
#org.hibernate.annotations.TypeDefs allows you to declare type
definitions. These annotations can be placed at the class or package
level. Note that these definitions are global for the session factory
(even when defined at the class level). If the type is used on a
single entity, you can place the definition on the entity itself.
Otherwise, it is recommended to place the definition at the package
level.
If you can't change the classes but can change the hibernate config, I think you could change hibernate.mapping.precedence to be "hbm,class", and then add hbm files with all the information in the annotations for the relevant classes. There is hbm syntax to specify a UserType or an EnumType. You'd also have to define your UserType class if using that.
You might want to try this out on a test project before taking my word for it, I'm assuming it would work but I don't know for sure.
Copying all of the persistence information from annotations to hbm could be a pain, and would duplicate information. Try if possible to find a way to add the annotations to the classes themselves.

Hibernate - Should I use a discriminator?

I am taking a table per subclass approach to map some data using hibernate. Typically at the database layer I would introduce a type column in the abstract table and it's subtables, which would enforce that an abstract record can only relate to a subrecord of a matching type. However from looking at various resources it seems to state that the hibernate discriminator is used for legacy databases. Does this mean I am taking the wrong approach?
Table Per (Sub)Class means you have one table per class and no abstract table. Thus a discriminator is not needed. If you have Single Table or Joined inheritance, then you'd need a discriminator.
Edit: actually, Hibernate's Table Per Subclass seems to be the Joined inheritance strategy. However, this is stated by the documentation:
Hibernate's implementation of table per subclass does not require a discriminator column.
As stated above, Hibernate table per subclass does not require a discriminator.
According to the same source:
"... If you want to use a discriminator column with the table per subclass strategy, you can combine the use of subclass and join ..."
See section 9.1.3 in http://docs.jboss.org/hibernate/core/3.3/reference/en/html/inheritance.html#inheritance-tablepersubclass

Using reserved JPQL keywords with JPA

I have an entity class called "Group" and NetBeans warns me "The entity table name is a reserved Java Persistence QL keyword".
A similar case would be the use of reserved SQL keywords.
Will this name be escaped? Would the use of a different table name solve the problem #Table(name="otherName"). Or should I rename the class?
Will this name be escaped?
There is nothing in the JPA spec that says so, if your provider does, this is provider specific.
Would the use of a different table name solve the problem #Table(name="otherName")
Obviously, it would (as long as you don't use another reserved keyword of course). But if you are using a JPA 2.0 provider, there is a standard way to get a db object name escaped, with double quotes:
#Table(name="\"Group\"")
In JPA 1.0, there is nothing standard, it depends on your JPA provider. For example, Hibernate uses backticks:
#Table(name="`Group`")
Or should I rename the class?
No. The table name of an entity defaults to the entity name but you can control it using the #Table annotation as we saw. There is thus no need to change the class name of your entity.
You don't have to rename the class - and you shouldn't - the name you have chosen reflects your domain in the best way, and you should not change it because of tool or framework limitations, in case the tool/framework provides a way to avoid the "clash". JPA provides such a way.

Categories