Hibernate没有正确构建查询

时间:2018-01-27 23:20:04

标签: spring-boot kotlin spring-data-jpa

我的项目是使用Spring Boot和Kotlin构建的。

我有一个可能有父消息的类Message(在响应消息时),代码如下所示:

@Entity
class Message(
    @field: Id
    @field:GeneratedValue var id : Long = 0,

    @field: ManyToOne(targetEntity = Employee::class)
    var sender:Employee?=null,

    @field: ManyToOne(targetEntity = Employee::class)
    var receiver:Employee?=null,
    var time:LocalDateTime,
    var content: String,
    var read:Boolean,

    @field: ManyToOne(targetEntity = Message::class)
    @Nullable
    var father:Message?=null)
{}

调用此查询时(使用JpaRepository):

fun findByReceiverIdAndReadFalse(id:Long):List<Message>

我收到此错误:

Unknown column 'message0_.father_id' in 'field list'

实际应该是message0_.father.id,而不是father_id(父亲是消息实体)。

Hibernate构建的完整查询是:

select
        message0_.id as id1_5_,
        message0_.content as content2_5_,
        message0_.father_id as father_i5_5_,
        message0_.read as read3_5_,
        message0_.receiver_id as receiver6_5_,
        message0_.sender_id as sender_i7_5_,
        message0_.time as time4_5_ 
    from
        message message0_ 
    left outer join
        employee employee1_ 
            on message0_.receiver_id=employee1_.id 
    where
        employee1_.id=? 
        and message0_.read=0

Employee上课:

@Entity
data class Employee(
    @field: Id
    @field:GeneratedValue var id : Long = 0,
    var username : String = "",
    var password : String ="",
    var name : String ="",
    var lastName: String ="",
    var phone : String="",
    var email : String ="",
    var sex : String ="",
    var active : Boolean = false,
    @field: ManyToOne(targetEntity = District::class)
    var district : District? = null,
    @field: ManyToOne(targetEntity = Company::class)
    var company : Company? = null,
    var picture:String="",
    var resetPasswordCode:String="",
    // código que se le envía al usuario en el momento que crea
    // la cuenta, deberá abrir en enlace del correo para activarla
    // de ahí que "active" sea false.
    var activationCode : String="",
    // La fecha en que caduca el código activación: 5 días para activar
    var activationCodeExpiration : LocalDateTime = LocalDateTime.now().plusDays(5),
    var enabled : Boolean = true,
    var accountNonExpired: Boolean = true,
    var credentialsNonExpired: Boolean = true,
    var accountNonLocked : Boolean = true,
    @field: OneToMany(targetEntity = Roles::class) var roles :MutableSet<Roles> = mutableSetOf())

{
    fun toUser() : User
    {
        val authorities = mutableSetOf<GrantedAuthority>()
        roles.forEach{authorities.add(SimpleGrantedAuthority(it.role))}
        return 
User(username,password,enabled,accountNonExpired,credentialsNonExpired,accountNonLocked, authorities)
    }
}

有没有办法强制使用正确的字段? 我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

问题是字段>>> name = ['John','Melissa'] >>> sex = ['Male', 'Female'] >>> name_sex = [] >>> for i in sex: ... if i == 'Male': ... name_sex.append(1) ... else: ... name_sex.append(2) ... >>> print(name_sex) [1, 2] 上没有>>> name_sex1 = [] >>> list1 = list(map(str, name_sex)) >>> list1 >>> for i, j in zip(name, list1): ... a1 = ' '.join([i, j]) ... name_sex1.append(a1) ... >>> for i in name_sex1: ... print(i) ... John 1 Melissa 2 注释。加入专栏说JPA DB(不是字段!)中的哪一列引用了我们当前的实体,而targetEntity说JPA(以及那个对我来说完全过时的那个)哪个类代表我们当前的字段。