kotlin: some problems with arrays in annotations

你离开我真会死。 提交于 2020-01-23 10:53:21

问题


I have a some problem in annotations:

Entity Table(uniqueConstraints = array(UniqueConstraint(columnNames = array("key", "userid")))) 
public class ...

In this case I get the following error:

Type inference failed. Expected type mismatch: found: kotlin.Array required: kotlin.String

There is no problems with uniqueConstraints = array(...) but Idea shows me error in columnNames = array(...)

I using hibernate-jpa-2.1-api-1.0.0.Final.jar


Workaround: Instead uniqueConstraints I using composite key (@javax.persistence.IdClass)


回答1:


Use the spread operator:

UniqueConstraint(columnNames = *array("key", "userid"))



回答2:


This works for me:

@Table(uniqueConstraints = arrayOf(
        UniqueConstraint(columnNames = arrayOf("key", "key"))
))

Also for new version kotlin you may do that:

@Table(uniqueConstraints = [
    UniqueConstraint(columnNames = ["key", "key"])
])



回答3:


Was struggling with this myself today. The following snippet works with the most recent version of Kotlin:

@ApiResponses(value = *arrayOf(
        ApiResponse(code = 403, message = "Unauthorized"),
        ApiResponse(code = 404, message = "Item not found")))

or a shorter option:

@ApiResponses(
    ApiResponse(code = 403, message = "Unauthorized"),
    ApiResponse(code = 404, message = "No active snapshots"))

if an annotation with an array is defined as:

public @interface ApiResponses {
    ApiResponse[] value();
}


来源:https://stackoverflow.com/questions/26210704/kotlin-some-problems-with-arrays-in-annotations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!