MappedSuperClass example!

Documentation is here!

BaseModelObject.java
package biz.tugay.jpaExamples.model;
 
import javax.persistence.*;
import java.io.Serializable;
 
@MappedSuperclass
public abstract class BaseModelObject implements Serializable {
 
    protected long id;
 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public long getId() {
        return id;
    }
 
    public void setId(final long id) {
        this.id = id;
    }
}

Employee.java
package biz.tugay.jpaExamples.model;
 
import javax.persistence.*;
 
@Entity
@NamedQuery(
        name = "all",
        query = "SELECT e FROM Employee e"
)
@EntityListeners(
        EmployeePersistenceListener.class
)
public class Employee extends BaseModelObject {
 
    private String firstname;
    private String lastname;
    private String address;
    private String city;
 
    private EmployeeLedger employeeLedger;
 
    public String getFirstname() {
        return firstname;
    }
 
    public void setFirstname(final String firstname) {
        this.firstname = firstname;
    }
 
    public String getLastname() {
        return lastname;
    }
 
    public void setLastname(final String lastname) {
        this.lastname = lastname;
    }
 
    public String getAddress() {
        return address;
    }
 
    public void setAddress(final String address) {
        this.address = address;
    }
 
    public String getCity() {
        return city;
    }
 
    public void setCity(final String city) {
        this.city = city;
    }
 
    @OneToOne(
            targetEntity = EmployeeLedger.class,
            fetch = FetchType.EAGER,
            cascade = CascadeType.ALL
    )
    @JoinColumn(
            name = "EMPLOYEE_LEDGER_ID"
    )
    public EmployeeLedger getEmployeeLedger() {
        return employeeLedger;
    }
 
    public void setEmployeeLedger(EmployeeLedger employeeLedger) {
        this.employeeLedger = employeeLedger;
    }
 
    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", firstname='" + firstname + '\'' +
                ", lastname='" + lastname + '\'' +
                ", address='" + address + '\'' +
                ", city='" + city + '\'' +
                ", employeeLedger=" + employeeLedger +
                '}';
    }
}

EmployeeLedger.java
package biz.tugay.jpaExamples.model;
 
import javax.persistence.*;
 
@Entity
@Table(
        name = "EMPLOYEE_LEDGER"
)
public class EmployeeLedger extends BaseModelObject {
 
    private double monthlySalary;
 
    @Column(
            name = "MONTHLY_SALARY"
    )
    public double getMonthlySalary() {
        return monthlySalary;
    }
 
    public void setMonthlySalary(double monthlySalary) {
        this.monthlySalary = monthlySalary;
    }
 
    @Override
    public String toString() {
        return "EmployeeLedger{" +
                "id=" + id +
                ", monthlySalary=" + monthlySalary +
                '}';
    }
}