我想要的是拥有两个继承自一个用户超类的用户类型。通过用户名/密码对用户进行身份验证时,我不在乎他们当时是谁。但是,一旦他们登录后开始发出请求,那么它将变得很重要。
我不知道我该为继承类型和kotlin中的存储库使用什么。
@MappedSuperClass
open class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val Id: Long = 0
val username:String = ""
val password:String = ""
}
type1
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
data class Type1(
val property1: String) : User
{
val property2: String = ""
}
type2
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
data class Type2(
val property1: String) : User
{
val property2: String = ""
}
我想要的是一个用户存储库,它能够查看用户名和密码,然后查看其他两个的存储库以获取每个数据库的特定属性。我尝试过
@Repository
interface UserRepository: JpaRepository<User, Long> {
fun getUserByUsername(username: String)
}
然后
@Repository
interface Type1Repository: UserRepository<Type1, Long>
,但它不会在类型1存储库中接受<>。
我还没有找到关于Kotlin的很好的资源。如果您知道一个,请传递。谢谢。
答案 0 :(得分:2)
就像这里显示的那样:https://ideone.com/JmqsDn,您只是缺少中间接口中的类型,即:
interface UserRepository<User, Long>: JpaRepository<User, Long> {
fun getUserByUsername(username: String)
}
旁注:kotlin 1.1+是数据类从其他类继承的必需。