我有一个域类
@Entity
@Table(name = "user")
open class User(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
open var id: Long = 0L,
@Column(nullable = false)
var name: String? = null)
我有这个域类的子类,我想添加一个JsonIgnore批注:
open class UserFiltered(
@Id
@JsonIgnore
@GeneratedValue(strategy = GenerationType.IDENTITY)
override var id: Long = 0L
): User()
但是当我尝试将用户转换为UserFiltered时:
return ResponseModel<UserFiltered>(true, userModified as UserFiltered)
我收到以下错误:
java.lang.ClassCastException: com.example.platform.database.domain.User无法强制转换为 com.example.platform.model.UserFiltered位于 com.example.platform.controller.UserController.modifyUser(UserController.kt:71) 〜[classes /:na]
在Kotlin中是否有办法实现这一目标,否则我将不得不进行完整的转换,在User上设置每个参数?
答案 0 :(得分:0)
我不得不将子类修改为以下形式:
open class UserFiltered(
@Id
@JsonIgnore
@GeneratedValue(strategy = GenerationType.IDENTITY)
override var id: Long = 0L
): User(){
constructor(user: User): this()
}
并创建UserFiltered的实例
return ResponseModel<UserFiltered>(true, UserFiltered(userModified))