Search code examples
javasqljpaibatis

Java Persistence and entity classes


I have two entity classes - A and B. The template of the code is as follows:

class A extends class B  {
    // ...
}

@Entity
@Table("OPERATION")
@DiscriminatorValue()
@Access()
class B  {
    // ...
}

Class B has a schema defination in the .sql file. Class A does not have any schema defination.

My question is, doesn't class A need to have a schema definition in the .sql file as well, in order for it to be mapped to the relational database? I am confused on this and cannot understand how the mapping is taking place.

To use a more concrete example, I have the following two classes Operation and OperationAmend with their code as follows :

 @Entity
 @DiscriminatorValue()
 @Access()
 class OperationAmend extends Operation {   
     //some functions here for amending an operation
 }

 @Entity
 @Table("OPERATION")
 @DiscriminatorValue()
 @Access()
 class Operation  {   
     //some functions here for the operation.
 }

SQL Schema :

 CREATE TABLE OPERATION {    
     OperationId INT;    
     Name VARCHAR(20); 
 }

How can the OperationAmend class be saved even when it has no schema?


Solution

  • In JPA, an entity only has to be declared in the persistence.xml (or scanned for by the provider) for it to be recognized. The provider generates a schema for it. Now, the implementation tables might not exist in the DB, but that's easy enough to fix.