How to retrieve mapping entity class name from table name in JPA in runtime?
Following line
entityClass.getAnnotation(Table.class).name()
returns the table name. Is the opposite also possible in ORM?
Related
I am a newbie to Apache Flink. I am using Pojo Sink to load the data into Cassandra. Right now, I am specifying table and keyspace names with the help of #Table annotation.
Now, I want to pass table name and keyspace name dynamically on run time so that I can load data into tables specified by user. Is there any way to achieve this?
#Table is a CQL annotation that defines which table this class entity maps to. AFAIK, currently there is no way to make it dynamically mapped to any table at run time, because it would infer to the class name if the name is not specified, i.e.
#Table
public class MyTable {...}
infers a table 'mytable'
#Table(table = "another_table")
public class AnotherTable
infers the table 'another_table'
I referred #Table documentation and it states that:
If no Table annotation is specified for an entity class, the default
values apply.
My question is what is the default value?
The default table name is the unqualified classname of the entityclass and the default schema name is the connected schema from the database connection.
If you specify #Entity and you don't specify #Table, your class will be mapped and in the database you will get the class name as name for your table.
From Marking a POJO as persistent entity section in the documentation:
#Table is set at the class level; it allows you to define the table, catalog, and schema names for your entity mapping. If no #Table is defined the default values are used: the unqualified class name of the entity.
For example if you have:
#Entity
public class MyTest{ ...
Your table will have the name my_test in your database.
Note that PascalCase will be converted to pascal_case. Be aware of that.
Our project is using MyBatis for persisting Java Class Objects to tables and I have a table CUSTOMER_VACATION_PLAN but I need to map this table name to CustomerVacationPlan or simply Vacation. In JPA I used #Table(name="") to do that.
How would I do that in MyBatis?
In mybatis there is absolutely no coupling between table name and class name. The mapping is specified between result set (regardless of which table(s) or view(s) it is constructed from) and class.
<select id="selectVacations" resultType="Vacation">
SELECT * FROM CUSTOMER_VACATION_PLAN WHERE ID = #{id}
</select>
You need to read mybatis mapping documentation as it is very explicit about that.
I have some tables in db(postgresql) with names like this "Test".When i try create java classes from this tables with hibernate its not happening. I get classes from tables with names like this test. How to make hibernate can see tables with quotes in names?
UPDATE
Maybe i write question not correct. But i cant create java classes and i want to know how to do reverse ingenering with tables which have names in qoutes. I cant delete qoutes from table names and column names couse they have names like Type and Full.
By default, Hibernate assumes the database table name is the same as the class name, but you can override this behaviour via the #Table annotation:
#Entity
#Table(name="\"Test\"") // Will use "Test" (including the quotes) as the table name
public class Test {
The #Table annotation is used to specify the table to persist the data. The name attribute refers to the table name. If #Table annotation is not specified then Hibernate will by default use the class name as the table name. So your database's table name is "Test" then you should use your class name is "Test".
Please check your database with
select * from """Test""" if your table name is "Test".
Your entity class should be
#Entity
#Table(name = "\"\"\"Test\"\"\"")
public class Test {
}
It seems that you are not using annotations as of now...
So in case you want to use "Test" as table name you should define the mapping of POJO with your table either via annotations as defined by Bohemian
#Entity
#Table(name="\"Test\"")
public class Test {
or define in the Test.hbm.xml in which you have to map your table and fields to java class and columns.
Alternately, you can specify the schema and database name inside #Table annotation.
#Entity
#Table(name = "Test", schema = "public", catalog = "TestDatabase")
Hibernate will recognize the table without the need to escape double quotes.
I'm using JPA2 and both #Entity and #Table have a name attribute, e. g.:
#Entity(name="Foo")
#Table (name="Bar")
class Baz
What should I use, which ones are optional?
In my specific case I have a class User and a class Group, which have additional requirements (as far as I understand) because they are reserved words in SQL.
How would a working solution look like and with which name would I refer to the entity when writing queries?
Update: I added name="GROUPS" to both annotations in Group and did the same for User, but now I get this error:
Exception Description: The table [USERS] is not present in this descriptor.
Descriptor: RelationalDescriptor(example.Group --> [DatabaseTable(GROUPS)])
and this error
Internal Exception: java.sql.SQLException: Table not found in statement [SELECT ID, MAXIMUMROLE, MEMBERSHIPS_ID FROM USERS]
#Table is optional. #Entity is needed for annotating a POJO class as an entity, but the name attribute is not mandatory.
If you have a class
#Entity
class MyEntity {}
A table with name "MyEntity" will be created and the Entity name will be MyEntity. Your JPQL query would be:
select * from MyEntity
In JPQL you always use the Entity name and by default it is the class name.
if you have a class
#Entity(name="MyEntityName")
#Table(name="MyEntityTableName")
class MyEntity {}
then a table with name MyEntityTableName is created and the entity name is MyEntityName.
Your JPQL query would be :
select * from MyEntityName
The name in #Entity is for JPA-QL queries, it defaults to the class name without package (or unqualified class name, in Java lingo), if you change it you have to make sure you use this name when building queries.
The name in #Table is the table name where this entity is saved.
#Entity is useful with model classes to denote that this is the entity or table
#Table is used to provide any specific name to your table if you want to provide any different name
Note: if you don't use #Table then hibernate consider that #Entity is your table name by default
#Entity
#Table(name = "emp")
public class Employee implements java.io.Serializable { }