Home > Net >  JPA Entity class which is not mapped to any table
JPA Entity class which is not mapped to any table

Time:03-26

I am using a entity class for mixing two/three table columns in one entity to hold an outcome of SYS_REFCURSOR in oracle

This allows me to have single class which is not mapped to any table but it still is an Entity

@Data
@Entity
@NoArgsConstructor
class EmployeeDetails {
    @Id
    @Column("emp_id")
    String empId;
    
    @Column("job_name")
    String jobName;

    @Column("dept_name")
    String deptName;

    //Future requirement
    //String updatedBy
}

Now I have an additional requirement, to add who last modified the employee table, I don't want modify the procedure now, the procedure is being re-used in another background procedure and batch jobs.

My question is, is it possible to use @ManyToOne on this class which is obviously not mapped to any table

If not how do avoid manually looping a child array list, is there a ready made option in JPA or spring boot to achieve that.

Or what will be the smartest/recommended way to bring the below Entity into this class

@Data
@Entity
@NoArgsConstructor
@Table(name="app_users")
class AppUsers {

    @Id
    @Column(name="user_id")
    String userId;

    @Column
    String userName;
}

CodePudding user response:

@Transient, check how this annotation works it will resolve the issue, you need to understand working of @Transient

CodePudding user response:

My spring boot 2.6.2 EntityManager code is as follows

q = em.createStoredProcedureQuery("MY_PROC",EmployeeDetails.class);
q.registerStoredProcedureParameter("OUT_REFC", void.class, ParameterMode.REF_CURSOR);
q.execute();
q.getResultList()

I have modified my class EmployeeDetails as below

@Data
@Entity
@NoArgsConstructor
class EmployeeDetails {
    @Id
    @Column("emp_id")
    String empId;
    
    @Column("job_name")
    String jobName;

    @Column("dept_name")
    String deptName;

    @OneToOne
    @JoinColumn(
      name="user_id",
      referencedColumnName="emp_id", 
      insertable=false, 
      updatable=false, 
      nullable=true
    )
    AppUsers updatedBy;
}

The log prints Hibernate two times one after one as below, first it calls the proc and then it calls the select query, so, I did not wrote that SQL myself, the JPA layer is taking care of it

Hibernate:
  {call MY_PROC(?)}
Hibernate:
  select 
   ...
   ...
  from app_users
 where user_id=?

so, my expectation achieved and I am getting the values

  • Related