创建没有数据库连接的JPA实体



我正在尝试创建一个新的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;
}

AbstractUpdateEvent:

@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;

如果您正在复制jpa实体,您可以尝试使用新的JpaRepository接口-新实体引用的是新的表或相同的表

如果您想拥有实体的副本,请先加载它,然后分离它。

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)

您可以在util类BeanUtils.copyProperties(entity,model(中编写entityToModel类转换器

最新更新