在@ConstructorResult中,无法对具有相似类型的字段使用多个映射

时间:2019-04-19 06:21:59

标签: mysql spring hibernate jpa entitymanager

我正在使用@ConstructorResult进行sql结果集映射,但是如果对具有相似类型的字段(例如在给定程序“ firstName”和“ emailId”中)的多个查询进行多个映射,则无法执行以下任务:为此,我必须制作两个参数类型相似的构造函数,这在Java中是不允许的,因此如果这里有多个映射该怎么办。

我也尝试了@EntityResult,但是为此我不得不对类中存在的所有字段进行映射。但是我想为多个结果放置多个映射,例如: 一个查询仅得出userId和firstName, 另一个结果只有userId和emailId等。

package com.example.demo.DO;

import ...;

@Entity
@Table(name = "UserDetail")
@SqlResultSetMapping(name = "USER_DETAIL_MAPPING_1", classes = {
    @ConstructorResult(entityClass = UserDetailDO.class, fields = {
            @ColumnResult(name = "userId", column = "userId"),
            @ColumnResult(name = "profilePicUrl", column = "profilePicUrl"),
            @ColumnResult(name = "firstName", column = "firstName"),
    }) 
})

@SqlResultSetMapping(name = "USER_DETAIL_MAPPING_2", classes = {
    @ConstructorResult(entityClass = UserDetailDO.class, fields = {
            @ColumnResult(name = "userId", column = "userId"),
            @ColumnResult(name = "profilePicUrl", column = "profilePicUrl"),
            @ColumnResult(name = "emailId", column = "emailId"),
    }) 
})

public class UserDetailDO {
@Id
Integer userId;
String profilePicUrl;
String firstName;
String lastName;
String birthDate;
String emailId;

public UserInfoDO(Integer userId, String profilePicUrl, String firstName) {
    super();
    this.userId = userId;
    this.profilePicUrl = profilePicUrl;
    this.firstName = firstName;
}
//This constructor is not allowed because type is same in both constructor
public UserInfoDO(Integer userId, String profilePicUrl, String emailId) {
    super();
    this.userId = userId;
    this.profilePicUrl = profilePicUrl;
    this.emailId = emailId;
}

//Getters & Setters...
}

0 个答案:

没有答案