我正在嘗試創建一個新的 JPA 物體而不在資料庫中生成新行。
我已經嘗試復制現有物體,但問題是副本似乎與原始物體有聯系(我猜是因為 ID)。因為當我嘗試設定新值時,為(副本和原始)設定了值。
問題是:我有一個來自這個串列的物件串列(JPA 物體),一些物件需要添加在一起。添加物件的總和將顯示在新物件中 - 但該物件不得出現在資料庫中。
這是物體的樣子:
@Aggregate
@Entity
@Table(name = "CUSTOMER_PORTFOLIO_UE",
indexes = {
@Index(columnList = "isin"),
@Index(columnList = "clearstreamDepotNumber"),
@Index(columnList = "validFromDate"),
@Index(columnList = "isin, clearstreamDepotNumber, validFromDate")
})
public class CustomerPortfolioUpdateEvent extends AbstractUpdateEvent implements HasLogicalKey<LogicalCustomerPortfolioKey> {
@NotEmpty
@Length(max = 7)
@Column(nullable = false, length = 7)
private String customerAccountNumber;
@NotNull
@Length(max = 3)
@Column(nullable = false, length = 3)
private String portfolioId;
@NotNull
@Length(min = 12, max = 12)
@Column(nullable = false, length = 12)
private String isin;
@NotEmpty
@Length(max = 7)
@Column(nullable = false, length = 7)
private String depotNumber;
@Nullable
@Length(max = 20)
@Column(length = 20)
private String customerAccountShortName;
@Nullable
@Length(max = 1)
@Column(length = 1)
private String customerGroupId;
@NotNull
@Column(nullable = false, length = 4)
private LegalEntity legalEntity;
@Nullable
@Length(max = 6)
@Column(length = 6)
private String customerSectorId;
@Nullable
@Length(max = 20)
@Column(length = 20)
private String customerSectorName;
@Nullable
@Length(max = 8)
@Column(length = 8)
private String customerAdvisorNumber;
@Nullable
@Length(max = 20)
@Column(length = 20)
private String customerAdvisorName;
@NotNull
@Length(min = 2, max = 3)
@Column(nullable = false, length = 3)
private String taxCountryId;
@NotNull
@Length(min = 2, max = 3)
@Column(nullable = false, length = 3)
private String countryId;
@Nullable
@Length(max = 20)
@Column(length = 20)
private String depotName;
@Nullable
@Length(max = 20)
@Column(length = 20)
private String clearstreamDepotNumber;
@NotNull
@Column(nullable = false, columnDefinition = "DATE")
private LocalDate validFromDate;
@Nullable
@Column(precision = 18, scale = 3)
private BigDecimal openPositionValue;
@Nullable
@Column(precision = 18, scale = 3)
private BigDecimal settledPositionValue;
@NotNull
@Column(nullable = false, precision = 18, scale = 3)
private BigDecimal tradingPositionValue;
@Nullable
@Length(max = 3)
@Column(length = 3)
private String isinSub;
protected CustomerPortfolioUpdateEvent() { }
public CustomerPortfolioUpdateEvent(
@NotNull Timestamp recordedAt,
@NotNull @Length(min = 7, max = 7) String customerAccountNumber,
@NotNull @Length(max = 3) String portfolioId,
@NotNull @Length(min = 12, max = 12) String isin,
@NotNull @Length(max = 7) String depotNumber,
@Nullable String customerAccountShortName,
@Nullable String customerGroupId,
@NotNull LegalEntity legalEntity,
@Nullable @Length(max = 6) String customerSectorId,
@Nullable @Length(max = 20) String customerSectorName,
@Nullable @Length(max = 8) String customerAdvisorNumber,
@Nullable @Length(max = 20) String customerAdvisorName,
@NotNull @Length(min = 2, max = 3) String taxCountryId,
@NotNull @Length(min = 2, max = 3) String countryId,
@Nullable @Length(max = 20) String depotName,
@Nullable @Length(max = 20) String clearstreamDepotNumber,
@NotNull LocalDate validFromDate,
@Nullable BigDecimal openPositionValue,
@Nullable BigDecimal settledPositionValue,
@NotNull BigDecimal tradingPositionValue,
@Nullable String isinSub
) {
super(recordedAt);
this.customerAccountNumber = customerAccountNumber;
this.portfolioId = portfolioId;
this.isin = isin;
this.depotNumber = depotNumber;
this.customerAccountShortName = customerAccountShortName;
this.customerGroupId = customerGroupId;
this.legalEntity = legalEntity;
this.customerSectorId = customerSectorId;
this.customerSectorName = customerSectorName;
this.customerAdvisorNumber = customerAdvisorNumber;
this.customerAdvisorName = customerAdvisorName;
this.taxCountryId = taxCountryId;
this.countryId = countryId;
this.depotName = depotName;
this.clearstreamDepotNumber = clearstreamDepotNumber;
this.validFromDate = validFromDate;
this.openPositionValue = openPositionValue;
this.settledPositionValue = settledPositionValue;
this.tradingPositionValue = tradingPositionValue;
this.isinSub = isinSub;
}
抽象更新事件:
@MappedSuperclass
public abstract class AbstractUpdateEvent implements UpdateEvent {
public static final String SYSTEM_TRIGGERER = "SYSTEM";
@Id
@NotNull
@Length(max = IdGenerator.MAX_LENGTH)
@Column(length = IdGenerator.MAX_LENGTH)
private String id;
@NotNull
@Column(nullable= false)
private Timestamp recordedAt;
uj5u.com熱心網友回復:
如果您正在復制 jpa 物體,您可以嘗試使用新的 JpaRepository 介面,新物體指的是新表或同一個表
uj5u.com熱心網友回復:
如果您想擁有物體的副本,請先加載它,然后再分離它。
EntityManager em;
//load entity. for example :
A a = em.find(A.class, 39L);
//then detach
em.detach(a)
//you might want to clear the id:
a.setId(null);
//change some values if you want
a.setLabel("foo");
//persist the copy if you want
em.persist(a)
uj5u.com熱心網友回復:
您可以在 util 類 BeanUtils.copyProperties(entity,model) 中撰寫 entityToModel 類轉換器
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/521167.html
標籤:爪哇休眠jpa
