0

Description:

I have two tables, Shop_Employee and Shop_Employee_Type. I wanna display typeName directly when show the employee detail information, but I don't want to config the relationship(OneToMany or ManyToOne) between this two entity.
Because this will load all Shop_Employee_Type column's value, but these values are useless for me, I just need typeName of Shop_Employee_Type. Below is my code, but it doesn't work.

ShopEmployeeType:

@Entity
@Data
//@DynamicUpdate
public class ShopEmployeeType {
    @Id
    private String typeId;
    private String shopId;
    private String typeName;
    private Integer typeStatus;
    private String typeDescription;
}

Shop_Employee:

@Entity
@Data
public class ShopEmployee {

    @Id
    private  String employeeId;
    private String shopId;
    private String typeId;
    private String name;
    private String code;
    private String phone;
    private Integer status;
    private String idcardNumber;
    private String image;
    //@Transient
    private String typeName;

    public ShopEmployee() {
    }
}

Repository:

@Query(value = "select u.*,t.type_name from shop_employee u inner join shop_employee_type t on u.type_id=t.type_id", nativeQuery = true)
List<ShopEmployee> findAllData();

This could show typeName as I wished, but there is an error appears when I save a new entity Shop_Employee; If I add a @Transient for 'typeName', It could save successfully, but the value of 'typeName' is null when I query entity Shop_Employee.

1 Answer 1

2

Your query should return two Objects Shop_Employee and a String, so the return results should not be List<ShopEmployee> it should be :

@Query(value = "select u.*, t.type_name from shop_employee ...", nativeQuery = true)
List<Object[]> findAllData();

Then you can get the ShopEmployee using :

List<Object[]> list = findAllData();
for(Object[] obj : list){
   ShopEmployee shopEmployee = (ShopEmployee) obj[0];
   String type_name = (String) obj[1];
}

So, in ShopEmployee entity you don't need to use :

//@Transient
//private String typeName;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.