JPA - What if #Table is not specified for the entity? - java

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.

Related

Set schema for #Embedded tables in JPA annotation?

I'm using JPA to persist my data and have one column that is #embedded and refers to a class that is #embeddable.
When my job runs the tables are created in the schema I specify via the #table annotation. But the data for the #embeddable objects are written in the default schema. I do not see any property to set the schema for the #embeddable tables.
Hope you can point me in the right direction!
The answer seems to be to add the annotation #CollectionTable.

Retrieving mapping entity class name from table name in JPA in runtime

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?

Hibernate doesn't see tables with quotes in name

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.

What's the difference between the name argument in #Entity and #Table when using JPA?

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 { }

EclipseLink/JPA: Multiple #DiscriminatorColumn annotations for Entity

I'm looking for a way in EclipseLink to have two #DiscriminatorColumns on the same entity
My PostreSQL DB table is:
Dictionary
{
id,
object_type,
attribute_type,
translation
}
And classes are:
#Entity
#Table(name = "dictionary")
#Inheritance(strategy=InheritanceType.SINGLE_TABLE)
#DiscriminatorColumn(name="object_type",
discriminatorType=DiscriminatorType.INTEGER)
public class DictionaryRow implements Serializable;
#Entity
#DiscriminatorValue("0")
#DiscriminatorColumn(name="info_type",
discriminatorType=DiscriminatorType.INTEGER)
#Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class DictionaryAttribute extends DictionaryRow;
#Entity
#DiscriminatorValue("1")
public class DictionaryAttributeName extends DictionaryAttribute;
What I'm trying to achieve is that when I call for DictionaryAttributeName it will be resolved to SQL like:
select * from DICTIONARY where info_type = 1 and object_type = 0
But actually, it takes the DiscriminatorColumn from the DictionaryRow class, and DiscriminatorValue from the DictionaryAttributeName, resulting in the totally wrong SQL:
select * from DICTIONARY where object_type = 1
Is there a solution for this issue?
Thanks
According to the JPA 2.0 specification, this is not possible:
11.1.10 DiscriminatorColumn Annotation
For the SINGLE_TABLE mapping
strategy, and typically also for the
JOINED strategy, the persistence
provider will use a type discriminator
column. The DiscriminatorColumn
annotation is used to define the
discriminator column for the
SINGLE_TABLE and JOINED
inheritance mapping strategies.
The strategy and the discriminator column are only specified in the root
of an entity class hierarchy or
subhierarchy in which a different
inheritance strategy is applied.
References
JPA 2.0 Specification
Section 11.1.10 "DiscriminatorColumn Annotation"

Categories

Resources