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.
Related
I am wondering how does Spring JPA derive queries from methods. As an example, if I was to type
interface CarRepo extends CrudRepository<Car, Long>{ findByCarMake(Make make) }
my query would be automatically derived from the method and would be something as "SELECT * from Car WHERE carMake = xxxxx"
I do understand this concepts but I would like to understand how it works behind the scenes. So, how does it actually derive a query from the method name?
I am aiming at creating a similar thing to suit our needs for a NestJs project so in Typescript not Java and also for an..."unorthodox" database which does not have such support out of the box( Neo4J).
I ll be very grateful to whom can and will help me.
Spring Data JPA derives the queries from the method names in your repository.
There are certain keywords that are reserved by Spring. One the one hand, there are query subject keywords like findBy, existsBy, countBy, etc. which influence the return type of the method. On the other hand, there are operators like and, or, isIn, between, etc. that are applied to the actual query logic.
You start your query with a query subject keyword like findBy and then the fields of your entity (and optionally operators and more fields). You can even have nested fields like findByProviderName where your entity has a field provider which has a field name. If you define an invalid property or property path (e.g. findByProviderNamw), your Spring Boot application would fail on startup. You can find more about defining query methods in the official spring reference.
Spring Data using part tree JPA queries, than map them into SQL query by pre-defined parts.
#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.
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
I have the following question, which is not covered in Hibernate documentation. Or I just couldn't find the answer there. Googling doesn't give me details also.
If we use an Entity object as a parameter and bind it to an HQL using Query.setParameter, what happens next?
Does hibernate uses only an entity ID of a mapped parameter object to build the SQL 'where' query?
Or Hibernate uses some extra logic (maybe in some cases) which is not limited to ID only? For example, hibernate add additional fields in SQL which are not primary keys.
Is it dangerous to use detached Entity object as a parameter?
Thank you in advance!
In terms of the SQL it will simply compare using the ids. The entity you bind does not have to be managed within that session as the comment on your question suggests.
Essentially what happens is that Hibernate will attempt to resolve the entity type of the entity instance it is given. It will then use that type to bind the JDBC parameter value, which will write just the identifier. So the assumption here is that the entity instance can resolved to its "entity type". That is usually easy in most situations. Where it gets difficult is in the case of Hibernate-specific "entity name" features.
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