【问题标题】:Kotlin JPA Inheritance adviseKotlin JPA 继承建议
【发布时间】:2019-01-12 17:23:19
【问题描述】:

我想要的是从一个用户超类继承的两种用户类型。当我通过用户名/密码对用户进行身份验证时,我不在乎他们当时是谁。但是,一旦他们在登录后开始发出请求,它就会变得很重要。

我不知道我应该为继承类型和 kotlin 中的存储库使用什么。

@MappedSuperClass
open class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    val Id: Long = 0

    val username:String = ""

    val password:String = ""
}

类型1

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
data class Type1(
    val property1: String) : User
{
    val property2: String = ""
}

类型2

@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 的好资源。如果你知道一个,请传递它。谢谢。

【问题讨论】:

    标签: jpa kotlin spring-data-jpa


    【解决方案1】:

    如下所示:https://ideone.com/JmqsDn 你只是缺少中间接口中的类型,即:

    interface UserRepository<User, Long>: JpaRepository<User, Long> {
        fun getUserByUsername(username: String)
    }
    

    旁注:数据类需要 kotlin 1.1+ 才能从其他类继承。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-14
      • 2011-08-20
      • 1970-01-01
      • 2021-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      相关资源
      最近更新 更多