我正在尝试使用Spring Data / Crud Repository(.save)在DB中保存一个实体,该实体中具有通过@Cache方法加载的另一个实体。换句话说,我正在尝试保存一个具有Attributes实体的广告实体,这些属性是使用Spring @Cache加载的。
因此,我将一个分离的实体传递给Persist异常。
我的问题是,有没有一种方法可以将实体仍然使用@Cache来保存实体?
[我抬起头,但找不到任何人这样做,特别是知道我使用的CrudRepository只有.save()方法,据我所知,它可以管理Persist,Update,Merge等。] >
非常感谢您的帮助。
提前感谢。
Ad.java
@Entity @DynamicInsert @DynamicUpdate @Table(name = "ad") public class Ad implements SearchableAdDefinition { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @ManyToOne(fetch = FetchType.LAZY, optional = false) private User user; @OneToMany(mappedBy = "ad", fetch = FetchType.LAZY, cascade = CascadeType.ALL) private Set<AdAttribute> adAttributes; (.....) }
AdAttribute.java
@Entity @Table(name = "attrib_ad") @IdClass(CompositeAdAttributePk.class) public class AdAttribute { @Id @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "ad_id") private Ad ad; @Id @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "attrib_id") private Attribute attribute; @Column(name = "value", length = 75) private String value; public Ad getAd() { return ad; } public void setAd(Ad ad) { this.ad = ad; } public Attribute getAttribute() { return attribute; } public void setAttribute(Attribute attribute) { this.attribute = attribute; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } } @Embeddable class CompositeAdAttributePk implements Serializable { private Ad ad; private Attribute attribute; public CompositeAdAttributePk() { } public CompositeAdAttributePk(Ad ad, Attribute attribute) { this.ad = ad; this.attribute = attribute; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; CompositeAdAttributePk compositeAdAttributePk = (CompositeAdAttributePk) o; return ad.getId().equals(compositeAdAttributePk.ad.getId()) && attribute.getId().equals(compositeAdAttributePk.attribute.getId()); } @Override public int hashCode() { return Objects.hash(ad.getId(), attribute.getId()); } }
用于加载属性的方法:
@Cacheable(value = "requiredAttributePerCategory", key = "#category.id") public List<CategoryAttribute> findRequiredCategoryAttributesByCategory(Category category) { return categoryAttributeRepository.findCategoryAttributesByCategoryAndAttribute_Required(category, 1); }
用于创建/保留广告的方法:
@Transactional
public Ad create(String title, User user, Category category, AdStatus status, String description, String url, Double price, AdPriceType priceType, Integer photoCount, Double minimumBid, Integer options, Importer importer, Set<AdAttribute> adAtributes) {
//Assert.notNull(title, "Ad title must not be null");
Ad ad = adCreationService.createAd(title, user, category, status, description, url, price, priceType, photoCount, minimumBid, options, importer, adAtributes);
for (AdAttribute adAttribute : ad.getAdAttributes()) {
adAttribute.setAd(ad);
/* If I add this here, I don't face any exception, but then I don't take benefit from using cache:
Attribute attribute = attributeRepository.findById(adAttribute.getAttribute().getId()).get();
adAttribute.setAttribute(attribute);
*/
}
ad = adRepository.save(ad);
solrAdDocumentRepository.save(AdDocument.adDocumentBuilder(ad));
return ad;
}
我正在尝试使用Spring Data / Crud Repository(.save)在DB中保存一个实体,该实体中具有通过@Cache方法加载的另一个实体。换句话说,我正在尝试保存一个......>
findRequiredCategoryAttributesByCategory