gpt4 book ai didi

java - 实体 JPA 中的 session 范围属性

转载 作者:行者123 更新时间:2023-12-02 09:32:52 25 4
gpt4 key购买 nike

我对 JPA 很陌生。我的问题是:是否可以在实体中定义一个属性,该属性未保留在数据库中但属于 SessionScoped?

@Entity
@Table(name = "article_v_m")
public class Article implements Serializable {
@Id
@Column(name = "cart")
private String ref;

@Transient
public static final List<String> STATUS_PUBLISHED = Collections.unmodifiableList(Arrays.asList("", "D", "R"));

@Transient
public static final List<String> STATUS_DEAD = Collections.unmodifiableList(Arrays.asList("M", "E", "V"));

@Transient
public static final List<String> STATUS_UPCOMING = Collections.unmodifiableList(Arrays.asList("A"));

// I want this property to be SessionScoped
// The problem is that it persists between sessions
// I know this is because of the @Transient annotation
@Transient
public Double realDiscountPercent = 0.00;

@Column(name = "isbn")
private String isbn;

@Column(name = "lart")
private String title;

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}

public String getTitle() {
if (fullTitle == null || fullTitle.isEmpty()) {
return title;
}
return fullTitle;
}

public void setTitle(String title) {
this.title = title;
}

public Double getRealDiscountPercent() {
return realDiscountPercent;
}

public void setRealDiscountPercent(Double realDiscountPercent) {
this.realDiscountPercent = realDiscountPercent;
}
}

目的是在 View 之间检索 realDiscountPercent,但在 session 关闭时重置它。我在市场 View 中计算它,并希望在球童 View 中获取此信息。现在,即使我断开连接并重新连接到另一个帐户,该值仍然保持不变。

最佳答案

对于 session 范围内的属性,它需要是一个 singleton bean,这样您就可以创建一个如下的 bean:

@Component
@Scope("session")
public class MyStringProvider implements Provider<String> {

private String value = "something";

public String get() {
return this.value;
}
}

然后你可以像这样访问它:

@Autowired
private Provider<String> myStringProvider;

...

System.out.println(myStringProvider.get());

关于java - 实体 JPA 中的 session 范围属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57807692/

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