spring data jpa inheritance table for subclass:
---------------------------------------------------------------------------
by ,mdhav
mail: gmrdizzy@gmail.com
TUTIORALS FROM MADHAV:
NOTE: 1) maven dependeccy pom.xml , we can get from previous example.
2) presestance.xml , beans.xml file also we can get from my previous example program.
Employee .java
-----------------------------------------------------------------------------------------------------------------------------
package
import
javax.persistence.*;
import
org.springframework.data.jpa.domain.AbstractPersistable;
@Entity
@Table
(name="employee")
@Inheritance
(strategy=InheritanceType.JOINED)
public
class Employee extends AbstractPersistable<Long> {
}
}
}
----------------------------------------------------------------------------------------------
EmployeeRepository .java
------------------------------------------------------------------------------------------------------------------------------------
package
repository;
import
org.springframework.data.jpa.repository.JpaRepository;
import
org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public
interface EmployeeRepository extends JpaRepository<Employee, Long>,
JpaSpecificationExecutor<Employee>{
}
--------------------------------------------------------------------------------------
SalariedEmployee .java
----------------------------------------------------------------------------------------------------------------------------
package
repository;
import
javax.persistence.*;
import
org.springframework.data.jpa.domain.AbstractPersistable;
@Entity
@Table
(name="salariedemp")
@PrimaryKeyJoinColumn
(name="id")
public
class SalariedEmployee extends Employee {
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------
SalariedEmployeeRepository .java
-------------------------------------------------------------------------------------------------------------------------------
package
repository;
import
org.springframework.data.jpa.repository.JpaRepository;
import
org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public
interface SalariedEmployeeRepository extends JpaRepository<SalariedEmployee, Long>,
JpaSpecificationExecutor<SalariedEmployee>{
}
-----------------------------------------------------------------------------------------------------------------------
TestCase .java
--------------------------------------------------------------------------------------------------------------------
package
madhav;
import
repository.*;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Service;
import
org.springframework.transaction.annotation.Transactional;
@Service
public
class TestCase {
{
SalariedEmployee se=
new SalariedEmployee();
se.setAnnualSal(2000);
se.setEname(
"ram");
}
{
}
}
}
}
}
SalariedEmployeeRepository salariedEmployeeRepository) {
}
}
-------------------------------------------------------------------------------------------------------------------------------------
Test .java
---------------------------------------------------------------------------------------------------------------------------------
package madhav;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
Object tc=(Object)ctx.getBean("testCase");
TestCase g=(TestCase)tc;
g.createEmployee();
}
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
Object tc=(Object)ctx.getBean("testCase");
TestCase g=(TestCase)tc;
g.createEmployee();
}
}
---------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------
output from cosole:
04:41:19.766 [main] DEBUG org.hibernate.SQL -
alter table salariedemp
drop
foreign key FKA3327E4F5AC3820D
04:41:20.063 [main] DEBUG org.hibernate.SQL -
drop table if exists employee
04:41:20.111 [main] DEBUG org.hibernate.SQL -
drop table if exists salariedemp
04:41:20.158 [main] DEBUG org.hibernate.SQL -
create table employee (
id bigint not null auto_increment,
ename varchar(255),
primary key (id)
)
04:41:20.251 [main] DEBUG org.hibernate.SQL -
create table salariedemp (
annualSal double precision not null,
id bigint not null,
primary key (id)
)
04:41:20.392 [main] DEBUG org.hibernate.SQL -
alter table salariedemp
add index FKA3327E4F5AC3820D (id),
add constraint FKA3327E4F5AC3820D
foreign key (id)
references employee (id)
04:41:22.833 [main] DEBUG org.hibernate.engine.spi.ActionQueue - Executing identity-insert immediately
04:41:22.926 [main] DEBUG org.hibernate.SQL -
insert
into
employee
(ename)
values
(?)
04:41:23.161 [main] DEBUG o.h.id.IdentifierGeneratorHelper - Natively generated identity: 1
04:41:23.176 [main] DEBUG org.hibernate.SQL -
insert
into
salariedemp
(annualSal, id)
values
(?, ?)
putput from sql prompt:
--------------------------------
No comments:
Post a Comment