如何根据Spring Boot中的活动配置文件设置注释属性值?

huangapple go评论83阅读模式
英文:

How to set annotation attribute value based on the active profile in Spring Boot?

问题

我在我的Spring Boot应用程序中使用了两个镜像数据库架构,并在启动时使用Flyway迁移:一个是内存中的H2数据库 - 用于demo配置文件,另一个是Postgres数据库 - 用于prod配置文件。在每个架构中,都有一个名为reset_token的字段,其定义如下:

CREATE TABLE users(	
	... 
    reset_token CHAR(36)
);

问题是,Hibernate希望使用两种不同的注解来正确映射我的User实体中的String属性到相应的表列中:对于H2,使用@Column(columnDefinition = "char")注解,对于Postgres,使用@Column(columnDefinition = "bpchar")注解。我该如何做到这一点呢?我的意思是,是否可以根据活动配置文件的不同来设置columnDefinition属性的值,以便不为每个配置文件创建两个不同的User实体?

@Data
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
@RequiredArgsConstructor
@Entity
@Table(name = "users")
public class User {
	...
    @Column(columnDefinition = "char")
	// @Column(columnDefinition = "bpchar")
    private final String resetToken;        
}
英文:

I use two mirror database schemas in my Spring Boot application with Flyway migration on startup: an in-memory H2 - for demo profile, and Postgres - for prod. In each schema, there is a reset_token field which is defined like this:

CREATE TABLE users(	
	... 
    reset_token CHAR(36)
);

The problem is that Hibernate expects two different annotations to be used for correct mapping of String property in my User entity into corresponding table column: @Column(columnDefinition = "char") - for H2 and @Column(columnDefinition = "bpchar") - for Postgres. How can I do that? I mean, is it possible to set the value of columnDefinition attribute, based on the active profile, so that not to create two different User entities for each profile?

@Data
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
@RequiredArgsConstructor
@Entity
@Table(name = "users")
public class User {
	...
    @Column(columnDefinition = "char")
	// @Column(columnDefinition = "bpchar")
    private final String resetToken;        
}

答案1

得分: 1

你可以尝试坚持使用 @Column(columnDefinition = "bpchar"),并将 ;MODE=PostgreSQL 添加到 H2 JDBC 连接字符串中。

如果这样还不起作用,也许 这个回答会有帮助

如果 那个 也不起作用,那么我建议下面的答案。

英文:

You could try sticking to @Column(columnDefinition = "bpchar") and appending ;MODE=PostgreSQL to the H2 JDBC connection string.

If that doesn't work, perhaps this answer will help.

If that doesn't work either, then I'd suggest the answer just below it.

答案2

得分: 0

我认为你不需要columnDefinition,只需使用@Column。

英文:

I think you don't need columnDefinition, just use @Column

答案3

得分: 0

在注释中,您可以使用final var。因此,您需要添加一个final变量来保存您的tokenType。这个变量将从活动配置中填充。

您可以使用类似以下的方式:

@Value("${tokenType}") // 将从活动配置中获取值
private final String tokenType = "char";

@Column(columnDefinition = tokenType)
private final String resetToken;
英文:

In the annotation you can use final var. So you will need to add a final variable that will hold your tokenType. This var will be populated from the active profile.
You can use something like this:

@Value("${tokenType}") // will take the value from active profile
private final String tokenType = "char";

@Column(columnDefinition = tokenType)
private final String resetToken;

huangapple
  • 本文由 发表于 2020年9月4日 14:51:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63736212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定