gpt4 book ai didi

java - application.yaml 中的 Hibernate 参数注释值

转载 作者:太空宇宙 更新时间:2023-11-04 10:12:15 25 4
gpt4 key购买 nike

是否可以在 hibernate 模型类中使用 application.yaml 中的某些变量作为参数而不是硬编码值。

例如。

@GenericGenerator(
name = "generator",
strategy = "cool.nice.package.NiceClass",
parameters = {
@org.hibernate.annotations.Parameter(
name = "service", value = "niceValue"),
}
)

这样我很累,但没用。

@GenericGenerator(
name = "generator",
strategy = "cool.nice.package.NiceClass",
parameters = {
@org.hibernate.annotations.Parameter(
name = "service", value = "${value}"),
}
)

在application.yaml中

value: "niceValue"

还有其他方法可以使这个值通用吗?

我正在使用 Spring 4.3.5,Spring Boot 1.4.3。和 hibernate 5.0.11.Final。

最佳答案

我会通过以下方式做到这一点:

@PropertySource(ignoreResourceNotFound = true, value = "classpath:application.yaml")
@Entity
public class HbnModel{

@Value("${value}")
private String valueHere;

@GenericGenerator(
name = "generator",
strategy = "cool.nice.package.NiceClass",
parameters = {
@org.hibernate.annotations.Parameter(
name = "service", value = valueHere),
}
)
privte String someValue;

// Getters and Setter

}

关于java - application.yaml 中的 Hibernate 参数注释值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52167406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com